proc_open hangs with stdin/out with 2048+ bytes
| Bug #60120 | proc_open hangs with stdin/out with 2048+ bytes | ||||
|---|---|---|---|---|---|
| Submitted: | 2011-10-24 12:10 UTC | Modified: | 2011-10-26 08:41 UTC | ||
| From: | pajoye@php.net | Assigned: | pajoye (profile) | ||
| Status: | Closed | Package: | Filesystem function related | ||
| PHP Version: | Irrelevant | OS: | windows | ||
| Private report: | No | CVE-ID: | None | ||
[2011-10-24 12:10 UTC] pajoye@php.net
Description:
------------
The stream used to read data from stdin/out/err hangs if the data passed is
getting larger than 2048, under certain circumstances.
Test script:
---------------
error_reporting(E_ALL);
$cmd = 'php -r "fwrite(STDOUT, $in = file_get_contents(\'php://stdin\')); fwrite(STDERR, $in);"';
$descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
$stdin = str_repeat('*', 1024 * 16) . '!';
$stdin = str_repeat('*', 2049 );
$options = array_merge(array('suppress_errors' => true, 'binary_pipes' => true, 'bypass_shell' => false));
$process = proc_open($cmd, $descriptors, $pipes, getcwd(), array(), $options);
foreach ($pipes as $pipe) {
stream_set_blocking($pipe, false);
}
$writePipes = array($pipes[0]);
$stdinLen = strlen($stdin);
$stdinOffset = 0;
unset($pipes[0]);
while ($pipes || $writePipes) {
$r = $pipes;
$w = $writePipes;
$e = null;
$n = stream_select($r, $w, $e, 60);
if (false === $n) {
break;
} elseif ($n === 0) {
proc_terminate($process);
}
if ($w) {
$written = fwrite($writePipes[0], (binary)substr($stdin, $stdinOffset), 8192);
if (false !== $written) {
$stdinOffset += $written;
}
if ($stdinOffset >= $stdinLen) {
fclose($writePipes[0]);
$writePipes = null;
}
}
foreach ($r as $pipe) {
$type = array_search($pipe, $pipes);
$data = fread($pipe, 8192);
if (false === $data || feof($pipe)) {
fclose($pipe);
unset($pipes[$type]);
}
}
}
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2011-10-24 12:10 UTC] pajoye@php.net
-Assigned To: +Assigned To: pajoye
[2011-10-26 06:23 UTC] fabien at symfony dot com
[2011-10-26 08:41 UTC] pajoye@php.net
-Status: Assigned +Status: Closed
[2011-10-26 08:41 UTC] pajoye@php.net
[2012-02-19 14:34 UTC] nicolas dot sauveur at gmail dot com
On my install of php 5.3.9 (windows 7 wamp server 2, apache 2.2.21), the bug has only been fixed for 2048 to 4096 bytes. Above that, proc_open still hangs for me. I use the same test as above, with $stdin = str_repeat('*', 4097 );[2013-03-16 03:16 UTC] hanskrentel at yahoo dot de