Skip to content

Commit ec55d34

Browse files
committed
Merge branch 'PHP-8.2'
* PHP-8.2: Fix cli server blocking on accept when using multiple workers
2 parents 1f5e87d + ce527ed commit ec55d34

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
@@ -2529,6 +2529,14 @@ static zend_result php_cli_server_ctor(php_cli_server *server, const char *addr,
25292529
retval = FAILURE;
25302530
goto out;
25312531
}
2532+
// server_sock needs to be non-blocking when using multiple processes. Without it, the first process would
2533+
// successfully accept the connection but the others would block, causing client sockets of the same select
2534+
// call not to be handled.
2535+
if (SUCCESS != php_set_sock_blocking(server_sock, 0)) {
2536+
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR, "Failed to make server socket non-blocking");
2537+
retval = FAILURE;
2538+
goto out;
2539+
}
25322540
server->server_sock = server_sock;
25332541

25342542
php_cli_server_startup_workers();
@@ -2661,7 +2669,8 @@ static zend_result php_cli_server_do_event_for_each_fd_callback(void *_params, p
26612669
struct sockaddr *sa = pemalloc(server->socklen, 1);
26622670
client_sock = accept(server->server_sock, sa, &socklen);
26632671
if (!ZEND_VALID_SOCKET(client_sock)) {
2664-
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
2672+
int err = php_socket_errno();
2673+
if (err != SOCK_EAGAIN && php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
26652674
char *errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
26662675
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR,
26672676
"Failed to accept a client (reason: %s)", errstr);

0 commit comments

Comments
 (0)