Skip to content

Commit b146f75

Browse files
committed
merged branch romainneutron/slowness (PR #8749)
This PR was merged into the 2.2 branch. Discussion ---------- [Process] Fix #8746 : slowness added in unit tests since #8741 | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8746 | License | MIT Commits ------- 8c4bae3 [Process] Revert change 8d9c7c6 [Process] Fix #8746 : slowness added in unit tests since #8741
2 parents f6e664d + 8c4bae3 commit b146f75

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public function start($callback = null)
287287
stream_set_blocking($pipe, false);
288288
}
289289

290-
$this->writePipes(false);
290+
$this->writePipes();
291291
$this->updateStatus(false);
292292
$this->checkTimeout();
293293
}
@@ -339,9 +339,9 @@ public function wait($callback = null)
339339
if (null !== $callback) {
340340
$this->callback = $this->buildCallback($callback);
341341
}
342-
while ($this->processInformation['running']) {
343-
$this->updateStatus(true);
342+
while ($this->pipes || (defined('PHP_WINDOWS_VERSION_BUILD') && $this->fileHandles)) {
344343
$this->checkTimeout();
344+
$this->readPipes(true);
345345
}
346346
$this->updateStatus(false);
347347
if ($this->processInformation['signaled']) {
@@ -1069,23 +1069,7 @@ private function readPipes($blocking)
10691069
return;
10701070
}
10711071

1072-
foreach ($r as $pipe) {
1073-
$type = array_search($pipe, $this->pipes);
1074-
$data = fread($pipe, 8192);
1075-
1076-
if (strlen($data) > 0) {
1077-
// last exit code is output and caught to work around --enable-sigchild
1078-
if (3 == $type) {
1079-
$this->fallbackExitcode = (int) $data;
1080-
} else {
1081-
call_user_func($this->callback, $type == 1 ? self::OUT : self::ERR, $data);
1082-
}
1083-
}
1084-
if (false === $data || feof($pipe)) {
1085-
fclose($pipe);
1086-
unset($this->pipes[$type]);
1087-
}
1088-
}
1072+
$this->processReadPipes($r);
10891073
}
10901074
}
10911075

@@ -1094,7 +1078,7 @@ private function readPipes($blocking)
10941078
*
10951079
* @param Boolean $blocking Whether to use blocking calls or not.
10961080
*/
1097-
private function writePipes($blocking)
1081+
private function writePipes()
10981082
{
10991083
if (null === $this->stdin) {
11001084
fclose($this->pipes[0]);
@@ -1109,7 +1093,11 @@ private function writePipes($blocking)
11091093
$stdinOffset = 0;
11101094

11111095
while ($writePipes) {
1112-
$r = array();
1096+
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
1097+
$this->processFileHandles();
1098+
}
1099+
1100+
$r = $this->pipes;
11131101
$w = $writePipes;
11141102
$e = null;
11151103

@@ -1136,6 +1124,34 @@ private function writePipes($blocking)
11361124
$writePipes = null;
11371125
}
11381126
}
1127+
1128+
$this->processReadPipes($r);
1129+
}
1130+
}
1131+
1132+
/**
1133+
* Processes read pipes, executes callback on it.
1134+
*
1135+
* @param array $pipes
1136+
*/
1137+
private function processReadPipes(array $pipes)
1138+
{
1139+
foreach ($pipes as $pipe) {
1140+
$type = array_search($pipe, $this->pipes);
1141+
$data = fread($pipe, 8192);
1142+
1143+
if (strlen($data) > 0) {
1144+
// last exit code is output and caught to work around --enable-sigchild
1145+
if (3 == $type) {
1146+
$this->fallbackExitcode = (int) $data;
1147+
} else {
1148+
call_user_func($this->callback, $type == 1 ? self::OUT : self::ERR, $data);
1149+
}
1150+
}
1151+
if (false === $data || feof($pipe)) {
1152+
fclose($pipe);
1153+
unset($this->pipes[$type]);
1154+
}
11391155
}
11401156
}
11411157
}

src/Symfony/Component/Process/Tests/AbstractProcessTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public function testPhpDeadlock()
360360

361361
// Sleep doesn't work as it will allow the process to handle signals and close
362362
// file handles from the other end.
363-
$process = $this->getProcess('php -r "sleep 4"');
363+
$process = $this->getProcess('php -r "while (true) {}"');
364364
$process->start();
365365

366366
// PHP will deadlock when it tries to cleanup $process

0 commit comments

Comments
 (0)