php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27518 fseek() causes PHP to hang on fwrite()
Submitted: 2004-03-07 12:19 UTC Modified: 2004-03-07 16:39 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: pavcio at 4lo dot bytom dot pl Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5CVS-2004-03-07 (dev) OS: GNU/Linux 2.6.3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: pavcio at 4lo dot bytom dot pl
New email:
PHP Version: OS:

 

 [2004-03-07 12:19 UTC] pavcio at 4lo dot bytom dot pl
Description:
------------
I`ve noticed that fwrite() function hangs when it copies data from a file to a pipe and fseek() has been called before it. See the code for details.

My system:
  - GNU/Linux Slackware-current on 2.6.3 kernel
  - GCC 3.3.2
  - glibc 2.3.2
  - PHP versions tested: 5.0b4 and php5-200403071430


Reproduce code:
---------------
#!/usr/local/bin/php
<?php
$mfp = fopen(tempnam('/tmp', 'test'), 'w+');
for($i=0; $i < 1024; $i++) $kilobyte .= "A";
for($i=0; $i < 100; $i++) fwrite($mfp, $kilobyte);

$process = proc_open('/bin/cat', array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes);
fseek($mfp, 0);
while( TRUE ) {
    $data = fread($mfp, 65536);
    if( strlen($data) == 0 ) break;
    fwrite($pipes[0], $data);
}

proc_close($process);
?>


Expected result:
----------------
Try to guess ;-).

Actual result:
--------------
Here is the end of strace dump:

(...cut...)
pipe([7, 8])                            = 0
pipe([9, 10])                           = 0
pipe([11, 12])                          = 0
fork()                                  = 16951
close(7)                                = 0
(,,,cut...)
lseek(3, 0, SEEK_SET)                   = 0
(...cut...)
write(8, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"..., 8192) = 8192
write(8, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"..., 8192) = 8192
write(8, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"..., 8192) = 8192
write(8, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"..., 8192 <unfinished ...>

(here PHP hangs)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-07 16:39 UTC] wez@php.net
You are filling up the pipes and never consuming the data from its output.
Not a bug in php at all, but your script.
Use a smaller size chunk when writing to the pipe, and
read the data from your output pipe.

Also, *PLEASE* fill in the bug report correctly; if you'd
filled in the expected output in the first instance I could
have diagnosed your broken code just by reading it.

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 15:01:33 2025 UTC