From fcf575ad4166dec4f357061e6f8798e10ba964a4 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 16 Dec 2024 13:41:02 +0100 Subject: [PATCH] Improve fix for GH-16889 The original patch[1] cared only about pipe handles in the rset, but would be problematic if there are other handles (e.g. files in the rset, or pipes/files in the other sets), because `php_select()` would return immediately, reporting all non read-pipe handles as ready, but possibly never reporting read-pipe handles. We fix this by applying different logic for the case where only pipe handles are supplied in the rset, but no handles in the wset or eset. In this case `php_select()` only returns when actually one of the handles is ready, or when the timeout expires. To avoid busy looping in this case, we sleep for a short amount of time. This matches POSIX behavior. In all other cases, `php_select()` behaves as before (i.e. prior to the original fix), that is it returns immediately, reporting all handles as ready. We also add a test case that demonstrates multiplexing the output of a couple of child processes. See also the discussion on . [1] --- .../proc_open_multiplex.phpt | 37 +++++++++++++++++++ win32/select.c | 14 ++++++- 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 ext/standard/tests/general_functions/proc_open_multiplex.phpt diff --git a/ext/standard/tests/general_functions/proc_open_multiplex.phpt b/ext/standard/tests/general_functions/proc_open_multiplex.phpt new file mode 100644 index 0000000000000..704b0a50660e5 --- /dev/null +++ b/ext/standard/tests/general_functions/proc_open_multiplex.phpt @@ -0,0 +1,37 @@ +--TEST-- +Multiplexing of child output +--FILE-- + 0) { + foreach ($rset as $pipe) { + echo fread($pipe, 6), "\n"; + unset($read_pipes[array_search($pipe, $read_pipes)]); + } + $rset = $read_pipes; +} +?> +--EXPECT-- +hello9 +hello8 +hello7 +hello6 +hello5 +hello4 +hello3 +hello2 +hello1 +hello0 diff --git a/win32/select.c b/win32/select.c index 76d5ef1ee5be9..dec149b665ff5 100644 --- a/win32/select.c +++ b/win32/select.c @@ -16,12 +16,15 @@ #include "php.h" #include "php_network.h" +#include "win32/time.h" /* Win32 select() will only work with sockets, so we roll our own implementation here. * - If you supply only sockets, this simply passes through to winsock select(). * - If you supply file handles, there is no way to distinguish between * ready for read/write or OOB, so any set in which the handle is found will - * be marked as ready. Pipes will be checked if they are ready for read, though. + * be marked as ready. + * - If you supply only pipe handles in rfds, and no handles in wfds or efds, + * the pipes will only be marked as ready if there is data available. * - If you supply a mixture of handles and sockets, the system will interleave * calls between select() and WaitForMultipleObjects(). The time slicing may * cause this function call to take up to 100 ms longer than you specified. @@ -34,6 +37,7 @@ PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *e HANDLE handles[MAXIMUM_WAIT_OBJECTS]; int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS]; int n_handles = 0, i; + int num_read_pipes = 0; fd_set sock_read, sock_write, sock_except; fd_set aread, awrite, aexcept; int sock_max_fd = -1; @@ -78,6 +82,9 @@ PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *e sock_max_fd = i; } } else { + if (SAFE_FD_ISSET(i, rfds) && GetFileType(handles[n_handles]) == FILE_TYPE_PIPE) { + num_read_pipes++; + } handle_slot_to_fd[n_handles] = i; n_handles++; } @@ -136,7 +143,7 @@ PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *e if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) { if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) { DWORD avail_read = 0; - if (GetFileType(handles[i]) != FILE_TYPE_PIPE + if (num_read_pipes < n_handles || !PeekNamedPipe(handles[i], NULL, 0, NULL, &avail_read, NULL) || avail_read > 0 ) { @@ -156,6 +163,9 @@ 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) { + usleep(100); + } } while (retcode == 0 && (ms_total == INFINITE || GetTickCount64() < limit)); if (rfds) {