#23258 [Asn]: fgets() hangs 15 seconds on EOF of a socket
| From: | wez@php.net | Date: | Fri, 25 Apr 2003 10:44:20 +0000 |
| Subject: | #23258 [Asn]: fgets() hangs 15 seconds on EOF of a socket | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-38361@lists.php.net to get a copy of this message | ||
ID: 23258
Updated by: wez@php.net
Reported By: markus dot pfefferle at web dot de
Status: Assigned
Bug Type: Sockets related
Operating System: Windows 2000
PHP Version: 4.3.2-RC
Assigned To: wez
New Comment:
I must verify/fix this before 4.3.2 goes gold.
Previous Comments:
------------------------------------------------------------------------
[2003-04-23 07:15:07] markus dot pfefferle at web dot de
Back from Eastern holiday.
Downloaded and installed the CVS snapshot (phpinfo() says build 2195)
and tried it again.
Same error - 15 seconds pause at the last (failing) read from a
socket:
1051099825 : string(1) ""
1051099825 : string(1) ""
1051099825 : string(1) ""
1051099825 : string(1) ""
1051099841 : string(0) ""
So ticket still open.
------------------------------------------------------------------------
[2003-04-17 09:40:23] wez@php.net
Please try using this CVS snapshot:
http://snaps.php.net/php4-STABLE-latest.tar.gz
For Windows:
http://snaps.php.net/win32/php4-win32-STABLE-latest.zip
------------------------------------------------------------------------
[2003-04-17 09:23:54] markus dot pfefferle at web dot de
Err, nevermind. I figured out why - var_dump() writes to the output
buffer before echo does.
But in the end, the 15 second-problem remains. Here with fread($fp,
1):
1050589160 : string(1) "\n"
1050589160 : string(1) "\n"
1050589160 : string(1) "\n"
1050589160 : string(1) "\n"
1050589175 : string(0) ""
It takes fread() and fgets() in Blocking mode 15 seconds to realize
that the connection has been severed.
------------------------------------------------------------------------
[2003-04-17 09:13:58] markus dot pfefferle at web dot de
The "Array" is a mistake actually - an output from another place of my
code. Yet still the mystery remains, why the loop goes through another
cycle after the last call to fgets() has returned false and feof() is
not reflecting the end of the connection. And why $r in this case does
not check out as "false" in an expression to preliminary end the loop.
------------------------------------------------------------------------
[2003-04-17 07:35:51] markus dot pfefferle at web dot de
This well-known fragment of code takes up about 15 seconds on my
machine:
<pre>
$fp = fsockopen($host, $port);
fputs($fp, "POST $path HTTP/1.1\n");
fputs($fp, "Host: $host\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: ". strlen($data_to_send) ."\n");
fputs($fp, "Connection: close\n\n");
fputs($fp, "$data_to_send\n");
while(!feof($fp)) {
$res .= fgets($fp, 128);
}
</pre>
In order to trace the cause fot this pause, I modified whie while-loop
to give me some debugging information:
<pre>
while(!feof($fp)) {
$r = fgets($fp, 128);
echo time()." : ".var_dump($r),"\n";
}
</pre>
Truns out this on the final 3 lines:
<pre>
1050581993 :
string(2) "
"
1050581993 :
bool(false)
1050582009 :
Array
</pre>
The second line that returned false should already have flipped the EOF
flag on the connection, but apparently it didn't. The last call that
finally flips this flag however takes up 15 seconds of time and returns
(odd enough) an empty array.
Alrighty I think - if the false-value of the last chunk will give me a
clue to break the loop, I just change my code to this:
<pre>
do {
$r = fgets($fp, 128);
echo time()." : ".var_dump($r),"\n";
} while ($r);
</pre>
And guess what - exactly the same 15 seconds, exactly the same output
as before. The loop didn't even exit when $r was bool(false) according
to var_dump(). This is what really puzzles me.
I also used fread and toyed around with any possible length-parameter.
It's always these 15 seconds delay at the last read that returns this
odd empty array.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=23258&edit=1