Skip to content

Commit d52f045

Browse files
committed
Fix cli server blocking on accept when using multiple workers
Fixes GH-9400 Closes GH-9693
1 parent 4071e18 commit d52f045

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

sapi/cli/php_cli_server.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,14 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
24412441
retval = FAILURE;
24422442
goto out;
24432443
}
2444+
// server_sock needs to be non-blocking when using multiple processes. Without it, the first process would
2445+
// successfully accept the connection but the others would block, causing client sockets of the same select
2446+
// call not to be handled.
2447+
if (SUCCESS != php_set_sock_blocking(server_sock, 0)) {
2448+
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR, "Failed to make server socket non-blocking");
2449+
retval = FAILURE;
2450+
goto out;
2451+
}
24442452
server->server_sock = server_sock;
24452453

24462454
php_cli_server_startup_workers();
@@ -2581,7 +2589,8 @@ static int php_cli_server_do_event_for_each_fd_callback(void *_params, php_socke
25812589
struct sockaddr *sa = pemalloc(server->socklen, 1);
25822590
client_sock = accept(server->server_sock, sa, &socklen);
25832591
if (!ZEND_VALID_SOCKET(client_sock)) {
2584-
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
2592+
int err = php_socket_errno();
2593+
if (err != SOCK_EAGAIN && php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
25852594
char *errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
25862595
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR,
25872596
"Failed to accept a client (reason: %s)", errstr);

0 commit comments

Comments
 (0)