diff --git a/src/Process.php b/src/Process.php index 2a275e9..d06b303 100644 --- a/src/Process.php +++ b/src/Process.php @@ -199,11 +199,20 @@ public function start(LoopInterface $loop = null, $interval = 0.1) $options['suppress_errors'] = true; } + $errstr = ''; + \set_error_handler(function ($_, $error) use (&$errstr) { + // Match errstr from PHP's warning message. + // proc_open(/dev/does-not-exist): Failed to open stream: No such file or directory + $errstr = $error; + }); + + $pipes = array(); $this->process = @\proc_open($cmd, $fdSpec, $pipes, $this->cwd, $this->env, $options); + \restore_error_handler(); + if (!\is_resource($this->process)) { - $error = \error_get_last(); - throw new \RuntimeException('Unable to launch a new process: ' . $error['message']); + throw new \RuntimeException('Unable to launch a new process: ' . $errstr); } // count open process pipes and await close event for each to drain buffers before detecting exit diff --git a/tests/AbstractProcessTest.php b/tests/AbstractProcessTest.php index d41de2e..de7da35 100644 --- a/tests/AbstractProcessTest.php +++ b/tests/AbstractProcessTest.php @@ -126,7 +126,7 @@ public function testStartWithCustomPipesWillAssignPipes() $this->assertInstanceOf('React\Stream\WritableStreamInterface', $process->pipes[3]); } - public function testStartWithInvalidFileDescriptorPathWillThrow() + public function testStartWithInvalidFileDescriptorPathWillThrowWithoutCallingCustomErrorHandler() { if (defined('HHVM_VERSION')) { $this->markTestSkipped('Not supported on legacy HHVM'); @@ -138,8 +138,22 @@ public function testStartWithInvalidFileDescriptorPathWillThrow() $process = new Process('exit 0', null, null, $fds); + $error = null; + set_error_handler(function ($_, $errstr) use (&$error) { + $error = $errstr; + }); + $this->setExpectedException('RuntimeException', 'No such file or directory'); - $process->start($this->createLoop()); + + try { + $process->start($this->createLoop()); + restore_error_handler(); + } catch (\Exception $e) { + restore_error_handler(); + $this->assertNull($error); + + throw $e; + } } public function testStartWithExcessiveNumberOfFileDescriptorsWillThrow()