1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-30 04:39:01 +01:00

Fix failing Process::join()

If the process is killed while joining, an exception was thrown from Process::kill().
This commit is contained in:
Aaron Piotrowski 2019-01-09 10:25:30 -06:00
parent beafcdb140
commit 80c07011d6
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
3 changed files with 32 additions and 4 deletions

View File

@ -267,8 +267,10 @@ final class Process implements Context
throw new SynchronizationError("Did not receive an exit result from process");
}
} catch (\Throwable $exception) {
if ($this->isRunning()) {
$this->kill();
throw $exception;
}
throw new ContextException("Failed to receive result from process", 0, $exception);
}
$this->channel->close();

View File

@ -35,12 +35,12 @@ class ProcessTest extends TestCase
/**
* @expectedException \Amp\Parallel\Sync\PanicError
* @expectedExceptionMessage No script found at 'test-process.php'
* @expectedExceptionMessage No script found at '../test-process.php'
*/
public function testInvalidScriptPath()
{
Loop::run(function () {
$process = new Process("test-process.php");
$process = new Process("../test-process.php");
yield $process->start();
yield $process->join();
});
@ -84,4 +84,23 @@ class ProcessTest extends TestCase
\var_dump(yield $process->join());
});
}
/**
* @expectedException \Amp\Parallel\Context\ContextException
* @expectedExceptionMessage Failed to receive result from process
*/
public function testKillWhenJoining()
{
Loop::run(function () {
$process = new Process([
__DIR__ . "/sleep-process.php",
5,
]);
yield $process->start();
$promise = $process->join();
$process->kill();
$this->assertFalse($process->isRunning());
yield $promise;
});
}
}

View File

@ -0,0 +1,7 @@
<?php
use Amp\Parallel\Sync\Channel;
return function (Channel $channel) use ($argv) {
\sleep((int) ($argv[1] ?? 1));
};