Skip to content

Commit b614b4a

Browse files
committed
GH-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. We need to fix a couple of tests cases: * bug60692.phpt and bug64770.phpt assume that at least the stdin and stdout pipes are always selected as readable, and that the select call will not change their order. We're being more defensive now. * the potentials warnings of bug49936_win32.phpt need to be suppressed, like it has been done earlier for the POSIX variant of this test case[1]. Possibly this test case should be dropped altogether[2]. [1] <c884d37> [2] <2c6b85f> Closes GH-16917.
1 parent d98e191 commit b614b4a

File tree

6 files changed

+49
-16
lines changed

6 files changed

+49
-16
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ PHP NEWS
7474
. Fixed crypt() tests on musl when using --with-external-libcrypt
7575
(Michael Orlitzky).
7676

77+
- Streams:
78+
. Fixed bug GH-16889 (stream_select() timeout useless for pipes on Windows).
79+
(cmb)
80+
7781
- Windows:
7882
. Fixed bug GH-10992 (Improper long path support for relative paths). (cmb,
7983
nielsdos)

ext/standard/tests/streams/bug49936_win32.phpt

+2-9
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,10 @@ default_socket_timeout=2
1212

1313
$dir = 'ftp://your:self@localhost/';
1414

15-
var_dump(opendir($dir));
16-
var_dump(opendir($dir));
15+
var_dump(@opendir($dir));
16+
var_dump(@opendir($dir));
1717

1818
?>
1919
--EXPECTF--
20-
Warning: opendir(): connect() failed: %s in %s on line %d
21-
22-
Warning: opendir(ftp://...@localhost/): Failed to open directory: operation failed in %s on line %d
2320
bool(false)
24-
25-
Warning: opendir(): connect() failed: %s in %s on line %d
26-
27-
Warning: opendir(ftp://...@localhost/): Failed to open directory: operation failed in %s on line %d
2821
bool(false)

ext/standard/tests/streams/bug60602.phpt

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ if (is_resource($p)) {
1818
$data = '';
1919

2020
while (1) {
21+
$r = [$pipes[1]];
2122
$w = $e = NULL;
22-
$n = stream_select($pipes, $w, $e, 300);
23+
$n = stream_select($r, $w, $e, 300);
2324

2425
if ($n === false) {
2526
echo "no streams \n";
@@ -29,7 +30,7 @@ if (is_resource($p)) {
2930
proc_terminate($p, 9);
3031
break;
3132
} else if ($n > 0) {
32-
$line = fread($pipes[1], 8192);
33+
$line = fread($r[0], 8192);
3334
if (strlen($line) == 0) {
3435
/* EOF */
3536
break;

ext/standard/tests/streams/bug64770.phpt

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ if (is_resource($p)) {
1818
$data = '';
1919

2020
while (1) {
21+
$r = [$pipes[1]];
2122
$w = $e = NULL;
22-
$n = stream_select($pipes, $w, $e, 300);
23+
$n = stream_select($r, $w, $e, 300);
2324

2425
if ($n === false) {
2526
echo "no streams \n";
@@ -29,7 +30,7 @@ if (is_resource($p)) {
2930
proc_terminate($p, 9);
3031
break;
3132
} else if ($n > 0) {
32-
$line = fread($pipes[1], 8192);
33+
$line = fread($r[0], 8192);
3334
if (strlen($line) == 0) {
3435
/* EOF */
3536
break;
+26
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

+11-3
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,23 @@ 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 (GetFileType(handles[i]) != FILE_TYPE_PIPE
140+
|| !PeekNamedPipe(handles[i], NULL, 0, NULL, &avail_read, NULL)
141+
|| avail_read > 0
142+
) {
143+
FD_SET((uint32_t)handle_slot_to_fd[i], &aread);
144+
retcode++;
145+
}
139146
}
140147
if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
141148
FD_SET((uint32_t)handle_slot_to_fd[i], &awrite);
149+
retcode++;
142150
}
143151
if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
144152
FD_SET((uint32_t)handle_slot_to_fd[i], &aexcept);
153+
retcode++;
145154
}
146-
retcode++;
147155
}
148156
}
149157
}

0 commit comments

Comments
 (0)