@@ -40,35 +40,34 @@ function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.ph
40
40
$ handle = proc_open ($ cmd , $ descriptorspec , $ pipes , $ doc_root );
41
41
}
42
42
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
46
44
for ($ i =0 ; $ i < 60 ; $ i ++) {
47
45
usleep (50000 ); // 50ms per try
48
46
$ status = proc_get_status ($ handle );
49
- $ fp = @ fsockopen ( PHP_CLI_SERVER_HOSTNAME , PHP_CLI_SERVER_PORT );
47
+
50
48
// Failure, the server is no longer running
51
49
if (!($ status && $ status ['running ' ])) {
52
50
$ error = "Server is not running \n" ;
53
51
break ;
54
52
}
55
- // Success, Connected to servers
56
- if ($ fp ) {
57
- $ error = '' ;
58
- break ;
59
- }
60
53
}
61
54
62
- if ($ fp ) {
63
- fclose ($ fp );
64
- }
65
-
66
- if ($ error ) {
55
+ php_cli_server_start_error:
56
+ if (isset ($ error )) {
67
57
echo $ error ;
68
58
proc_terminate ($ handle );
69
59
exit (1 );
70
60
}
71
61
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
+
72
71
register_shutdown_function (
73
72
function ($ handle ) use ($ router ) {
74
73
proc_terminate ($ handle );
0 commit comments