From 4916378931ff356312719d28ec713aaa39a4354e Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sun, 17 Sep 2017 20:56:17 +0200 Subject: [PATCH] Fix exit code watcher on Windows --- lib/Internal/Windows/Handle.php | 3 +++ lib/Internal/Windows/Runner.php | 2 ++ lib/Internal/Windows/SocketConnector.php | 11 ++++------- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/Internal/Windows/Handle.php b/lib/Internal/Windows/Handle.php index 196180f..567d814 100644 --- a/lib/Internal/Windows/Handle.php +++ b/lib/Internal/Windows/Handle.php @@ -16,6 +16,9 @@ final class Handle extends ProcessHandle { /** @var string */ public $exitCodeWatcher; + /** @var bool */ + public $exitCodeRequested = false; + /** @var resource */ public $proc; diff --git a/lib/Internal/Windows/Runner.php b/lib/Internal/Windows/Runner.php index 4cc8d58..b9e1739 100644 --- a/lib/Internal/Windows/Runner.php +++ b/lib/Internal/Windows/Runner.php @@ -110,6 +110,8 @@ final class Runner implements ProcessRunner { /** @inheritdoc */ public function join(ProcessHandle $handle): Promise { /** @var Handle $handle */ + $handle->exitCodeRequested = true; + if ($handle->exitCodeWatcher !== null) { Loop::reference($handle->exitCodeWatcher); } diff --git a/lib/Internal/Windows/SocketConnector.php b/lib/Internal/Windows/SocketConnector.php index 411b135..38f3a8e 100644 --- a/lib/Internal/Windows/SocketConnector.php +++ b/lib/Internal/Windows/SocketConnector.php @@ -236,8 +236,9 @@ final class SocketConnector { $handle->stdioDeferreds[1]->resolve(new ResourceInputStream($handle->sockets[1])); $handle->stdioDeferreds[2]->resolve(new ResourceInputStream($handle->sockets[2])); - if ($handle->exitCodeWatcher !== null) { - Loop::enable($handle->exitCodeWatcher); + $handle->exitCodeWatcher = Loop::onReadable($handle->sockets[0], [$this, 'onReadableExitCode'], $handle); + if (!$handle->exitCodeRequested) { + Loop::unreference($handle->exitCodeWatcher); } unset($this->pendingProcesses[$handle->wrapperPid]); @@ -258,7 +259,7 @@ final class SocketConnector { if (\strlen($data) !== 5) { $handle->status = ProcessStatus::ENDED; $handle->joinDeferred->fail(new ProcessException( - \sprintf('Failed to read exit code from wrapper: Recieved %d of 5 expected bytes', \strlen($data)) + \sprintf('Failed to read exit code from wrapper: Received %d of 5 expected bytes', \strlen($data)) )); return; } @@ -324,9 +325,5 @@ final class SocketConnector { $handle->connectTimeoutWatcher = Loop::delay(self::CONNECT_TIMEOUT, [$this, 'onProcessConnectTimeout'], $handle); $this->pendingProcesses[$handle->wrapperPid] = $handle; - - $handle->exitCodeWatcher = Loop::onReadable($handle->sockets[0], [$this, 'onReadableExitCode'], $handle); - Loop::unreference($handle->exitCodeWatcher); - Loop::disable($handle->exitCodeWatcher); } }