From 0d8f449385776312caab8f5a139439acc60b9984 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Mon, 30 Oct 2023 10:41:42 -0500 Subject: [PATCH] Back port descriptor fix --- src/MockWebServer.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/MockWebServer.php b/src/MockWebServer.php index 1e9a4c3..3a110d9 100644 --- a/src/MockWebServer.php +++ b/src/MockWebServer.php @@ -27,6 +27,13 @@ class MockWebServer { */ private $process; + /** + * Contains the descriptors for the process after it has been started + * + * @var resource[] + */ + private $descriptors = []; + /** * TestWebServer constructor. * @@ -67,7 +74,7 @@ public function start() : void { InternalServer::incrementRequestCounter($this->tmpDir, 0); - $this->process = $this->startServer($fullCmd); + [ $this->process, $this->descriptors ] = $this->startServer($fullCmd); for( $i = 0; $i <= 20; $i++ ) { usleep(100000); @@ -123,6 +130,10 @@ public function stop() : void { usleep(10000); } } + + foreach( $this->descriptors as $descriptor ) { + @fclose($descriptor); + } } /** @@ -275,9 +286,9 @@ private function isWindowsPlatform() : bool { } /** - * @return resource + * @return array{resource, resource[]} */ - private function startServer( string $fullCmd ) { + private function startServer( string $fullCmd ) : array { if( !$this->isWindowsPlatform() ) { // We need to prefix exec to get the correct process http://php.net/manual/ru/function.proc-get-status.php#93382 $fullCmd = 'exec ' . $fullCmd; @@ -287,14 +298,13 @@ private function startServer( string $fullCmd ) { $env = null; $cwd = null; - $stdin = fopen('php://stdin', 'rb'); $stdoutf = tempnam(sys_get_temp_dir(), 'MockWebServer.stdout'); $stderrf = tempnam(sys_get_temp_dir(), 'MockWebServer.stderr'); $descriptorSpec = [ - 0 => $stdin, - 1 => [ 'file', $stdoutf, 'a' ], - 2 => [ 'file', $stderrf, 'a' ], + 0 => fopen('php://stdin', 'rb'), + 1 => fopen($stdoutf, 'a'), + 2 => fopen($stderrf, 'a'), ]; $process = proc_open($fullCmd, $descriptorSpec, $pipes, $cwd, $env, [ @@ -303,7 +313,7 @@ private function startServer( string $fullCmd ) { ]); if( is_resource($process) ) { - return $process; + return [ $process, $descriptorSpec ]; } throw new Exceptions\ServerException('Error starting server');