|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46049 feof() hangs
Submitted: 2008-09-11 12:13 UTC Modified: 2008-11-24 15:39 UTC
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: Assigned: dsp (profile)
Status: Closed Package: Streams related
PHP Version: 5.3CVS-2008-09-11 (CVS) OS: Linux
Private report: No CVE-ID: None
 [2008-09-11 12:13 UTC]
The code below works fine with PHP 5.2.6 (and earlier), but not with the unreleased PHP 5.2.7 and PHP 5.3.0:

    892 $handle = @fopen($url, 'r');
    894 if (!$handle) {
    895     throw new RuntimeException(
    896       'Could not connect to the Selenium RC server.'
    897     );
    898 }
    900 stream_set_blocking($handle, 1);
    901 stream_set_timeout($handle, 0, $this->timeout);
    903 $info     = stream_get_meta_data($handle);
    904 $response = '';
    906 while ((!feof($handle)) && (!$info['timed_out'])) {
    907     $response .= fgets($handle, 4096);
    908     $info = stream_get_meta_data($handle);
    909 }
    911 fclose($handle);

The code above hangs with PHP 5.2.7 and PHP 5.3.0 in line 906 as shown using Xdebug's tracing:

    fopen() Driver.php:892
    stream_set_blocking() Driver.php:900
    stream_set_timeout() Driver.php:901
    stream_get_meta_data() Driver.php:903
    feof() Driver.php:906
    fgets() Driver.php:907
    stream_get_meta_data() Driver.php:908
    feof() Driver.php:906

The second feof() call above hangs.

More information can be found here:


Changing the loop to

    while (!$info['eof'] && !$info['timed_out']) {
        $response .= fgets($handle, 4096);
        $info = stream_get_meta_data($handle);

fixes the problem. This means a difference between feof() and $info['eof'].


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-11 12:17 UTC]
David, it appears that this problem is caused by this patch of yours:
Take a look at it please.
 [2008-10-28 22:08 UTC]
David, I guess we just have to revert that bad patch of yours then?
 [2008-11-05 13:43 UTC]
Jani: I think it's an issue with the ssl socks, as my patch didn't patch 
those. I try to have time to have a look on this, but I 
cannot reproduce the patch yet even though I sit down with sebstian and 
try to figure out what's going wrong.
 [2008-11-18 23:46 UTC]
Please try using this CVS snapshot:
For Windows:

 [2008-11-21 17:46 UTC]
Shorter reproduce code:

$socket = stream_socket_client('tcp://');

The feof() will block until timeout is reached.

David, your fix correctly made feof() behave as documented (blocks until timeout is reached if no data is available), but should feof() really blocks here ? It seems it never behaved as documented (checked php4.4, 5.1, 5.2.6).
 [2008-11-24 02:19 UTC]
If everybody is fine with that, I will revert the patch.
 [2008-11-24 15:39 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

Patch was reverted
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Sep 29 11:01:24 2023 UTC