From 80c07011d66a5c9255c983ddfe87ce15fdabf1ca Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Wed, 9 Jan 2019 10:25:30 -0600 Subject: [PATCH] Fix failing Process::join() If the process is killed while joining, an exception was thrown from Process::kill(). --- lib/Context/Process.php | 6 ++++-- test/Context/ProcessTest.php | 23 +++++++++++++++++++++-- test/Context/sleep-process.php | 7 +++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 test/Context/sleep-process.php diff --git a/lib/Context/Process.php b/lib/Context/Process.php index fe38347..d92e2f5 100644 --- a/lib/Context/Process.php +++ b/lib/Context/Process.php @@ -267,8 +267,10 @@ final class Process implements Context throw new SynchronizationError("Did not receive an exit result from process"); } } catch (\Throwable $exception) { - $this->kill(); - throw $exception; + if ($this->isRunning()) { + $this->kill(); + } + throw new ContextException("Failed to receive result from process", 0, $exception); } $this->channel->close(); diff --git a/test/Context/ProcessTest.php b/test/Context/ProcessTest.php index 17e3bee..3c6f60a 100644 --- a/test/Context/ProcessTest.php +++ b/test/Context/ProcessTest.php @@ -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; + }); + } } diff --git a/test/Context/sleep-process.php b/test/Context/sleep-process.php new file mode 100644 index 0000000..01396b3 --- /dev/null +++ b/test/Context/sleep-process.php @@ -0,0 +1,7 @@ +