Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Fixed switch error code casting bug #32

Merged
merged 1 commit into from
Oct 1, 2013
Merged

Fixed switch error code casting bug #32

merged 1 commit into from
Oct 1, 2013

Conversation

burci
Copy link
Contributor

@burci burci commented Sep 30, 2013

No description provided.

@shaneharter
Copy link
Owner

@burci, walk me through why this is necessary? It's been 9 months since I wrote that code so I can't say I'm fully informed about my decision making back then but looking at the code suggests that sometimes error() is called with null as its first argument? Is it that, sometimes, communication errors are passed in as -1 (line 206..) but we weren't handling that correctly in the error() method? If so, actually, I think I remember that needing to investigate that bug further when I noticed it months ago.

Still, I'm wondering if we won't now have places that do pass in null? Should this be:

case null:
case -1:
 // handle error here?

@burci
Copy link
Contributor Author

burci commented Sep 30, 2013

@shaneharter, the short answer is the switch auto typecast mechanism, and the case null: code never reached.

foreach(array(null, 0, "", "0", "a") as $test){
    switch($test){
        case 0: echo "0"; break;
        case null: echo "N"; break;
    }
}

The result will be 00000, see http:https://www.php.net/manual/en/control-structures.switch.php#41767

The long answer: i am hit by a php bug: https://bugs.php.net/bug.php?id=61369 , so i got $error_code = null; in $this->error() function call https://github.com/shaneharter/PHP-Daemon/blob/master/Core/Worker/Via/SysV.php#L207 . But

        switch($error) {
            case 0:             // Success
            case 4:             // System Interrupt
            case MSG_ENOMSG:    // No message of desired type
                // Ignored Errors
                return true;
                break;

at https://github.com/shaneharter/PHP-Daemon/blob/master/Core/Worker/Via/SysV.php#L362 executed insted of

            case null:
                // Almost certainly an issue with shared memory
                $this->mediator->log("Shared Memory I/O Error at Address {$this->mediator->guid}.");
                $this->mediator->count_error('corruption');
                ...

https://github.com/shaneharter/PHP-Daemon/blob/master/Core/Worker/Via/SysV.php#L407

@shaneharter
Copy link
Owner

I consider myself pretty knowledgable of esoteric php issues. This is a new
one to me. Good catch man. I had the generic observation that the error
handling was sometimes not working perfectly but I never could see how to
reproduce the issue.

Will merge when I get back to my desk.

On Monday, September 30, 2013, Peter Buri wrote:

@shaneharter https://github.com/shaneharter, the short answer is the
switch auto typecast mechanism, and the case null: code never reached.

foreach(array(null, 0, "", "0", "a") as $test){
switch($test){
case 0: echo "0"; break;
case null: echo "N"; break;
}}

The result will be 00000, see
http:https://www.php.net/manual/en/control-structures.switch.php#41767

The long answer: i am hit by a php bug:
https://bugs.php.net/bug.php?id=61369 , so i got $error_code = null; in
$this->error() function call
https://github.com/shaneharter/PHP-Daemon/blob/master/Core/Worker/Via/SysV.php#L207. But

    switch($error) {
        case 0:             // Success
        case 4:             // System Interrupt
        case MSG_ENOMSG:    // No message of desired type
            // Ignored Errors
            return true;
            break;

at
https://github.com/shaneharter/PHP-Daemon/blob/master/Core/Worker/Via/SysV.php#L362executed insted of

        case null:
            // Almost certainly an issue with shared memory
            $this->mediator->log("Shared Memory I/O Error at Address {$this->mediator->guid}.");
            $this->mediator->count_error('corruption');
            ...

https://github.com/shaneharter/PHP-Daemon/blob/master/Core/Worker/Via/SysV.php#L407


Reply to this email directly or view it on GitHubhttps://github.com//pull/32#issuecomment-25403457
.

@shaneharter
Copy link
Owner

@burci Now that I'm at my desk I had a chance to look at the PHP bug you linked. Actually, it's a bug I've encountered myself and is the original reason why I built the integrated debug console so I could step through both sides of the multi-process communication in way that wasn't possible with xdebug and see what the hell was going on. Also, the 2.1 version I just released (feature_abstract_ipc) was done conceptually to separate the concerns better and shrink down the monster v2.0 Core_Worker_Mediator class into something more managable. But it was also done so I/you/somebody can implement other Core_IWorkerVia classes.

Anyway, wanted to give you some moral support with your bugs 👍

@shaneharter
Copy link
Owner

@burci Sorry for emailing you a hundred times :) But I can't merge this until we make a small change. Please update the PR when you get a chance to do so.

The issue is that at L206 now you're overwriting any existing value in $error_code. There are useful non-null values returned from that in some cases. So lets just do a:

if ($error_code === null) $error_code = -1;

I'll go in at some point and get rid of all these "magic number" error codes and add some class constants to make this more self documenting.

@burci
Copy link
Contributor Author

burci commented Oct 1, 2013

@shaneharter sorry i missed that msg_send can change the $error_code.

Should I introduce a const ERROR_UNKNOWN = -1;? Do you have a better constant name idea?

@shaneharter
Copy link
Owner

Yeah, the constant would be great. Name looks good to me. Thanks man, really appreciate your help!!!

@burci
Copy link
Contributor Author

burci commented Oct 1, 2013

Done!

shaneharter added a commit that referenced this pull request Oct 1, 2013
Fixed switch error code casting bug
@shaneharter shaneharter merged commit 13c05c5 into shaneharter:master Oct 1, 2013
@burci burci deleted the bug-sysv-errorcode branch October 1, 2013 19:26
@burci
Copy link
Contributor Author

burci commented Oct 1, 2013

Thx!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants