From edf0a552bf0e97f2add402082af4663bca14f578 Mon Sep 17 00:00:00 2001 From: TonnyJ Date: Wed, 25 Nov 2026 22:31:34 +0100 Subject: [PATCH] Fix GH-25763: stream_select() with a zero timeout sleeps on Windows php_select() calls usleep(100) when only pipes are watched and none of them is ready. It does so even for a zero timeout, so a call which should only poll takes a timer tick, about 15 ms by default. Sleep only if there is something to wait for. Positive and infinite timeouts are unchanged. --- .../stream_select_win32_zero_timeout.phpt | 77 +++++++++++++++++++ win32/select.c | 3 +- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 ext/standard/tests/streams/stream_select_win32_zero_timeout.phpt diff --git a/ext/standard/tests/streams/stream_select_win32_zero_timeout.phpt b/ext/standard/tests/streams/stream_select_win32_zero_timeout.phpt new file mode 100644 index 000000000000..f341c5acbcd9 --- /dev/null +++ b/ext/standard/tests/streams/stream_select_win32_zero_timeout.phpt @@ -0,0 +1,77 @@ +--TEST-- +stream_select() with a zero timeout does not sleep when no pipe is ready +--SKIPIF-- + +--FILE-- + usleep(100)); +$select = fastest(fn() => select($pipes[1], 0)); +var_dump($select < $sleep / 2); +var_dump(select($pipes[1], 0)); + +echo "positive timeout still waits\n"; +$start = hrtime(true); +var_dump(select($pipes[1], 0, 200000)); +var_dump(hrtime(true) - $start > 100e6); + +echo "data\n"; +fwrite($pipes[0], "\n"); +var_dump(select($pipes[1], 10), select($pipes[1], 0)); +var_dump(fread($pipes[1], 10)); +var_dump(select($pipes[1], 0)); + +echo "closed pipe\n"; +fwrite($pipes[0], "\n"); +var_dump(select($pipes[1], 10)); +var_dump(fread($pipes[1], 10), feof($pipes[1])); +var_dump(select($pipes[1], 0)); +?> +--EXPECT-- +empty pipe +bool(true) +int(0) +positive timeout still waits +int(0) +bool(true) +data +int(1) +int(1) +string(1) "x" +int(0) +closed pipe +int(1) +string(0) "" +bool(true) +int(1) diff --git a/win32/select.c b/win32/select.c index 0cd7b8529559..97eabbdf37c6 100644 --- a/win32/select.c +++ b/win32/select.c @@ -168,7 +168,8 @@ PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *e } } } - if (retcode == 0 && num_read_pipes == n_handles && sock_max_fd < 0) { + if (retcode == 0 && num_read_pipes == n_handles && sock_max_fd < 0 + && (tv == NULL || tv->tv_sec > 0 || tv->tv_usec > 0)) { usleep(100); } } while (retcode == 0 && (ms_total == INFINITE || GetTickCount64() < limit));