Skip to content

Commit

Permalink
Set up killProcess to ensure PhantomJS is stopped, including children
Browse files Browse the repository at this point in the history
It seems that `proc_terminate` doesn't reliably shut down child
processes, especially in Windows.

A new method of `killProcess` has been set up that will use Windows
specific `taskkill` and Unix's `kill` to stop the process and its
children.

This in the aim to resolve issue #20 and to mitigate issue #25.
  • Loading branch information
grantlucas committed Jun 12, 2015
1 parent 962f2de commit 3834f37
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/Phantoman.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

class Phantoman extends \Codeception\Platform\Extension
{

// list events to listen to
static $events = array(
'suite.before' => 'beforeSuite',
Expand Down Expand Up @@ -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('.');

Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 3834f37

Please sign in to comment.