xref: /PHP-7.3/ext/standard/tests/file/bug60120.phpt (revision 3f72c77c)
1--TEST--
2Bug #60120 (proc_open hangs when data in stdin/out/err is getting larger or equal to 2048)
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) != 'WIN') {
6    die('skip only for Windows');
7}
8$php = getenv('TEST_PHP_EXECUTABLE');
9if (!$php) {
10	die("skip No php executable defined\n");
11}
12?>
13--FILE--
14<?php
15
16error_reporting(E_ALL);
17
18$php = getenv('TEST_PHP_EXECUTABLE');
19if (!$php) {
20	die("No php executable defined\n");
21}
22$cmd = 'php -r "fwrite(STDOUT, $in = file_get_contents(\'php://stdin\')); fwrite(STDERR, $in);"';
23$descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
24$stdin = str_repeat('*', 1024 * 16) . '!';
25$stdin = str_repeat('*', 2049 );
26
27$options = array_merge(array('suppress_errors' => true, 'bypass_shell' => false));
28$process = proc_open($cmd, $descriptors, $pipes, getcwd(), array(), $options);
29
30foreach ($pipes as $pipe) {
31    stream_set_blocking($pipe, false);
32}
33$writePipes = array($pipes[0]);
34$stdinLen = strlen($stdin);
35$stdinOffset = 0;
36
37unset($pipes[0]);
38
39while ($pipes || $writePipes) {
40    $r = $pipes;
41    $w = $writePipes;
42    $e = null;
43    $n = stream_select($r, $w, $e, 60);
44
45    if (false === $n) {
46        break;
47    } elseif ($n === 0) {
48        proc_terminate($process);
49
50    }
51    if ($w) {
52        $written = fwrite($writePipes[0], substr($stdin, $stdinOffset), 8192);
53        if (false !== $written) {
54            $stdinOffset += $written;
55        }
56        if ($stdinOffset >= $stdinLen) {
57            fclose($writePipes[0]);
58            $writePipes = null;
59        }
60    }
61
62    foreach ($r as $pipe) {
63        $type = array_search($pipe, $pipes);
64        $data = fread($pipe, 8192);
65        if (false === $data || feof($pipe)) {
66            fclose($pipe);
67            unset($pipes[$type]);
68        }
69    }
70}
71echo "OK.";
72?>
73--EXPECT--
74OK.
75