php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60817 stream_get_line() incorrectly blocks
Submitted: 2012-01-20 11:55 UTC Modified: 2012-05-15 12:33 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:3 (75.0%)
From: landeholm at gmail dot com Assigned: cataphract (profile)
Status: Closed Package: Streams related
PHP Version: 5.3.9 OS: Linux/Ubuntu
Private report: No CVE-ID: None
 [2012-01-20 11:55 UTC] landeholm at gmail dot com
Description:
------------
stream_get_line() will block even though data has been received and there are lines ready to be parsed in php's internal buffer. 

It makes it impossible for example to implement a blocking HTTP server as the server will only parse the first line in the HTTP request and then both the server and client will hang as both are waiting for more data.

Test script:
---------------
$c = pcntl_fork();
if ($c > 0) {
$socket = stream_socket_server("tcp://127.0.0.1:10000");
$socket2 = stream_socket_accept($socket);
while (!feof($socket2))
print "server got line: " . stream_get_line($socket2, 1024, "\r\n") . "\n";
print "server done\n";
} else {
sleep(1);
$socket = stream_socket_client("tcp://127.0.0.1:10000");
fwrite($socket, "test #1\r\ntest2\r\ntest3\r\ntest4\r\n");
fread($socket, 1000);
print "client done\n";
}
die(0);


Expected result:
----------------
client done
server got line: test #1
server got line: test2
server got line: test3
server got line: test4
server done

Actual result:
--------------
server got line: test #1


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-20 11:56 UTC] landeholm at gmail dot com
Oops, the expected result should be:

server got line: test #1
server got line: test2
server got line: test3
server got line: test4
server done
client done
 [2012-01-22 13:49 UTC] cataphract@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: cataphract
 [2012-01-22 20:30 UTC] cataphract@php.net
Automatic comment from SVN on behalf of cataphract
Revision: http://svn.php.net/viewvc/?view=revision&revision=322582
Log: - Further fix for bug #60455 (stream_get_line misbehaves if EOF is not detected
  together with the last read).
- Fixed bug #60817 (stream_get_line() reads from stream even when there is
  already sufficient data buffered). stream_get_line() now behaves more like
  fgets(), as is documented.
#withheld commit to 5.4
 [2012-01-22 20:57 UTC] cataphract@php.net
This bug has been fixed in SVN.

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/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Fixed, now the function doesn't try to fill the buffer with $length bytes; it 
behaves more like fgets() typically returning false in the last iteration (if the 
file ends with the delimiter).
 [2012-01-22 20:57 UTC] cataphract@php.net
-Status: Assigned +Status: Closed
 [2012-03-05 01:56 UTC] stas@php.net
-Status: Closed +Status: Re-Opened
 [2012-03-05 01:56 UTC] stas@php.net
Re-opened to track 5.4. Please merge to 5.4 and close.
 [2012-03-06 14:47 UTC] john dot papaioannou at gmail dot com
I am still seeing this issue using the code given by landeholm at gmail dot com 
(broken down into two scripts because I 'm on Windows). Specifically, referring 
to this line:

print "server got line: " . stream_get_line($socket2, 1024, "\r\n") . "\n";

* with a buffer size of 1024, the read times out
* with buffer sizes smaller or equal to the lines actually being written from 
the client (e.g. tried 5 and 8 bytes) the read works


Windows 7 x64, PHP 5.3.10 x86 on Apache 2.2.21
 [2012-04-18 09:46 UTC] laruence@php.net
Automatic comment on behalf of cataphract
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f5a457318217a66fddc0d47af04a00f144dcbcec
Log: - Further fix for bug #60455 (stream_get_line misbehaves if EOF is not detected   together with the last read). - Fixed bug #60817 (stream_get_line() reads from stream even when there is   already sufficient data buffered). stream_get_line() now behaves more like   fgets(), as is documented. #withheld commit to 5.4
 [2012-05-15 12:23 UTC] olouvignes at gmail dot com
Just migrated from 11.10 w/ php5.3.6-13 to 12.04 w/ php5.3.10-1 and this new php 
version broken a beanstalkd socket. It does not stop on "\r\n" anymore and hangs 
till timeout, while it works correctly on 5.3.6-13.
 [2012-05-15 12:30 UTC] cataphract@php.net
-Status: Re-Opened +Status: Closed
 [2012-05-15 12:33 UTC] cataphract@php.net
This fix is only on 5.3.11, so it's normal you're experiencing the problem in 5.3.10.
 [2012-07-24 23:37 UTC] rasmus@php.net
Automatic comment on behalf of cataphract
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f5a457318217a66fddc0d47af04a00f144dcbcec
Log: - Further fix for bug #60455 (stream_get_line misbehaves if EOF is not detected   together with the last read). - Fixed bug #60817 (stream_get_line() reads from stream even when there is   already sufficient data buffered). stream_get_line() now behaves more like   fgets(), as is documented. #withheld commit to 5.4
 [2013-11-17 09:34 UTC] laruence@php.net
Automatic comment on behalf of cataphract
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f5a457318217a66fddc0d47af04a00f144dcbcec
Log: - Further fix for bug #60455 (stream_get_line misbehaves if EOF is not detected   together with the last read). - Fixed bug #60817 (stream_get_line() reads from stream even when there is   already sufficient data buffered). stream_get_line() now behaves more like   fgets(), as is documented. #withheld commit to 5.4
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 06:01:30 2024 UTC