Skip to content

Commit ce527ed

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: Fix cli server blocking on accept when using multiple workers
2 parents 25cdb3b + 6be8efd commit ce527ed

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
@@ -2499,6 +2499,14 @@ static zend_result php_cli_server_ctor(php_cli_server *server, const char *addr,
24992499
retval = FAILURE;
25002500
goto out;
25012501
}
2502+
// server_sock needs to be non-blocking when using multiple processes. Without it, the first process would
2503+
// successfully accept the connection but the others would block, causing client sockets of the same select
2504+
// call not to be handled.
2505+
if (SUCCESS != php_set_sock_blocking(server_sock, 0)) {
2506+
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR, "Failed to make server socket non-blocking");
2507+
retval = FAILURE;
2508+
goto out;
2509+
}
25022510
server->server_sock = server_sock;
25032511

25042512
php_cli_server_startup_workers();
@@ -2631,7 +2639,8 @@ static zend_result php_cli_server_do_event_for_each_fd_callback(void *_params, p
26312639
struct sockaddr *sa = pemalloc(server->socklen, 1);
26322640
client_sock = accept(server->server_sock, sa, &socklen);
26332641
if (!ZEND_VALID_SOCKET(client_sock)) {
2634-
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
2642+
int err = php_socket_errno();
2643+
if (err != SOCK_EAGAIN && php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
26352644
char *errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
26362645
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR,
26372646
"Failed to accept a client (reason: %s)", errstr);

0 commit comments

Comments
 (0)