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

Warning: feof(): 60 is not a valid stream resource in .. /src/Socket/Connection.php on line 18 #4

Closed
webzak opened this issue Jun 11, 2014 · 16 comments

Comments

@webzak
Copy link

webzak commented Jun 11, 2014

When data are short after reading with stream_socket_recvfrom() stream type changes from 'stream' to 'Unknown' and feof($stream) generates warning.

The possible fix is to add check of type before feof()

if ('' === $data || false === $data || get_resource_type($stream) == 'Unknown' || feof($stream))

@brieucthomas
Copy link

I have the same error when the connection is closed from a data listener.

A solution : detect the end of stream before dispatching data event.

if ('' === $data || false === $data || feof($stream)) {
    $this->end();
} else {
    $this->emit('data', array($data, $this));
}

A temporary solution is to specify false on closure output :

$conn->end(false);

@cboden
Copy link
Member

cboden commented Aug 16, 2014

Would you be able to provide instructions on how to replicate this error?

@jeremykendall
Copy link

I got (almost) the same error this morning while working on the DNS basic usage example. Maybe this could help replicate the warning:

<?php
// dns.php
error_reporting(PHP_INT_MAX);

require_once 'vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();
$factory = new \React\Dns\Resolver\Factory();
$dns = $factory->create('8.8.8.8', $loop);

$dns->resolve($argv[1])
    ->then(
        function ($ip) { echo "Host: $ip\n"; },
        function ($e) { echo "Error: {$e->getMessage()}\n"; }
);

$loop->run();

Running php dns.php jeremykendall.net returned:

$ php dns.php jeremykendall.net
Host: 50.57.159.57
PHP Warning:  feof(): 33 is not a valid stream resource in /Users/jeremykendall/dev/explore-react/vendor/react/react/src/Socket/Connection.php on line 18
PHP Stack trace:
PHP   1. {main}() /Users/jeremykendall/dev/explore-react/dns.php:0
PHP   2. React\EventLoop\StreamSelectLoop->run() /Users/jeremykendall/dev/explore-react/dns.php:17
PHP   3. React\EventLoop\StreamSelectLoop->waitForStreamActivity() /Users/jeremykendall/dev/explore-react/vendor/react/react/src/EventLoop/StreamSelectLoop.php:201
PHP   4. call_user_func:{/Users/jeremykendall/dev/explore-react/vendor/react/react/src/EventLoop/StreamSelectLoop.php:227}() /Users/jeremykendall/dev/explore-react/vendor/react/react/src/EventLoop/StreamSelectLoop.php:227
PHP   5. React\Socket\Connection->handleData() /Users/jeremykendall/dev/explore-react/vendor/react/react/src/EventLoop/StreamSelectLoop.php:227
PHP   6. feof() /Users/jeremykendall/dev/explore-react/vendor/react/react/src/Socket/Connection.php:18

Warning: feof(): 33 is not a valid stream resource in /Users/jeremykendall/dev/explore-react/vendor/react/react/src/Socket/Connection.php on line 18

Call Stack:
    0.0002     238968   1. {main}() /Users/jeremykendall/dev/explore-react/dns.php:0
    0.0060     906616   2. React\EventLoop\StreamSelectLoop->run() /Users/jeremykendall/dev/explore-react/dns.php:17
    0.0062     907648   3. React\EventLoop\StreamSelectLoop->waitForStreamActivity() /Users/jeremykendall/dev/explore-react/vendor/react/react/src/EventLoop/StreamSelectLoop.php:201
    0.0515     908184   4. call_user_func:{/Users/jeremykendall/dev/explore-react/vendor/react/react/src/EventLoop/StreamSelectLoop.php:227}() /Users/jeremykendall/dev/explore-react/vendor/react/react/src/EventLoop/StreamSelectLoop.php:227
    0.0515     908312   5. React\Socket\Connection->handleData() /Users/jeremykendall/dev/explore-react/vendor/react/react/src/EventLoop/StreamSelectLoop.php:227
    0.0527     913368   6. feof() /Users/jeremykendall/dev/explore-react/vendor/react/react/src/Socket/Connection.php:18

Environment:

  • Mac OS X 10.9.4
  • PHP 5.5.12
  • React 0.4.1
$ php -v
PHP 5.5.12 (cli) (built: May 23 2014 15:03:24)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.4, Copyright (c) 2002-2014, by Derick Rethans

@RadekDvorak
Copy link

The solution suggested by brieucthomas did work for me though I had to check for NULL data as well.

@andig
Copy link
Contributor

andig commented Oct 7, 2014

Similar problem here running http-client on windows:

PHP Warning:  feof(): 52 is not a valid stream resource in C:\andi\htdocs\vz\vendor\react\react\src\Socket\Connection.ph
p on line 18
PHP Stack trace:
PHP   1. {main}() C:\andi\htdocs\vz\http-client.php:0
PHP   2. React\EventLoop\StreamSelectLoop->run() C:\andi\htdocs\vz\http-client.php:40
PHP   3. React\EventLoop\StreamSelectLoop->waitForStreamActivity() C:\andi\htdocs\vz\vendor\react\react\src\EventLoop\St
reamSelectLoop.php:201
PHP   4. call_user_func:{C:\andi\htdocs\vz\vendor\react\react\src\EventLoop\StreamSelectLoop.php:227}() C:\andi\htdocs\v
z\vendor\react\react\src\EventLoop\StreamSelectLoop.php:227
PHP   5. React\Socket\Connection->handleData() C:\andi\htdocs\vz\vendor\react\react\src\EventLoop\StreamSelectLoop.php:2
27
PHP   6. feof() C:\andi\htdocs\vz\vendor\react\react\src\Socket\Connection.php:18

Update: fix from @webzak confirmed working.

@andig
Copy link
Contributor

andig commented Nov 14, 2014

Any chance this is gonna be fixed? Stumpes me every time I'm doing a composer install :(

@clue
Copy link
Member

clue commented Nov 15, 2014

@jeremykendall, thanks for your script! We can now successfully reproduce this issue. I believe this to be an issue in the react/dns component. Would you care to report the error over in the corresponding project?

This might also be the reason why @andig experiences the same issue in the react/http-client component. It uses the react/dns component to resolve hostnames to IPs. @andig, would you care to verify that this should work when you use IPs instead of hostnames?

The underlying problem appears to be that the Connection represents an incoming server side connection. It MUST NOT be used as an outgoing connection in a client side context. I've just filed ticket #13 to update the documentation to reflect this.

Does anybody else experience the same issue in a different context?

@andig
Copy link
Contributor

andig commented Nov 15, 2014

would you care to verify that this should work when you use IPs instead of hostnames?

Verified - this is my current workaround.

Connection represents an incoming server side connection

Does that mean that a new type of client connection needs to be developed now? Would this be goal of this issue then?

@miladr
Copy link

miladr commented Nov 29, 2014

@brieucthomas solution worked for me. No warning anymore.

@clue
Copy link
Member

clue commented Nov 29, 2014

Thanks for the confirmation @andig. The React\Socket\Connection must only be used in a server side context, all other streams should use the React\Stream\Stream class instead. I've filed PR #13 to update the documentation to reflect this, but otherwise everything is already there.

Thanks for reporting back @miladr, would you care to also describe how to reproduce the issue you were having?

See also my above comment for details. As it currently stands it appears to be caused by using React\Socket\Connection in places where a React\Stream\Stream should be used instead.

@miladr
Copy link

miladr commented Nov 29, 2014

You are welcome.

I'm creating a Http testing tool for our project.
I used this code to send Http requests.

<?php

require 'vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

$dnsResolverFactory = new \React\Dns\Resolver\Factory();
$dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);

$factory = new \React\HttpClient\Factory();
$client = $factory->create($loop, $dnsResolver);

foreach(range(1,10) as $index){

    echo "Request #" . $index . " Had been sent at " . date("i:s",time()) . PHP_EOL;
    $request = $client->request('GET', 'http://httpbin.org/get?index=' . $index);

    $buffer = '';

    $request->on(
        'response',
        function ($response) use (&$buffer) {
            $response->on(
                'data',
                function ($data) use (&$buffer) {
                    $buffer .= $data;
                }
            );
        }
    );
    $request->on(
        'end',
        function () use (&$buffer , $index)  {
            echo "Request #" . $index . " completed at " . date("i:s",time()) . PHP_EOL;
        }
    );

    $request->end(false);
}

$loop->run();

@andig
Copy link
Contributor

andig commented Nov 29, 2014

@clue

Thanks for the confirmation @andig. The React\Socket\Connection must only be used in a server side context, all other streams should use the React\Stream\Stream class instead. I've filed PR #13 to update the documentation to reflect this,

I'm having my slow day. https://github.com/reactphp/dns/blob/master/src/Query/Executor.php uses React\Socket\Connection which should not be the case?

but otherwise everything is already there.

So this is the piece that needs to be fixed and is currently missing imho?

@clue
Copy link
Member

clue commented Nov 29, 2014

Thanks @miladr, so the error you were having also boils down to the react/dns component as described above.

@andig, afaict yes. Unless anybody else happens to come up with any other source of this issue, I'm having to assume this is only caused by the dns component.

@clue
Copy link
Member

clue commented Dec 3, 2014

I've forwarded this error report to the component that is actually the source of this error:

reactphp/dns#13

Afaict this component does not need to be patched.

Can we close this ticket?

@jeremykendall
Copy link

@clue I vote yes. Thanks for your hard work on this.

@andig
Copy link
Contributor

andig commented Dec 3, 2014

@clue that's what I've meant- thank you!

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

No branches or pull requests

8 participants