php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19944 fgets() returns garbage when STDIN set to non-blocking
Submitted: 2002-10-16 23:32 UTC Modified: 2002-10-22 11:45 UTC
From: ces at vaultbbs dot com Assigned:
Status: Closed Package: Sockets related
PHP Version: 4.3.0-pre1 OS: RedHat 7.3
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: ces at vaultbbs dot com
New email:
PHP Version: OS:

 

 [2002-10-16 23:32 UTC] ces at vaultbbs dot com
I am trying to use the CLI version of 4.3.0-pre1.  My script needs to act if there hasn't been input within a certain amount of time.  I've used the following function to accomplish this with TCP/IPsockets in non-blocking mode:

function GetSocketLine($socket, $timeout)
{
 $timeout += time();
 $holdString = "";
 while(time() < $timeout)
  {
  $string = fgets($socket, 1024);
  if($string != false)
    break;
  }
 return($string);
}

However, if I use set_stream_blocking() to set STDIN to non-blocking, the above code returns garbage instead of "false" when there is nothing to receive.  It would seem to me that if STDIN is set to non-blocking and STDIN is passed to the above function as $socket, it ought to return "false" when blocking would occur--it shouldn't return garbage.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-10-17 02:15 UTC] wez@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

Use a snapshot from 
http://snaps.php.net/php4-latest.tar.bz2
fgets had some real nasty problems.
 [2002-10-17 12:52 UTC] ces at vaultbbs dot com
I just tried with the latest from snaps.php.net.  Problem still exists as of php4-200210170600.
 [2002-10-17 17:07 UTC] sniper@php.net
Try this one again: http://snaps.php.net/php4-latest.tar.bz2
You might have got the snapshot which didn't have the fixes.


 [2002-10-17 21:59 UTC] wez@php.net
As an aside:
you can use stream_select() to acheive this (see docs for socket_select; they work the same way, but on different things).
Also, you should use if ($string !== false) to 100% correct in your script.
 [2002-10-18 20:11 UTC] ces at vaultbbs dot com
As of snap-200210181500 it still doesn't work.  Results are the same with garbage returned when using fgets() on STDIN when in non-blocking mode.

Last two snaps I've tried (10/17 and 200210181500) have the added feature of also core dumping when fgets() is used on a non-blocking socket opened with fsockopen().  Previous versions of PHP work as expected with fgets() on a non-blocking socket, but the snaps seem to core dump.  10/17 snap would core dump on first call to fgets() whereas today's snap seems to go through the while loop three times and then core dumps when it hits fgets() again.

Thus, the current observation is that when using fgets in non-blocking mode:

1. STDIN returns garbage.
2. Sockets core dump.
 [2002-10-18 20:11 UTC] ces at vaultbbs dot com
Rats, just changing status to "Open"--already added comment above...
 [2002-10-19 04:27 UTC] wez@php.net
Can you provide a backtrace for the core-dump?

 [2002-10-19 05:35 UTC] wez@php.net
I've just committed a fix for the STDIN problem;
please try the next snapshot (dated/timestamped after
this message).
Please also test with sockets.

 [2002-10-22 11:45 UTC] ces at vaultbbs dot com
Latest snap seems to work great.  Non-blocking sockets and STDIN both seem to work as expected, no garbage, no crashing.

As far as I can tell this issue is closed at this time.  Thanks!
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Jul 06 01:05:44 2022 UTC