1--TEST--
2Bug #64438 proc_open hangs with stdin/out with 4097+ bytes
3--FILE--
4<?php
5
6error_reporting(E_ALL);
7
8if (substr(PHP_OS, 0, 3) == 'WIN') {
9	$cmd = PHP_BINARY . ' -n -r "fwrite(STDOUT, $in = file_get_contents(\'php://stdin\')); fwrite(STDERR, $in);"';
10} else {
11	$cmd = PHP_BINARY . ' -n -r \'fwrite(STDOUT, $in = file_get_contents("php://stdin")); fwrite(STDERR, $in);\'';
12}
13$descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
14$stdin = str_repeat('*', 4097);
15
16$options = array_merge(array('suppress_errors' => true, 'bypass_shell' => false));
17$process = proc_open($cmd, $descriptors, $pipes, getcwd(), array(), $options);
18
19foreach ($pipes as $pipe) {
20    stream_set_blocking($pipe, false);
21}
22$writePipes = array($pipes[0]);
23$stdinLen = strlen($stdin);
24$stdinOffset = 0;
25
26unset($pipes[0]);
27
28$pipeEvents = [];
29while ($pipes || $writePipes) {
30    $r = $pipes;
31    $w = $writePipes;
32    $e = null;
33    $n = stream_select($r, $w, $e, 60);
34
35    if (false === $n) {
36        break;
37    } elseif ($n === 0) {
38        proc_terminate($process);
39
40    }
41    if ($w) {
42        $written = fwrite($writePipes[0], substr($stdin, $stdinOffset), 8192);
43        if (false !== $written) {
44            $stdinOffset += $written;
45        }
46        if ($stdinOffset >= $stdinLen) {
47            fclose($writePipes[0]);
48            $writePipes = null;
49        }
50    }
51
52    foreach ($r as $pipe) {
53        $type = array_search($pipe, $pipes);
54        $data = fread($pipe, 8192);
55        if (false === $data || feof($pipe)) {
56            $pipeEvents[(int)$pipe][] = "Closing pipe";
57            fclose($pipe);
58            unset($pipes[$type]);
59        } else {
60            $pipeEvents[(int)$pipe][] = "Read " . strlen($data) . " bytes";
61        }
62    }
63}
64
65var_dump($pipeEvents);
66
67?>
68===DONE===
69--EXPECTF--
70array(2) {
71  [%d]=>
72  array(2) {
73    [0]=>
74    string(15) "Read 4097 bytes"
75    [1]=>
76    string(12) "Closing pipe"
77  }
78  [%d]=>
79  array(2) {
80    [0]=>
81    string(15) "Read 4097 bytes"
82    [1]=>
83    string(12) "Closing pipe"
84  }
85}
86===DONE===
87