From f919371aeea323d9d45d2ac2c0b2daf082681f28 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Mon, 18 Feb 2019 09:38:42 -0600 Subject: [PATCH] Don't swallow SynchronizationError in ContextException --- lib/Context/Parallel.php | 12 ++++++------ lib/Context/Process.php | 10 +++++++--- lib/Context/Thread.php | 9 +++++---- test/Context/ThreadTest.php | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) 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() {