1
0
mirror of https://github.com/danog/process.git synced 2024-11-26 12:14:43 +01:00

Fix #29: Kill unresponsive processes

This commit is contained in:
Max Furtuna 2019-01-15 15:33:42 -06:00 committed by Aaron Piotrowski
parent 90f3f31cbd
commit e7e28219da
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
3 changed files with 31 additions and 2 deletions

View File

@ -157,6 +157,15 @@ final class Runner implements ProcessRunner
throw new ProcessException("Terminating process failed");
}
$handle->pidDeferred->promise()->onResolve(function ($error, $pid) {
// The function should not call posix_kill() if $pid is null (i.e., there was an error starting the process).
if ($error) {
return;
}
// ignore errors because process not always detached
@\posix_kill($pid, 9);
});
if ($handle->status < ProcessStatus::ENDED) {
$handle->status = ProcessStatus::ENDED;
$handle->joinDeferred->fail(new ProcessException("The process was killed"));

View File

@ -16,6 +16,7 @@ class ProcessTest extends TestCase
const CMD_PROCESS = \DIRECTORY_SEPARATOR === "\\" ? "cmd /c echo foo" : "echo foo";
const CMD_PROCESS_SLOW = \DIRECTORY_SEPARATOR === "\\" ? "cmd /c ping -n 3 127.0.0.1 > nul" : "sleep 2";
/**
* @expectedException \Amp\Process\StatusError
*/
@ -23,8 +24,8 @@ class ProcessTest extends TestCase
{
Loop::run(function () {
$process = new Process(self::CMD_PROCESS);
$process->start();
$process->start();
yield $process->start();
yield $process->start();
});
}
@ -360,6 +361,20 @@ class ProcessTest extends TestCase
});
}
public function testKillPHPImmediatly()
{
Loop::run(function () {
$socket = \stream_socket_server("tcp://0.0.0.0:10000", $errno, $errstr);
$this->assertNotFalse($socket);
$process = new Process(["php", __DIR__ . "/bin/socket-worker.php"]);
yield $process->start();
$conn = \stream_socket_accept($socket);
$this->assertSame('start', \fread($conn, 5));
$process->kill();
$this->assertEmpty(\fread($conn, 3));
});
}
public function testDebugInfo()
{
Loop::run(function () {

View File

@ -0,0 +1,5 @@
<?php
$socket = \fsockopen('127.0.0.1', 10000);
\fwrite($socket, 'start');
\sleep(2);
\fwrite($socket, 'end');