Skip to content

Commit 6f42d90

Browse files
committed
phpGH-16889: stream_select() timeout useless for pipes on Windows
Pipes are blocking on Windows, but `php_select()` always returns them as ready for read/write. This renders the `stream_select()` timeout useless, what can cause a following read to block for a very long time. While there is no general fix (and least not within reach for a stable version), we can at least cater to the important case of read pipes by peeking the pipe to check whether data is available. If there is none, we do not add the handle to the read set.
1 parent ff3b4ec commit 6f42d90

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
GH-16889 (stream_select() timeout useless for pipes on Windows)
3+
--FILE--
4+
<?php
5+
$desc = [
6+
["pipe", "r"],
7+
["pipe", "w"],
8+
["pipe", "w"],
9+
];
10+
// open process which won't produce output for 10s
11+
$proc = proc_open([PHP_BINARY, "-r", "sleep(10); echo 'finish';"], $desc, $pipes);
12+
$read = [$pipes[1]];
13+
$write = null;
14+
$except = null;
15+
$time0 = microtime(true);
16+
// select STDOUT pipe of process for 1ms
17+
if (stream_select($read, $write, $except, 0, 1000)) {
18+
var_dump(fread($read[0], 1));
19+
}
20+
// avoid blocking of finishing the test process
21+
proc_terminate($proc);
22+
$time1 = microtime(true);
23+
var_dump($time1 - $time0 < 1);
24+
?>
25+
--EXPECT--
26+
bool(true)

win32/select.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* - If you supply only sockets, this simply passes through to winsock select().
2222
* - If you supply file handles, there is no way to distinguish between
2323
* ready for read/write or OOB, so any set in which the handle is found will
24-
* be marked as ready.
24+
* be marked as ready. Pipes will be checked if they are ready for read, though.
2525
* - If you supply a mixture of handles and sockets, the system will interleave
2626
* calls between select() and WaitForMultipleObjects(). The time slicing may
2727
* cause this function call to take up to 100 ms longer than you specified.
@@ -135,15 +135,20 @@ PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *e
135135
for (i = 0; i < n_handles; i++) {
136136
if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
137137
if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
138-
FD_SET((uint32_t)handle_slot_to_fd[i], &aread);
138+
DWORD avail_read = 0;
139+
if (!PeekNamedPipe(handles[i], NULL, 0, NULL, &avail_read, NULL) || avail_read > 0) {
140+
FD_SET((uint32_t)handle_slot_to_fd[i], &aread);
141+
retcode++;
142+
}
139143
}
140144
if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
141145
FD_SET((uint32_t)handle_slot_to_fd[i], &awrite);
146+
retcode++;
142147
}
143148
if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
144149
FD_SET((uint32_t)handle_slot_to_fd[i], &aexcept);
150+
retcode++;
145151
}
146-
retcode++;
147152
}
148153
}
149154
}

0 commit comments

Comments
 (0)