php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66232 proc_open: Inconsistent handling of EOFs on pipes
Submitted: 2013-12-04 05:14 UTC Modified: 2022-12-27 17:35 UTC
From: php-bugs at jbeekman dot nl Assigned: bukka (profile)
Status: Analyzed Package: Streams related
PHP Version: 5.5Git-2013-12-04 (Git) OS: Linux 3.11.4-vhost-blk+ x86_64
Private report: No CVE-ID: None
 [2013-12-04 05:14 UTC] php-bugs at jbeekman dot nl
Description:
------------
When a program executed with proc_open closes an output pipe, PHP sometimes incorrectly reports that the stream has not reached EOF and at other times it's stuck in a blocking read.

Attached are 5 test programs and a test script:

* The closewait program is a test program that writes a specified number of bytes to STDOUT, closes STDOUT, and sleeps for a specified time.

* The fifotest* programs are used to test the operation of named pipes (FIFOs). The C and PHP version are very similar. just opening a specified file and reading from it in 64-byte chunks until error/EOF.

* The pipetest* programs are used to test the operation of unnamed pipes (pipes). The C version uses pipe() to create the pipe and fork() to give one end to a child process. The PHP version uses proc_open() to do the same. Both programs use the same logic as the fifotest* programs to read from the pipe.

* The test.sh script compiles the C programs and runs the C and PHP programs with different parameters.

From the tests, you can see that the FIFOs work the same in both C and PHP, but that the pipe has some erratic behavior. In one case it exits after reading all the data from the pipe, but does not indicate EOF. In two other cases it blocks after reading all the data from the pipe until the child process exits.

From `man 7 pipe': "[I/O on pipes and FIFOs] The only difference between pipes and FIFOs is the manner in which they are created and opened. Once these tasks have been accomplished, I/O on pipes and FIFOs has exactly the same semantics."

There is no posix_pipe() (see bug #60896), so I can't test whether there is an issue with pipes in general or just with proc_open().

Test script:
---------------
git commit 00a7b1ff (branch PHP-5.5 as of 2013-12-3 16:36:07 PST)
Configure Command: './configure'  '--disable-all' '--disable-cgi' '--enable-cli'

Download all of the following and save them under the specified name in the same folder and then: chmod ugo+x test.sh; ./test.sh

** unable to post links because of bugs.php.net spam detection **

Expected result:
----------------
===>FIFO test (C)
n: 63 EOF: 1
n: 64 n: 0 EOF: 1
n: 64 n: 1 EOF: 1
===>FIFO test (PHP)
n: 63 EOF: 1
n: 64 n: 0 EOF: 1
n: 64 n: 1 EOF: 1
===>pipe test (C)
n: 63 EOF: 1
n: 64 n: 0 EOF: 1
n: 64 n: 1 EOF: 1
===>pipe test (PHP)
n: 63 EOF: 1
n: 64 n: 0 EOF: 1
n: 64 n: 1 EOF: 1

real less than 1
user irrelevant
sys irrelevant


Actual result:
--------------
===>FIFO test (C)
n: 63 EOF: 1
n: 64 n: 0 EOF: 1
n: 64 n: 1 EOF: 1
===>FIFO test (PHP)
n: 63 EOF: 1
n: 64 n: 0 EOF: 1
n: 64 n: 1 EOF: 1
===>pipe test (C)
n: 63 EOF: 1
n: 64 n: 0 EOF: 1
n: 64 n: 1 EOF: 1
===>pipe test (PHP)
n: 63 EOF: 0
n: 64 n: 0 EOF: 1
n: 64 n: 1 EOF: 1

real 2.13
user 0.04
sys 0.06


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-04 05:17 UTC] php-bugs at jbeekman dot nl
Here are the test files that the bug tracker would not let me post in the description ("Please do not SPAM our bug system."). You will have to insert :// in the appropriate places.

Download all of the following and save them under the specified name in the same folder and then: chmod ugo+x test.sh; ./test.sh

closewait.c http pastebin.com/utgrkXJM
fifotest.c http pastebin.com/p8R3WsEk
fifotest.php http pastebin.com/bJgCcjpF
pipetest.c http pastebin.com/9Wd2QSLD
pipetest.php http pastebin.com/qRwuZsnx
test.sh http pastebin.com/J8TmXYAG
 [2022-12-27 17:02 UTC] bukka@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: bukka
 [2022-12-27 17:02 UTC] bukka@php.net
Apology for taking so long to look into this. I have done investigation and debugging of the provided programs. It comes basically to 2 things as mentioned:

1. EOF handling for pipes - This is a real issue which I created a new issue with all the details: https://github.com/php/php-src/issues/10171

2. Delay in pipe test (PHP) reading - this is actually and issue in the test (took me some time to realise that) as it is not the same as C pipetest. Specifically it is using shell (due to $argv implode). It means that the stdout is not closed for the proc opened program (shell) and it needs wait for program to finish. If the implode is replaced just by passing $argv (`$p = proc_open($argv, $descriptorspec, $fd);`), then it works in the same way as C example.

So closing this bug as all that needs to be done is described in the new issue. Thanks for reporting this though.
 [2022-12-27 17:35 UTC] bukka@php.net
-Status: Closed +Status: Analyzed
 [2022-12-27 17:35 UTC] bukka@php.net
I will actually leave it open and close it once the issue is done.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC