diff --git a/src/Phantoman.php b/src/Phantoman.php index e0957d9..90dd9c1 100644 --- a/src/Phantoman.php +++ b/src/Phantoman.php @@ -15,7 +15,6 @@ class Phantoman extends \Codeception\Platform\Extension { - // list events to listen to static $events = array( 'suite.before' => 'beforeSuite', @@ -154,10 +153,10 @@ private function stopServer() break; } - foreach ($this->pipes as $pipe) { - fclose($pipe); + // If the process is running, attempt to kill it + if (proc_get_status($this->resource)['running'] == true) { + $this->killProcess(proc_get_status($this->resource)['pid']); } - proc_terminate($this->resource, 2); $this->write('.'); @@ -214,6 +213,29 @@ private function isWindows() return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; } + /** + * Kill Process + * + * proc_terminate doesn't reliably kill child processes + * + * @param int $pid The PID of the process to kill + */ + private function killProcess($pid) + { + $pid = escapeshellarg($pid); + + // Close all pipes first + foreach ($this->pipes as $pipe) { + fclose($pipe); + } + + if ($this->isWindows()) { + exec("taskkill /f /t /pid " . $pid); + } else { + exec("kill -9 " . $pid); + } + } + // methods that handle events public function beforeSuite(\Codeception\Event\SuiteEvent $e) {