Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supplied resource is not a valid stream resource #11

Closed
sm2017 opened this issue Jan 9, 2017 · 24 comments
Closed

supplied resource is not a valid stream resource #11

sm2017 opened this issue Jan 9, 2017 · 24 comments
Assignees
Labels
Milestone

Comments

@sm2017
Copy link

sm2017 commented Jan 9, 2017

See walkor/workerman#130

@sm2017
Copy link
Author

sm2017 commented Feb 12, 2017

Reply please , I open this Issue on Jan 9 and I need to solve the problem , Help please

Please see walkor/workerman#130

I test it in php5.6 and libevent , it works well but in php7 and libevent , I have this error

PHP Warning:  stream_set_blocking(): supplied resource is not a valid stream resource in /home/app/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 263

Warning: stream_set_blocking(): supplied resource is not a valid stream resource in /home/app/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 263
PHP Warning:  stream_set_read_buffer(): supplied resource is not a valid stream resource in /home/app/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 266

Warning: stream_set_read_buffer(): supplied resource is not a valid stream resource in /home/app/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 266
PHP Warning:  socket_import_stream(): supplied resource is not a valid stream resource in /home/app/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 270

Warning: socket_import_stream(): supplied resource is not a valid stream resource in /home/app/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 270
PHP Warning:  socket_set_option() expects parameter 1 to be resource, boolean given in /home/app/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 271

Warning: socket_set_option() expects parameter 1 to be resource, boolean given in /home/app/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 271
PHP Warning:  socket_set_option() expects parameter 1 to be resource, boolean given in /home/app/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 272

Warning: socket_set_option() expects parameter 1 to be resource, boolean given in /home/app/vendor/workerman/workerman/Connection/AsyncTcpConnection.php on line 272
PHP Warning:  event_set(): fd argument must be either valid PHP stream or valid PHP socket resource in /home/app/vendor/workerman/workerman/Events/Libevent.php on line 105

Warning: event_set(): fd argument must be either valid PHP stream or valid PHP socket resource in /home/app/vendor/workerman/workerman/Events/Libevent.php on line 105
PHP Warning:  event_set(): fd argument must be either valid PHP stream or valid PHP socket resource in /home/app/vendor/workerman/workerman/Events/Libevent.php on line 105

Warning: event_set(): fd argument must be either valid PHP stream or valid PHP socket resource in /home/app/vendor/workerman/workerman/Events/Libevent.php on line 105

@sm2017
Copy link
Author

sm2017 commented Feb 12, 2017

@ichiriac Help me

@ichiriac
Copy link

Hi @sm2017,

Sorry, I've not saw your issue before today. I think the problem comes with this statement :
https://github.com/walkor/Workerman/blob/master/Connection/AsyncTcpConnection.php#L261

Running this statement :
https://github.com/walkor/Workerman/blob/master/Events/Libevent.php#L127

This will run into the code (and it's okay) :
https://github.com/expressif/pecl-event-libevent/blob/master/libevent.c#L771

Not sure why (bad refcount maybe), but also seems to trigger this code :
https://github.com/expressif/pecl-event-libevent/blob/master/libevent.c#L153

I need to check this in debug mode but for me, when you call the del function, followed by event_del statement, it seems that the object after unset does not have any more references to him. The garbage collector passes, and kills the object (line 153 on libevent.c), and the ressource is also closed.

After investigations, this line is changes to be compliant with PHP7 (zend_list_delete to zend_list_close)

In most user extension functions like mysql_close(), you should use zend_list_close() instead of zend_list_delete(). This closes the actual connection and frees extension specific data structures, but doesn't free the zend_reference structure. that might be still referenced from zval(s). This also doesn't decrement the resource reference counter.

So when the event is destroyed (no more references to him), the connection is closed, that explains your warnings.

One solution could be to revert back to zend_list_delete but I believe that makes memory leaks (not sure).


Sorry but I have no time to work again on libevent, maybe in few month I will be back again on this but for now I'm stuck on other projects.

If you know C fork me and make a pull request, I'll check it and package it for windows libraries.

@sm2017
Copy link
Author

sm2017 commented Feb 19, 2017

@ichiriac , I need to fix the bug , Please fix it ASAP and help me
Thanks

@ichiriac
Copy link

ok @sm2017, ping me back in 1 month if it's not moving. I'll update this issue when it will be fixed.

@sm2017
Copy link
Author

sm2017 commented Feb 20, 2017

Thanks a lot , I will check this page periodically
Thanks again 👍

@forkeer
Copy link

forkeer commented Feb 20, 2017

@ichiriac i have this issue too but i forget that if you solve its nice help for me

@sm2017
Copy link
Author

sm2017 commented Mar 8, 2017

@ichiriac Update me please

@ichiriac ichiriac self-assigned this Mar 8, 2017
@ichiriac ichiriac added the bug label Mar 8, 2017
@ichiriac ichiriac added this to the Stable / PHP7 milestone Mar 8, 2017
@ichiriac
Copy link

ichiriac commented Mar 8, 2017

Hi @sm2017,

I'll take a look at this tonight

@sm2017
Copy link
Author

sm2017 commented Mar 8, 2017

Thanks a lot 😆 👍

ichiriac added a commit that referenced this issue Mar 8, 2017
@ichiriac
Copy link

ichiriac commented Mar 9, 2017

I'll continue tomorrow, I've did not succeed to force the GC with a small code

@ichiriac
Copy link

ichiriac commented Mar 9, 2017

Hi @sm2017,

The issue should be fixed, can you try by rebuilding from sources or do you need windows binaries ?

I can't reproduce the bug directly as in PHP7 it's not easy to trigger the GC, so I need you to test and tell me if it's OK.

Then I'll take tome time to find out a way to trigger properly the garbage collector, in order to cover this ticket with tests

@ichiriac ichiriac reopened this Mar 9, 2017
@sm2017
Copy link
Author

sm2017 commented Mar 11, 2017

I install libevent @05900f3b8d70deb424b3193a4eb850062f146638 , but same result , I have supplied resource is not a valid stream resource error

Read http://php.net/manual/en/function.gc-collect-cycles.php maybe helps

@sm2017
Copy link
Author

sm2017 commented Mar 12, 2017

See #16 I found that the problem is event_free that called at https://github.com/reactphp/event-loop/blob/master/src/LibEventLoop.php#L103

Before event_free the $fd is resource of type (stream) and after resource of type (Unknown) so in next lines we have supplied resource is not a valid stream resource error

@ichiriac
Copy link

@sm2017
Copy link
Author

sm2017 commented Mar 12, 2017

I install again libevent extension and run test script the result is

<?php

/* poll STDIN using basic API */
function foo($fd, $events, $arg)
{
    static $i;

    $i++;

    if ($i == 10) {
        event_base_loopexit($arg[1]);
    }
    var_dump(fread($fd, 1));
}


$base = event_base_new();

$fd = fopen('https://raw.githubusercontent.com/expressif/pecl-event-libevent/master/tests/input.txt', 'r');

// first event reader
$ev1 = event_new();
var_dump(event_set($ev1, $fd, EV_READ | EV_PERSIST, "foo", array($ev1, $base)));
var_dump(event_base_set($ev1, $base));
var_dump(event_add($ev1));
var_dump(event_base_loop($base, EVLOOP_ONCE));
var_dump(event_del($ev1));
var_dump($fd);
event_free($ev1);
unset($ev1);
var_dump($fd);

// second event reader
$ev2 = event_new();
var_dump(event_set($ev2, $fd, EV_READ | EV_PERSIST, "foo", array($ev2, $base)));
var_dump(event_base_set($ev2, $base));
var_dump(event_add($ev2));
var_dump(event_base_loop($base, EVLOOP_ONCE));
var_dump(event_del($ev2));
unset($ev2);

?>
bool(true)
bool(true)
bool(true)
string(1) "0"
int(0)
bool(true)
resource(6) of type (stream)
resource(6) of type (Unknown)
PHP Warning:  event_set(): fd argument must be either valid PHP stream or valid PHP socket resource in /home/libeventBug/test.php on line 35
bool(false)
bool(false)
PHP Warning:  event_add(): Unable to add event without an event base in /home/libeventBug/test.php on line 37
bool(false)
int(1)
PHP Warning:  event_del(): Unable to delete event without an event base in /home/libeventBug/test.php on line 39
bool(false)
[warn] event_del: event has no event_base set.

@ichiriac
Copy link

@sm2017, I'm looking at it, I'll tell you when it's fixed so you can test

@sm2017
Copy link
Author

sm2017 commented Mar 12, 2017

@ichiriac Ok just I found another issue

See https://github.com/walkor/Workerman/blob/master/Events/Libevent.php#L84 , the 2th argument of event_set is 0 that is integer not resource , I see http://php.net/manual/en/function.event-set.php and 2th argument is mixed and Signal number or resource indicating the stream. but with expressif/pecl-event-libevent we have this error PHP Warning: event_set(): invalid file descriptor passed

ichiriac added a commit that referenced this issue Mar 12, 2017
@ichiriac
Copy link

@sm2017, this bug is now fixed, travis passed, waiting your confirmation.

For the other bug can you open a new issue and send a PR with a phpt to reproduce the error, check the output with php5 in order to get the output for the expected section.

@sm2017
Copy link
Author

sm2017 commented Mar 12, 2017

I run test script and it works , but in main project I have issue let me find the problem

@sm2017
Copy link
Author

sm2017 commented Mar 12, 2017

I think we have Segmentation fault in new commit

In this line https://github.com/walkor/Workerman/blob/152756a04f95645217d159f5a496077de0be65cd/Worker.php#L1022 we have pcntl_wait after I install libevent $status is SIGSEGV = 11

@sm2017
Copy link
Author

sm2017 commented Mar 13, 2017

I want to create a phpt file for Segmentation fault bug , But I cannot , Can you help me?

I know the problem is event_add but cannot reproduce error

Please do following

git clone https://github.com/walkor/workerman-chat
cd workerman-chat
composer install
php start.php start

The code running in following order

https://github.com/walkor/GatewayWorker/blob/master/src/Gateway.php#L819
https://github.com/walkor/Workerman/blob/master/Connection/AsyncTcpConnection.php#L171
https://github.com/walkor/Workerman/blob/master/Connection/AsyncTcpConnection.php#L186
https://github.com/walkor/Workerman/blob/master/Events/Libevent.php#L113

The problem is https://github.com/walkor/Workerman/blob/master/Events/Libevent.php#L113 I don't know why in this line script will be terminated and send SIGSEGV to parent process

https://github.com/walkor/Workerman/blob/master/Worker.php#L1022

But in PHP 5.6 and libevent it workes well

@ichiriac
Copy link

Hi @sm2017,

This is another issue, we can continue here : #19

I will close this one as it's fixed.

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

No branches or pull requests

3 participants