mirror of
https://github.com/danog/process.git
synced 2024-11-26 20:24:43 +01:00
Fix #29: Kill unresponsive processes
This commit is contained in:
parent
90f3f31cbd
commit
e7e28219da
@ -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"));
|
||||
|
@ -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 () {
|
||||
|
5
test/bin/socket-worker.php
Normal file
5
test/bin/socket-worker.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
$socket = \fsockopen('127.0.0.1', 10000);
|
||||
\fwrite($socket, 'start');
|
||||
\sleep(2);
|
||||
\fwrite($socket, 'end');
|
Loading…
Reference in New Issue
Block a user