php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26863 fgets hangs on some urls
Submitted: 2004-01-10 16:56 UTC Modified: 2004-03-15 08:39 UTC
Votes:5
Avg. Score:2.6 ± 1.4
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: jim at bluedojo dot com Assigned: wez (profile)
Status: No Feedback Package: *Directory/Filesystem functions
PHP Version: 4CVS, 5CVS (2004-01-10) OS: *
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: jim at bluedojo dot com
New email:
PHP Version: OS:

 

 [2004-01-10 16:56 UTC] jim at bluedojo dot com
Description:
------------
fgets() hangs infintely on some urls.

This url:  http://www.nwf.org/productions/whales.html

will not do anything and should return false when I use stream_set_timeout() but it doesn't time out.

I tried fgets($fd, 1024) and fgets($fd, 4096) but that doesn't work either.

Reproduce code:
---------------
$url = http://www.nwf.org/productions/whales.html;

if ($fd = @fopen($url,'rb')){
   stream_set_timeout($fd, 6);
   $html = '';
   while (!feof($fd)) {
      $html .= trim(fgets($fd));
   }
   
   fclose($fd);
}

Expected result:
----------------
The code should store the html code of "$url" into "$html."  But it hangs on some urls when it should return false due to stream_set_timeout();

Actual result:
--------------
fgets() Hangs infinitely.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-10 17:27 UTC] jim at bluedojo dot com
I used the latest snapshot.  fgets() still hangs on that specific url and stream_set_timeout does not time out.  It should return false but doesn't.
 [2004-01-10 17:35 UTC] wez@php.net
I suspect the problem to be with feof() rather than fgets().

try this:

do {
   $line = fgets($fp);
   if ($line === false)
      break;
   $html .= trim($line);
} while(true);

 [2004-01-10 18:06 UTC] jim at bluedojo dot com
The url is now working now so I cannot use it as a test case (it needs to return false).  I will see if I can find a new url to test it with the new code.
 [2004-01-10 19:48 UTC] wez@php.net
So the URL needs to return a failure code in order
to trigger the problem?
Please as specific as you can about it to help us figure
out whats happening.
 [2004-01-12 22:08 UTC] jim at bluedojo dot com
I think removing feof() solved the problem.  I don't get any infinite stalls anymore.  Thanks.
 [2004-01-14 18:04 UTC] jim at bluedojo dot com
I will try to answer the question the best I can.

I have written a spider in php that can index millions of pages.  Every once in a while it will encounter a page that will not load up (which I thought was due to fgets).  When I type this url in the location bar of a browser, the page seems like it will load forever and nothing will show up.

When I set stream_set_timeout($fd, 6) then once would expect that $fd will time out in 6 seconds and exit the loop.  I believe that feof would detect that the stream would time out:


if ($fd = @fopen($url,'rb')){
   stream_set_timeout($fd, 6);
   $html = '';
   while (!feof($fd)) {
      $html .= trim(fgets($fd));
   }
   fclose($fd);
}

To answer wez's question, I had to find a url that didn't work (that took forever to load) in order to test that feof would exit due to the timing out of the stream ($fd).

The url that I was using wasn't working for about one day.  Then it started to load normally in a browser a day later so I couldn't use it anymore as a test case for this problem.

Making the stream time out is important for my application because it needs to move on to the next url or else it will loop forever.

Hope that helps.
 [2004-02-21 17:57 UTC] scottmacvicar at ntlworld dot com
If you have a server which is under extreme load and is taking around 30 seconds to respond, results similar to a DoS attack. It appears that the stream doesn't have a timeout so it simply finishes when max_execution_time is reached.
 [2004-03-15 08:39 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 [2008-12-22 01:38 UTC] chtmpl at sibmail dot com
Guys, looks like server just tries to keep connection alive. To avoid this put "Connection: close" onto your http header.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Sep 16 19:03:38 2021 UTC