#40517 [Opn]: async connections do not obey connect timeout

From: Date: Tue, 20 Feb 2007 16:01:24 +0000
Subject: #40517 [Opn]: async connections do not obey connect timeout
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969375540@lists.php.net to get a copy of this message
ID: 40517 Updated by: wez@php.net Reported By: djgrrr+phpbugs at p2p-network dot net Status: Open -Bug Type: Streams related +Bug Type: Documentation problem Operating System: Linux 2.6 PHP Version: 5.2.1 New Comment: The timeout parameter for stream_socket_client() only applies if you are not making an async connection attempt. When you ask for an async connection attempt, the function returns "immediately" and so cannot possibly wait for the connection timeout interval. The correct way to detect whether an async connection has completed is to use stream_select() and wait for the socket to become readable. If reading from that socket gives you an error, then the connection attempt failed. Also note that you really should not be using stream_get_meta_data() to determine anything about the status of a network connection unless you understand exactly how the PHP streams internals function. The manual states this very clearly--your code is broken if you use it for any kind of logic in your script. Making this into a documentation problem. Previous Comments: ------------------------------------------------------------------------ [2007-02-19 13:18:34] djgrrr+phpbugs at p2p-network dot net <?php $host = 'tcp://72.232.216.58:56004'; $sock = stream_socket_client($host, $errno, $err, 5, (STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT)); stream_set_blocking($sock, false); while (isset($sock)) { $r = array($sock); $w = $e = NULL; stream_select($r, $w, $e, 1, 0); if (count($r) > 0) { echo "Ready to read:\n"; foreach($r as $rr) { $md = stream_get_meta_data($rr); var_dump($md, feof($rr), fgets($rr)); fclose($rr); unset($sock); echo $host."\n"; } } } ?> The above code should cause the stream_select to return a readable connection after 5 seconds (in reality it takes around 3 minutes), which when read from will return false, indicating its a failed connection attempt. It would be nice if eof was set on the stream, because the stream technically is at its end (although i'm not sure if that would affect the use of stream_select or not). ------------------------------------------------------------------------ [2007-02-19 09:46:08] tony2001@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. ------------------------------------------------------------------------ [2007-02-17 16:05:43] djgrrr+phpbugs at p2p-network dot net Description: ------------ when using stream_socket_client() with the flags (STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT), the async connection does not obey the timeout parameter. Also, when the connection does time out (after around 3 minutes) it does not set the eof flag; although it does return false if you try to read from the connection, it would also make a lot of sense if the eof flag was also set on the resource, so calls to feof() return true. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=40517&edit=1

« previous php.doc (#969375540) next »