Skip to content

Commit af9d538

Browse files
committed
PHPUnit process is blocked when there's a lot of output and a test with separate process
1 parent 7c58ed5 commit af9d538

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

src/Util/PHP/DefaultPhpProcess.php

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
use function array_merge;
1313
use function fclose;
1414
use function file_put_contents;
15+
use function fread;
1516
use function fwrite;
1617
use function is_array;
1718
use function is_resource;
1819
use function proc_close;
1920
use function proc_open;
20-
use function stream_get_contents;
21+
use function proc_terminate;
22+
use function sprintf;
23+
use function stream_select;
2124
use function sys_get_temp_dir;
2225
use function tempnam;
2326
use function unlink;
@@ -112,16 +115,64 @@ protected function runProcess(string $job, array $settings): array
112115

113116
$stderr = $stdout = '';
114117

115-
if (isset($pipes[1])) {
116-
$stdout = stream_get_contents($pipes[1]);
118+
unset($pipes[0]);
119+
$timeout = 5;
117120

118-
fclose($pipes[1]);
119-
}
121+
while (true) {
122+
$r = $pipes;
123+
$w = null;
124+
$e = null;
125+
126+
$n = @stream_select($r, $w, $e, $timeout);
127+
128+
if ($n === false) {
129+
break;
130+
}
131+
132+
if ($n === 0) {
133+
proc_terminate($process, 9);
134+
135+
throw new PhpProcessException(
136+
sprintf(
137+
'Job execution aborted after %d seconds',
138+
$timeout,
139+
),
140+
);
141+
}
120142

121-
if (isset($pipes[2])) {
122-
$stderr = stream_get_contents($pipes[2]);
143+
if ($n > 0) {
144+
foreach ($r as $pipe) {
145+
$pipeOffset = 0;
123146

124-
fclose($pipes[2]);
147+
foreach ($pipes as $i => $origPipe) {
148+
if ($pipe === $origPipe) {
149+
$pipeOffset = $i;
150+
151+
break;
152+
}
153+
}
154+
155+
if (!$pipeOffset) {
156+
break;
157+
}
158+
159+
$line = fread($pipe, 8192);
160+
161+
if ($line === '' || $line === false) {
162+
fclose($pipes[$pipeOffset]);
163+
164+
unset($pipes[$pipeOffset]);
165+
} elseif ($pipeOffset === 1) {
166+
$stdout .= $line;
167+
} else {
168+
$stderr .= $line;
169+
}
170+
}
171+
172+
if (empty($pipes)) {
173+
break;
174+
}
175+
}
125176
}
126177

127178
proc_close($process);

tests/end-to-end/regression/5993/Issue5993Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
--SKIPIF--
1111
<?php
1212
for ($i = 0; $i < 390; $i++) {
13-
trigger_error("error $i");
13+
\trigger_error("error {$i}");
1414
}
1515
?>
1616
--FILE--

0 commit comments

Comments
 (0)