php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62394 socket_read non-blocking resets timeout with no data read
Submitted: 2012-06-22 10:31 UTC Modified: -
Votes:14
Avg. Score:3.6 ± 0.8
Reproduced:5 of 9 (55.6%)
Same Version:0 (0.0%)
Same OS:2 (40.0%)
From: sysfly at gmail dot com Assigned:
Status: Open Package: Sockets related
PHP Version: 5.3.14 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sysfly at gmail dot com
New email:
PHP Version: OS:

 

 [2012-06-22 10:31 UTC] sysfly at gmail dot com
Description:
------------
If you call socket_read and it throws an error such as socket error 10035 then the 
connection will never timeout even if the remote side disconnects. Socket error 
10035 is thrown if data is not ready to be read (hasn't been sent) yet and is not 
really indicative of a disconnect or other fatal error.

Maybe there's a distinction to be made between fatal and nonfatal socket errors. 
Possibly have it return an empty string instead of throwing a nonfatal error? 
There is no data to read so that seems to be the more appropriate course of 
action.

Test script:
---------------
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connected = socket_connect($socket, '127.0.0.1', 1337); // some server that never sends you data. connection must be accepted though, or else this throws an error.
socket_set_nonblock($socket);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1, 'usec' => 0)); // set timeout to something fast, a second.

while (($read = @socket_read($socket, 1)) === false)
{
	// socket_read throwing an error doesn't mean the connection has failed and the server is gone
	// in context, it just means the server hasn't sent any data yet. [in testing, it never will.]
	if (socket_last_error($socket) != 10035)
		break; // an error that probably means we disconnected has been thrown!
}

echo('yay we did it! '.socket_last_error($socket));

Expected result:
----------------
yay we did it! (some FATAL error code)

Actual result:
--------------
infinite nothingness unless set_time_limit is set, in which case the typical time 
limit exceeded error interrupts the script.

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Nov 17 10:01:32 2019 UTC