php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46636 feof() blocking on non-blocking socket
Submitted: 2008-11-21 02:56 UTC Modified: 2008-12-02 01:00 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: aragon at phat dot za dot net Assigned: dsp (profile)
Status: No Feedback Package: Streams related
PHP Version: 5.2.7RC4 OS: FreeBSD 7.0-STABLE
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: aragon at phat dot za dot net
New email:
PHP Version: OS:

 

 [2008-11-21 02:56 UTC] aragon at phat dot za dot net
Description:
------------
There was a change since 5.2.6 release that is causing feof() to block when testing a non-blocking socket for EOF.  It happens if the socket has no data waiting in its buffer and is open.

If I compare 5.2.6 and 5.2.7 code it looks like main/streams/streams.c:642:

        if (!stream->eof && PHP_STREAM_OPTION_RETURN_ERR ==
                        php_stream_set_option(stream, PHP_STREAM_OPTION_CHECK_LIVENESS,
                        -1, NULL)) {
                stream->eof = 1;
        }

In 5.2.6 php_stream_set_option is called with a value of 0, not -1.

Reproduce code:
---------------
<?
$errno = 0; $errstr = '';
$socket = stream_socket_client('tcp://127.0.0.1:80', $errno, $errstr, 5, STREAM_CLIENT_CONNECT|STREAM_CLIENT_ASYNC_CONNECT);
if (stream_set_blocking($socket, 0) === false) die('blah');
$state = $i = 1;
while (1) {
	$read = $write = $except = null;
	switch ($state) {
		case 1: $write = array($socket); break;
		case 2: $read = array($socket); break;
	}
	echo $i.':'.time().chr(10);
	if (stream_select($read, $write, $except, 0, 500000) > 0) {
		echo $i.':'.time().chr(10);
		if (feof($socket)) break;
		echo $i++.':'.time().chr(10);
		switch ($state) {
		case 1:
			fwrite($socket, 'GET /blog HTTP/1.0' . chr(13).chr(10).chr(13).chr(10));
			$state = 2; break;
		case 2:
			if (fread($socket, 8192)) echo 'ooo'.chr(10); break;
		}
	}
}
echo time().chr(10);
?>

Expected result:
----------------
1:0
1:0
1:0
2:0
2:0
2:0
ooo
3:0
3:0
0


Actual result:
--------------
1:0
1:0
1:5
2:5
2:5
2:5
ooo
3:5
3:5
5


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-21 02:59 UTC] aragon at phat dot za dot net
Sorry, I pasted misleading reproduce code.  It should be:

<?
$errno = 0; $errstr = '';
$socket = stream_socket_client('tcp://127.0.0.1:80', $errno, $errstr, 5, STREAM_CLIENT_CONNECT|STREAM_CLIENT_ASYNC_CONNECT);
if (stream_set_blocking($socket, 0) === false) die('blah');
$state = $i = 1;
$start = time();
while (1) {
	$read = $write = $except = null;
	switch ($state) {
		case 1: $write = array($socket); break;
		case 2: $read = array($socket); break;
	}
	echo $i.':'.(time()-$start).chr(10);
	if (stream_select($read, $write, $except, 0, 500000) > 0) {
		echo $i.':'.(time()-$start).chr(10);
		if (feof($socket)) break;
		echo $i++.':'.(time()-$start).chr(10);
		switch ($state) {
		case 1:
			fwrite($socket, 'GET /blog HTTP/1.0' . chr(13).chr(10).chr(13).chr(10));
			$state = 2; break;
		case 2:
			if (fread($socket, 8192)) echo 'ooo'.chr(10); break;
		}
	}
}
echo (time()-$start).chr(10);
?>
 [2008-11-21 08:30 UTC] pajoye@php.net
Arnaud, can you take a look please?

http://tinyurl.com/6g42xf
 [2008-11-21 13:31 UTC] lbarnaud@php.net
I will, but it seems related to this one: http://bugs.php.net/bug.php?id=46049
 [2008-11-22 10:39 UTC] johannes@php.net
David, please check when checking #46049
 [2008-11-24 15:40 UTC] dsp@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2008-12-02 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 11:01:33 2024 UTC