From c9b9aa148fb0a7eab0f5cf37d77344b981105868 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 21 Feb 2016 10:36:01 -0600 Subject: [PATCH] Close stream and process before resolving delayed --- src/Process/Process.php | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Process/Process.php b/src/Process/Process.php index 21b90bf..49dee85 100644 --- a/src/Process/Process.php +++ b/src/Process/Process.php @@ -182,10 +182,11 @@ class Process implements ProcessContext $this->poll = Loop\poll($stream, function ($resource) { if (!is_resource($resource) || feof($resource)) { + $this->close($resource); $this->delayed->reject(new ProcessException('Process ended unexpectedly.')); } else { $code = fread($resource, 1); - + $this->close($resource); if (!strlen($code) || !is_numeric($code)) { $this->delayed->reject(new ProcessException('Process ended without providing a status code.')); } else { @@ -193,20 +194,29 @@ class Process implements ProcessContext } } - if (is_resource($resource)) { - fclose($resource); - } - - if (is_resource($this->process)) { - proc_close($this->process); - $this->process = null; - } - - $this->stdin->close(); $this->poll->free(); }); } + /** + * Closes the stream resource provided, the open process handle, and stdin. + * + * @param resource $resource + */ + private function close($resource) + { + if (is_resource($resource)) { + fclose($resource); + } + + if (is_resource($this->process)) { + proc_close($this->process); + $this->process = null; + } + + $this->stdin->close(); + } + /** * @coroutine *