]> granicus.if.org Git - php/commitdiff
Separate check for process creation and ability to accept connections
authorJoe Watkins <krakjoe@php.net>
Wed, 19 Jun 2019 09:09:26 +0000 (11:09 +0200)
committerJoe Watkins <krakjoe@php.net>
Wed, 19 Jun 2019 09:09:26 +0000 (11:09 +0200)
sapi/cli/tests/php_cli_server.inc

index ff4eef0e129f0d8727961c6319efd12919669404..1a02e7e0ff64cf3433fca5e9f0d51ddca993d24f 100644 (file)
@@ -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);