From: Joe Watkins Date: Wed, 19 Jun 2019 09:09:26 +0000 (+0200) Subject: Separate check for process creation and ability to accept connections X-Git-Tag: php-7.4.0alpha2~56 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d6480fa231831feece7403a640b6037c11eec317;p=php Separate check for process creation and ability to accept connections --- diff --git a/sapi/cli/tests/php_cli_server.inc b/sapi/cli/tests/php_cli_server.inc index ff4eef0e12..1a02e7e0ff 100644 --- a/sapi/cli/tests/php_cli_server.inc +++ b/sapi/cli/tests/php_cli_server.inc @@ -40,35 +40,34 @@ function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.ph $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root); } - // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.' - // it might not be listening yet...need to wait until fsockopen() call returns - $error = "Unable to connect to server\n"; + // note: here we check the process is running for ($i=0; $i < 60; $i++) { usleep(50000); // 50ms per try $status = proc_get_status($handle); - $fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT); + // Failure, the server is no longer running if (!($status && $status['running'])) { $error = "Server is not running\n"; break; } - // Success, Connected to servers - if ($fp) { - $error = ''; - break; - } } - if ($fp) { - fclose($fp); - } - - if ($error) { +php_cli_server_start_error: + if (isset($error)) { echo $error; proc_terminate($handle); exit(1); } + // note: here we check the server is listening, even when the server prints + // listening on %s:%d + // it may not be ready to accept connections + if (!fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT)) { + $error = + "Server is not accepting connections\n"; + goto php_cli_server_start_error; + } + register_shutdown_function( function($handle) use($router) { proc_terminate($handle);