php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62816 stream_socket_accept can't block infinitely
Submitted: 2012-08-14 13:25 UTC Modified: 2012-08-14 18:55 UTC
Votes:10
Avg. Score:4.3 ± 0.9
Reproduced:9 of 9 (100.0%)
Same Version:2 (22.2%)
Same OS:3 (33.3%)
From: xrstf-misc at yahoo dot com Assigned:
Status: Open Package: Sockets related
PHP Version: 5.4.5 OS: Linux (ARM)
Private report: No CVE-ID: None
 [2012-08-14 13:25 UTC] xrstf-misc at yahoo dot com
Description:
------------
I'm opening a socket on 0.0.0.0, port 8080 (doesn't matter) and then I'm trying 
to accept incoming connections. For this, the stream_socket_accept() function 
should block and wait for connections.

This works on Windows and Linux (x86), but on ARM it fails. For some reason, the 
functions returns immediately, generating a warning:

  stream_socket_accept(): accept failed: Connection timed out

Setting the timeout in my testscript to 0 doesn't change anything. Setting the 
timeout to a non-infinite one like 10 seconds makes the function block correctly 
for 10 seconds.

The problem occurs in all PHP 5.4 versions. I didn't test 5.3 or earlier.

Test script:
---------------
<?php

$addr   = "tcp://0.0.0.0:8080";
$errno  = null;
$errstr = null;

print "\n";
print "  > Creating socket @ $addr...";
$socket = stream_socket_server($addr, $errno, $errstr);

if ($socket === false) {
    print " ERROR: $errstr ($errno)\n\n";
    exit(2);
}

print " OK :)\n";
print "  > Accepting incoming connections (listening)...";
$conn = stream_socket_accept($socket, -1);
print "    ERROR: should not have reached this point!\n\n";
exit(1);

Expected result:
----------------
The script should stop and wait at the stream_socket_accept() call forever.

Actual result:
--------------
The function returns false and throws a warning.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-14 14:16 UTC] xrstf-misc at yahoo dot com
Oddly enough, using the "raw" socket functions works as expected.

Test code:
----------
$socket = socket_create_listen($port); // instead of stream_socket_server
$conn   = socket_accept($socket);      // instead of stream_socket_accept

Maybe there is something wrong with the timeout handling in the stream related 
functions?
 [2012-08-14 17:10 UTC] xrstf-misc at yahoo dot com
When doing a select() call before accepting, using an infinite timeout works. 
Seems like select() correctly handles NULL as "no timeout".

So I can do (pseudocode):

if (1 === select(array($myListeningSocket), null, null, null /* <- timeout */)) {
    $conn = accept($myListeningSocket);
}
 [2012-08-14 18:55 UTC] xrstf-misc at yahoo dot com
-Summary: stream_socket_accept does not block +Summary: stream_socket_accept can't block infinitely
 [2012-08-14 18:55 UTC] xrstf-misc at yahoo dot com
(Changed bug title to better reflect the actual problem.)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC