Skip to content

Commit 9afbea2

Browse files
[Process] Fix signaling/stopping logic on Windows
1 parent 52209db commit 9afbea2

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -647,36 +647,24 @@ public function getStatus()
647647
* Stops the process.
648648
*
649649
* @param int|float $timeout The timeout in seconds
650-
* @param int $signal A POSIX signal to send in case the process has not stop at timeout, default is SIGKILL
650+
* @param int $signal A POSIX signal to send in case the process has not stop at timeout, default is SIGKILL (9)
651651
*
652652
* @return int The exit-code of the process
653-
*
654-
* @throws RuntimeException if the process got signaled
655653
*/
656654
public function stop($timeout = 10, $signal = null)
657655
{
658656
$timeoutMicro = microtime(true) + $timeout;
659657
if ($this->isRunning()) {
660-
if ('\\' === DIRECTORY_SEPARATOR && !$this->isSigchildEnabled()) {
661-
exec(sprintf('taskkill /F /T /PID %d 2>&1', $this->getPid()), $output, $exitCode);
662-
if ($exitCode > 0) {
663-
throw new RuntimeException('Unable to kill the process');
664-
}
665-
}
666658
// given `SIGTERM` may not be defined and that `proc_terminate` uses the constant value and not the constant itself, we use the same here
667659
$this->doSignal(15, false);
668660
do {
669661
usleep(1000);
670662
} while ($this->isRunning() && microtime(true) < $timeoutMicro);
671663

672664
if ($this->isRunning() && !$this->isSigchildEnabled()) {
673-
if (null !== $signal || defined('SIGKILL')) {
674-
// avoid exception here :
675-
// process is supposed to be running, but it might have stop
676-
// just after this line.
677-
// in any case, let's silently discard the error, we can not do anything
678-
$this->doSignal($signal ?: SIGKILL, false);
679-
}
665+
// Avoid exception here: process is supposed to be running, but it might have stopped just
666+
// after this line. In any case, let's silently discard the error, we cannot do anything.
667+
$this->doSignal($signal ?: 9, false);
680668
}
681669
}
682670

@@ -1200,7 +1188,18 @@ private function doSignal($signal, $throwException)
12001188
return false;
12011189
}
12021190

1203-
if (true !== @proc_terminate($this->process, $signal)) {
1191+
if ('\\' === DIRECTORY_SEPARATOR) {
1192+
exec(sprintf('taskkill /F /T /PID %d 2>&1', $this->getPid()), $output, $exitCode);
1193+
if ($exitCode) {
1194+
if ($throwException) {
1195+
throw new RuntimeException(sprintf('Unable to kill the process (%s).', implode(' ', $output)));
1196+
}
1197+
1198+
return false;
1199+
}
1200+
}
1201+
1202+
if (true !== @proc_terminate($this->process, $signal) && '\\' !== DIRECTORY_SEPARATOR) {
12041203
if ($throwException) {
12051204
throw new RuntimeException(sprintf('Error while sending signal `%s`.', $signal));
12061205
}

0 commit comments

Comments
 (0)