From: rdlowrey at gmail dot com Operating system: Fedora 17 PHP version: 5.4.15 Package: Streams related Bug Type: Bug Bug description:async stream_socket_client return
Description: ------------ Normally when creating a client socket connection asynchronously you have to wait until the socket stream can be selected as writable to determine whether or not the connection succeeded. However, when connecting to a nonexistent server on the loopback address there is no way to differentiate between a failed connection and a successful connection using the STREAM_CLIENT_ASYNC_CONNECT flag. The example code connects to a nonexistent server so it should always fail. PHP reports the connection as having succeeded and it's not until you actually attempt to read or write to the socket stream that you can get an accurate `feof()` return value. Test script: --------------- <?php $uri = '127.0.0.1:9000'; // <-- NOTHING LISTENING ON THIS PORT! $flags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT; $socket = stream_socket_client($uri, $errNo, $errStr, $timeout=42, $flags); usleep(50000); // just in case var_dump($socket); // resource(5) of type (stream) $w = [$socket]; $r = $e = []; if (stream_select($r, $w, $e, 0, 0)) { $selectedSock = current($w); var_dump($selectedSock); // resource(5) of type (stream) ... says it's writable, BUT IT CAN'T POSSIBLY BE WRITABLE } $r = [$socket]; $w = $e = []; if (stream_select($r, $w, $e, 0, 0)) { $selectedSock = current($r); var_dump($selectedSock); // resource(5) of type (stream) ... says it's readable, let's check for EOF } usleep(50000); // just in case $isFeof = feof($selectedSock); var_dump($isFeof); // bool(false) ... not EOF ... WTF? var_dump(fread($selectedSock, 8192)); // string(0) "" var_dump(feof($selectedSock)); // bool(TRUE) -- Now we know the truth! Expected result: ---------------- feof($socket) on an asynchronously connected socket stream should return TRUE if the connection attempt failed once the socket becomes "writable." Actual result: -------------- It's not possible to determine if the connection attempt failed without first attempting to read/write to/from the socket stream. -- Edit bug report at https://bugs.php.net/bug.php?id=64803&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=64803&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=64803&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=64803&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=64803&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=64803&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=64803&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=64803&r=needscript Try newer version: https://bugs.php.net/fix.php?id=64803&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=64803&r=support Expected behavior: https://bugs.php.net/fix.php?id=64803&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=64803&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=64803&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=64803&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64803&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=64803&r=dst IIS Stability: https://bugs.php.net/fix.php?id=64803&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=64803&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=64803&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=64803&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=64803&r=mysqlcfg