diff --git a/lib/Context/Parallel.php b/lib/Context/Parallel.php index 67c8d88..071febc 100644 --- a/lib/Context/Parallel.php +++ b/lib/Context/Parallel.php @@ -269,15 +269,15 @@ final class Parallel implements Context return call(function () { try { $response = yield $this->channel->receive(); - - if (!$response instanceof ExitResult) { - throw new SynchronizationError('Did not receive an exit result from thread.'); - } + $this->close(); } catch (\Throwable $exception) { $this->kill(); throw new ContextException("Failed to receive result from thread", 0, $exception); - } finally { - $this->close(); + } + + if (!$response instanceof ExitResult) { + $this->kill(); + throw new SynchronizationError('Did not receive an exit result from thread.'); } return $response->getResult(); diff --git a/lib/Context/Process.php b/lib/Context/Process.php index d92e2f5..cda4f6a 100644 --- a/lib/Context/Process.php +++ b/lib/Context/Process.php @@ -263,9 +263,6 @@ final class Process implements Context return call(function () { try { $data = yield $this->channel->receive(); - if (!$data instanceof ExitResult) { - throw new SynchronizationError("Did not receive an exit result from process"); - } } catch (\Throwable $exception) { if ($this->isRunning()) { $this->kill(); @@ -273,6 +270,13 @@ final class Process implements Context throw new ContextException("Failed to receive result from process", 0, $exception); } + if (!$data instanceof ExitResult) { + if ($this->isRunning()) { + $this->kill(); + } + throw new SynchronizationError("Did not receive an exit result from process"); + } + $this->channel->close(); $code = yield $this->process->join(); diff --git a/lib/Context/Thread.php b/lib/Context/Thread.php index d26eecf..af2c14c 100644 --- a/lib/Context/Thread.php +++ b/lib/Context/Thread.php @@ -230,10 +230,6 @@ final class Thread implements Context try { $response = yield $this->channel->receive(); - - if (!$response instanceof ExitResult) { - throw new SynchronizationError('Did not receive an exit result from thread.'); - } } catch (\Throwable $exception) { $this->kill(); throw new ContextException("Failed to receive result from thread", 0, $exception); @@ -242,6 +238,11 @@ final class Thread implements Context $this->close(); } + if (!$response instanceof ExitResult) { + $this->kill(); + throw new SynchronizationError('Did not receive an exit result from thread.'); + } + return $response->getResult(); }); } diff --git a/test/Context/ThreadTest.php b/test/Context/ThreadTest.php index 4bd81c8..313843e 100644 --- a/test/Context/ThreadTest.php +++ b/test/Context/ThreadTest.php @@ -269,7 +269,7 @@ class ThreadTest extends TestCase /** * @expectedException \Amp\Parallel\Context\ContextException - * @expectedExceptionMessage The context stopped responding + * @expectedExceptionMessage Failed to receive result */ public function testExitingContextOnJoin() {