@@ -40,35 +40,34 @@ function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.ph
4040$ handle = proc_open ($ cmd , $ descriptorspec , $ pipes , $ doc_root );
4141}
4242
43- // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
44- // it might not be listening yet...need to wait until fsockopen() call returns
45- $ error = "Unable to connect to server \n" ;
43+ // note: here we check the process is running
4644 for ($ i =0 ; $ i < 60 ; $ i ++) {
4745 usleep (50000 ); // 50ms per try
4846 $ status = proc_get_status ($ handle );
49- $ fp = @ fsockopen ( PHP_CLI_SERVER_HOSTNAME , PHP_CLI_SERVER_PORT );
47+
5048 // Failure, the server is no longer running
5149 if (!($ status && $ status ['running ' ])) {
5250 $ error = "Server is not running \n" ;
5351 break ;
5452 }
55- // Success, Connected to servers
56- if ($ fp ) {
57- $ error = '' ;
58- break ;
59- }
6053 }
6154
62- if ($ fp ) {
63- fclose ($ fp );
64- }
65-
66- if ($ error ) {
55+ php_cli_server_start_error:
56+ if (isset ($ error )) {
6757 echo $ error ;
6858 proc_terminate ($ handle );
6959 exit (1 );
7060 }
7161
62+ // note: here we check the server is listening, even when the server prints
63+ // listening on %s:%d
64+ // it may not be ready to accept connections
65+ if (!fsockopen (PHP_CLI_SERVER_HOSTNAME , PHP_CLI_SERVER_PORT )) {
66+ $ error =
67+ "Server is not accepting connections \n" ;
68+ goto php_cli_server_start_error;
69+ }
70+
7271 register_shutdown_function (
7372 function ($ handle ) use ($ router ) {
7473 proc_terminate ($ handle );
0 commit comments