php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48683 stream_select() returns 1 even when no streams have changed
Submitted: 2009-06-25 04:15 UTC Modified: 2009-07-24 09:32 UTC
From: php at richardneill dot org Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.2CVS-2009-06-25 (snap) OS: Linux
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: php at richardneill dot org
New email:
PHP Version: OS:

 

 [2009-06-25 04:15 UTC] php at richardneill dot org
Description:
------------
It seems that stream_select is failing to return 0 even if all of the streams would block.

I've tested the code below on multiple versions of PHP from 5.2.4 upward, and get the same result on them all. 

(It's possible I've misunderstood the requirements for sockets, but I've quintuple-checked this code.)

Reproduce code:
---------------
#!/usr/bin/php
<?

echo "This should never print 'did fread'.\n";
echo "It should just print 'stream_select returned 0' every second\n";
echo "\n";

$fp=fopen("/dev/null",r);  #open /dev/null for reading. 
                           #Should immediately result in EOF.

while (true) {
	$r = array($fp);
   	
   	$n = stream_select($r, $w = null, $e = null, 1);	
        #stream select on read array, timeout 1 sec

   	echo "stream_select returned $n\n";
   
   	if ($n) {
              #Try to read up to 1024 bytes
	      echo fread($fp,1024);
	      echo "did fread.\n";
   	}

   	usleep (100000);		//slow down (0.1s)
}

?>

Expected result:
----------------
I expect to see the line 
  stream_select returned 0
repeated every 1 second.



Actual result:
--------------
I get repeated instances of:
  stream_select returned 1
  did fread.

fread is returning nothing, but still the stream_select insists that there is data available to be read!


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-25 08:37 UTC] sjoerd-php at linuxonly dot nl
Thank you for your bug report.

In your code example, you use /dev/null as blocking file. However, this is not entirely correct. As you say, /dev/null gives EOF immediately, which means stream_select sees it as ready:

"The streams listed in the read  array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a stream resource is also ready on end-of-file, in which case an fread() will return a zero length string)."

In your loop, fread() constantly read a empty string and the file resource is constantly ready.

Maybe this information already solves your problem. If it does not, please provide a better code example.
 [2009-07-05 02:36 UTC] php at richardneill dot org
Thank you for your explanation. I agree - I had misunderstood the 
docs, and PHP is behaving correctly.
 [2009-07-24 09:32 UTC] jani@php.net
And you still left this open.. bogus since there is no bug here.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 05:01:28 2024 UTC