#46636 [Asn->Fbk]: feof() blocking on non-blocking socket

From: Date: Mon, 24 Nov 2008 15:40:03 +0000
Subject: #46636 [Asn->Fbk]: feof() blocking on non-blocking socket
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-131264@lists.php.net to get a copy of this message
 ID:               46636
 Updated by:       dsp@php.net
 Reported By:      aragon at phat dot za dot net
-Status:           Assigned
+Status:           Feedback
 Bug Type:         Streams related
 Operating System: FreeBSD 7.0-STABLE
 PHP Version:      5.2.7RC4
 Assigned To:      dsp
 New Comment:

Please try using this CVS snapshot:

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

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




Previous Comments:
------------------------------------------------------------------------

[2008-11-22 10:39:18] johannes@php.net

David, please check when checking #46049

------------------------------------------------------------------------

[2008-11-21 13:31:18] lbarnaud@php.net

I will, but it seems related to this one:
http://bugs.php.net/bug.php?id=46049

------------------------------------------------------------------------

[2008-11-21 08:30:29] pajoye@php.net

Arnaud, can you take a look please?

http://tinyurl.com/6g42xf

------------------------------------------------------------------------

[2008-11-21 02:59:04] 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 02:56:54] 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



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=46636&edit=1



Thread (6 messages)

« previous php.bugs (#131264) next »