php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #40517 async connections do not obey connect timeout
Submitted: 2007-02-17 16:05 UTC Modified: 2007-02-24 21:33 UTC
From: djgrrr+phpbugs at p2p-network dot net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.1 OS: Linux 2.6
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: djgrrr+phpbugs at p2p-network dot net
New email:
PHP Version: OS:

 

 [2007-02-17 16:05 UTC] 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. 


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-19 09:46 UTC] 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-19 13:18 UTC] 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-20 16:01 UTC] wez@php.net
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.


 [2007-02-20 17:32 UTC] djgrrr+phpbugs at p2p-network dot net
you completely missed my point..
stream_select() should return the failed aysnc connections as readable connections after the timeout parameter has elapsed. I know for a fact that this can be done, and its not hard to implement either.

Also, i use stream_get_meta_data() because feof() has a bug where it locks up randomly for no reason, where as stream_get_meta_data() does not. Don't try to tell me my code is flawed when i know its not.
 [2007-02-24 21:33 UTC] dmytton@php.net
A note has been added to the documentation about the timeout parameter.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 09 22:01:33 2025 UTC