Skip to content

Commit 11a58ee

Browse files
vibbowclaude
andcommitted
Fix GH-24171: Slow stream_socket_client() connect on Windows
On Windows, php_network_connect_socket() calls getsockopt(SO_ERROR) right after select() reports that the non-blocking connect completed. Inside mswsock, SockIsSocketConnected() can then race with mswsock's own async thread and wait on an internal completion port with a 10 ms timeout, so the call often blocks for 10-25 ms. Winsock reports a successful non-blocking connect in writefds and a failed one in exceptfds, and we already poll for POLLOUT|POLLPRI. So when only POLLOUT is set, the connect has succeeded and getsockopt() can be skipped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c88c89e commit 11a58ee

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ PHP NEWS
1818
. Fixed chown() and lchown() failing to resolve user names in ZTS builds
1919
when getpwnam_r() needs a larger buffer. (Ilia Alshanetsky)
2020

21+
- Streams:
22+
. Fixed bug GH-24171 (stream_socket_client()/fsockopen() connect on Windows
23+
is ~10-25 ms slow because getsockopt(SO_ERROR) stalls in mswsock).
24+
(vibbow)
25+
2126
22 Oct 2026, PHP 8.4.27
2227

2328
- BCMath:

‎main/network.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,13 @@ PHPAPI int php_network_connect_socket(php_socket_t sockfd,
418418
ret = -1;
419419
} else if (n == 0) {
420420
error = PHP_TIMEOUT_ERROR_VALUE;
421+
#ifdef PHP_WIN32
422+
} else if ((n & POLLOUT) && !(n & POLLPRI)) {
423+
/* Writable and not in exceptfds: the connect succeeded (see the
424+
* comment above). Skip getsockopt(SO_ERROR) here, as calling it
425+
* right after the connection completes can block for 10-25ms. */
426+
error = 0;
427+
#endif
421428
} else {
422429
len = sizeof(error);
423430
/* BSD-derived systems set errno correctly.

0 commit comments

Comments
 (0)