Skip to content

Commit 6be8efd

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

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
@@ -2438,6 +2438,14 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
24382438
retval = FAILURE;
24392439
goto out;
24402440
}
2441+
// server_sock needs to be non-blocking when using multiple processes. Without it, the first process would
2442+
// successfully accept the connection but the others would block, causing client sockets of the same select
2443+
// call not to be handled.
2444+
if (SUCCESS != php_set_sock_blocking(server_sock, 0)) {
2445+
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR, "Failed to make server socket non-blocking");
2446+
retval = FAILURE;
2447+
goto out;
2448+
}
24412449
server->server_sock = server_sock;
24422450

24432451
php_cli_server_startup_workers();
@@ -2578,7 +2586,8 @@ static int php_cli_server_do_event_for_each_fd_callback(void *_params, php_socke
25782586
struct sockaddr *sa = pemalloc(server->socklen, 1);
25792587
client_sock = accept(server->server_sock, sa, &socklen);
25802588
if (!ZEND_VALID_SOCKET(client_sock)) {
2581-
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
2589+
int err = php_socket_errno();
2590+
if (err != SOCK_EAGAIN && php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
25822591
char *errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
25832592
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR,
25842593
"Failed to accept a client (reason: %s)", errstr);

0 commit comments

Comments
 (0)