php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69963 fgets reads EOF if there is nothing on the PIPE to read for a short period
Submitted: 2015-06-29 20:21 UTC Modified: 2015-07-07 13:40 UTC
From: poonam dot chawla at mathworks dot com Assigned: ab (profile)
Status: Closed Package: Streams related
PHP Version: 5.6.10 OS: Windows
Private report: No CVE-ID: None
 [2015-06-29 20:21 UTC] poonam dot chawla at mathworks dot com
Description:
------------
I am using PHP to read from STDIN and write to a log file. The PHP process is spawned by another process (for the example sake, I am attaching a python script that spawns a new PHP process). 

I observed that if there is nothing to read on the STDIN for a short period of time, PHP behaves unexpectedly (reads EOF and exits).It was interesting to find out that PHP 5.4 exhibhits the correct behavior, however both PHP 5.5 and 5.6 behave unexpectedly.


Test script:
---------------
PHP:

<?php
$in = fopen("php://stdin", "rb");
$out = fopen('C:/Users/pchawla/php_test/log_file.txt', 'w');

while ( ! feof($in) ) {
    $line = fgets($in);
    fwrite($out, $line);
}
?>

import subprocess
import sys

Python:

import subprocess
import sys

def log():
    proc = subprocess.Popen(['php', 'C:/Users/pchawla/php_test/log_handler.php'], stdin=subprocess.PIPE)
    while True:
        line = sys.stdin.readline();
        proc.stdin.write(line)

if __name__ == "__main__":
    log()

Expected result:
----------------
fgets() should return FALSE only when there is an error or it reads EOF from the PIPE.


Actual result:
--------------
fgets() returns FALSE when there is nothing to read from the PIPE for a short period of time (~20 seconds).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-29 20:57 UTC] cmb@php.net
If that is a Windows only issue, it might be somehow related to
bug #69900.
 [2015-06-29 21:17 UTC] poonam dot chawla at mathworks dot com
Hi,

Thanks for an early response. It is a WINDOWS ONLY issue but unfortunately not the same one.

69900 : The issue/bug is that for every fgets call process hangs for 100ms.
However, in the issue I am experiencing there is no delay/hang on fgets.

Thanks,
Poonam
 [2015-06-30 09:25 UTC] ab@php.net
-Assigned To: +Assigned To: ab
 [2015-06-30 11:30 UTC] ab@php.net
@poonam, you obviously meant feof, not fgets?

Thanks.
 [2015-06-30 13:27 UTC] poonam dot chawla at mathworks dot com
My apologies. That is correct.
 [2015-07-01 14:58 UTC] ab@php.net
-Status: Assigned +Status: Feedback
 [2015-07-01 14:58 UTC] ab@php.net
Hi,

could you please check the build http://windows.php.net/downloads/snaps/ostc/69900/ which is linked in this mail http://news.php.net/php.internals/86976 ? Please be aware that you'll need add a stream context. The patch is unlikely to appear in PHP5, but in PHP7.

Though be aware that forcing blocking reads on pipes, you're putting yourself under the risk to experience dead locks, see #64438. When used carefully, it still can work and be useful.

Thanks.
 [2015-07-02 20:19 UTC] ab@php.net
@poonam, this patch has landed in master, if you haven't tested yet, please take the latest master snapshot http://windows.php.net/downloads/snaps/master/

Thanks.
 [2015-07-02 20:21 UTC] poonam dot chawla at mathworks dot com
-Status: Feedback +Status: Assigned
 [2015-07-02 20:21 UTC] poonam dot chawla at mathworks dot com
Thank you so much. I will try the patch and update this bug report.

Poonam
 [2015-07-07 13:40 UTC] ab@php.net
-Status: Assigned +Status: Closed
 [2015-07-07 13:40 UTC] ab@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

fixed in 7.0.0beta1
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 14:01:28 2024 UTC