@@ -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 can not do anything.
667+ $ this ->doSignal ($ signal ?: 9 , false );
680668 }
681669 }
682670
@@ -1200,9 +1188,21 @@ private function doSignal($signal, $throwException)
12001188 return false ;
12011189 }
12021190
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+
12031202 if (true !== @proc_terminate ($ this ->process , $ signal )) {
12041203 if ($ throwException ) {
1205- throw new RuntimeException (sprintf ('Error while sending signal `%s`. ' , $ signal ));
1204+ $ e = error_get_last ();
1205+ throw new RuntimeException (sprintf ('Error while sending signal `%s` (%s). ' , $ signal , $ e ['message ' ]));
12061206 }
12071207
12081208 return false ;
0 commit comments