ID: 51056
Updated by: magicaltux@php.net
Reported By: magicaltux@php.net
Status: Feedback
Bug Type: Streams related
Operating System: Linux Gentoo 2.6.32
PHP Version: 5.3.1
New Comment:
Confirmed with PHP_5_3
Testing PHP version: 5.3.3-dev
fread took 0.07ms to read 8 bytes
fread took 5.06ms to read 256 bytes
fread took 1000.10ms to read 53 bytes
fread took 5.03ms to read 256 bytes
fread took 1000.11ms to read 53 bytes
fread took 5.04ms to read 256 bytes
fread took 1000.10ms to read 53 bytes
I'll need a bit more time for PHP_5_2 as flex-2.5.4 is becoming more
difficult to find.
Previous Comments:
------------------------------------------------------------------------
[2010-02-16 11:11:54] magicaltux@php.net
This report looks a bit like bug #50856 (about non-blocking mode), but
it seems to be related to a different part of the streams api (non
blocking mode, fopen wrapper for http, while I'm testing on sockets in
blocking mode).
I'm about to test with vanilla PHP_5_2 and PHP_5_3 from svn (once
compilation completes). In the meantime I could reproduce the problem
on PHP 5.2.12 (gentoo patched version).
------------------------------------------------------------------------
[2010-02-16 10:56:46] jani@php.net
Isn't this same (or related) as bug #50856 is? Does it happen with
PHP_5_2 ? And I'd guess you have tested latest PHP_5_3 as well?
------------------------------------------------------------------------
[2010-02-16 10:34:48] magicaltux@php.net
Description:
------------
On a blocking stream, a call to fread() will return even if the passed
buffer size has not been reached.
A call to fread() should return immediatly if there is data pending to
be read (buffered by php). Instead of that, php will call poll() on
the stream to wait for more data to arrive, then will return the
previously read data and the new data.
Suggestion: if fread() is called on a blocking stream that already
contains data, PHP should call poll() with a 0 timeout, read any newly
available data and return immediatly.
If no data is currently in PHP's internal buffer, current behaviour
can be kept.
(it is also possible to skip completly the poll() part and directly
return any pending data without checking if the real stream has
anything, but I believe that it might not be as logical, a call to
fread() should read)
Reproduce code:
---------------
<?php
echo 'Testing PHP version: '.phpversion()."\n";
$pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM,
STREAM_IPPROTO_IP);
$pid = pcntl_fork();
if ($pid == -1) die("Failed to fork\n");
if ($pid > 0) {
// parent
fclose($pair[0]);
while(!feof($pair[1])) {
$start = microtime(true);
$data = fread($pair[1], 256);
printf("fread took %01.2fms to read %d bytes\n",
(microtime(true)-$start)*1000, strlen($data));
}
exit;
}
// child
fclose($pair[1]);
while(!feof($pair[0])) {
fwrite($pair[0], "Hello 1\n"); // 8 bytes
usleep(5000);
fwrite($pair[0], str_repeat('a', 300)."\n"); // 301 bytes
sleep(1);
}
Expected result:
----------------
Testing PHP version: 5.3.1
fread took 0.09ms to read 8 bytes
fread took 5.08ms to read 256 bytes
fread took 0.00ms to read 45 bytes
fread took 1000.10ms to read 8 bytes
fread took 5.04ms to read 256 bytes
fread took 0.00ms to read 45 bytes
fread took 1000.10ms to read 8 bytes
fread took 5.04ms to read 256 bytes
(etc)
Actual result:
--------------
Testing PHP version: 5.3.1
fread took 0.09ms to read 8 bytes
fread took 5.08ms to read 256 bytes
fread took 1000.10ms to read 53 bytes
fread took 5.04ms to read 256 bytes
fread took 1000.10ms to read 53 bytes
fread took 5.04ms to read 256 bytes
(etc)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=51056&edit=1