Skip to content

Commit 2e2114e

Browse files
authored
unix: fix partial read handling after POLLHUP (#4997)
It was reported that PTYs on Linux sometimes report POLLHUP, return a partial read, but still return more data on the next read. Libuv contains an optimization where it assumes a partial read after POLLHUP means the next read can be skipped because it's going to fail with EOF anyway. That assumption was thought to be always true but, alas, it isn't. The fact the optimization has been present for 13 years and this is the first bug report about it, indicates how rare this particular condition is, but of course we can't skim on correctness. The reworked optimization only uses POLLHUP as an input signal when POLLIN is not also set. That means we no longer have to track partial reads because we're going to try and read anyway as long as POLLIN is set. It seems to cause no measurable regressions on the test suite or the (lightly tested) benchmarks. Fixes: #4992
1 parent 588ea9b commit 2e2114e

4 files changed

Lines changed: 91 additions & 14 deletions

File tree

‎src/unix/stream.c‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,8 +1030,6 @@ static void uv__read(uv_stream_t* stream) {
10301030
int err;
10311031
int is_ipc;
10321032

1033-
stream->flags &= ~UV_HANDLE_READ_PARTIAL;
1034-
10351033
/* Prevent loop starvation when the data comes in as fast as (or faster than)
10361034
* we can read it. XXX Need to rearm fd if we switch to edge-triggered I/O.
10371035
*/
@@ -1146,11 +1144,15 @@ static void uv__read(uv_stream_t* stream) {
11461144
#endif
11471145
stream->read_cb(stream, nread, &buf);
11481146

1149-
/* Return if we didn't fill the buffer, there is no more data to read. */
1150-
if (nread < buflen) {
1151-
stream->flags |= UV_HANDLE_READ_PARTIAL;
1147+
/* Save a system call and return if we didn't fill the buffer
1148+
* completely, on the assumption the next read() will fail with EOF.
1149+
*
1150+
* Devices like PTYs sometimes operate in a packet-like mode where
1151+
* they don't return all available data in a single read but we'll
1152+
* catch it on the next read because of level-triggered I/O.
1153+
*/
1154+
if (nread < buflen)
11521155
return;
1153-
}
11541156
}
11551157
}
11561158
}
@@ -1202,22 +1204,23 @@ void uv__stream_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
12021204

12031205
assert(uv__stream_fd(stream) >= 0);
12041206

1205-
/* Ignore POLLHUP here. Even if it's set, there may still be data to read. */
1206-
if (events & (POLLIN | POLLERR | POLLHUP))
1207+
if (events & (POLLIN | POLLERR))
12071208
uv__read(stream);
12081209

12091210
if (uv__stream_fd(stream) == -1)
12101211
return; /* read_cb closed stream. */
12111212

12121213
/* Short-circuit iff POLLHUP is set, the user is still interested in read
1213-
* events and uv__read() reported a partial read but not EOF. If the EOF
1214-
* flag is set, uv__read() called read_cb with err=UV_EOF and we don't
1215-
* have to do anything. If the partial read flag is not set, we can't
1216-
* report the EOF yet because there is still data to read.
1214+
* events and uv__read() didn't see EOF. If the EOF flag is set, uv__read()
1215+
* called read_cb with err=UV_EOF and we don't have to do anything.
1216+
*
1217+
* POLLIN should not be set because, at least on Linux and possibly other
1218+
* operating systems, devices like PTYs sometimes produce partial reads even
1219+
* when more data is available.
12171220
*/
12181221
if ((events & POLLHUP) &&
1222+
!(events & POLLIN) &&
12191223
(stream->flags & UV_HANDLE_READING) &&
1220-
(stream->flags & UV_HANDLE_READ_PARTIAL) &&
12211224
!(stream->flags & UV_HANDLE_READ_EOF)) {
12221225
uv_buf_t buf = { NULL, 0 };
12231226
uv__stream_eof(stream, &buf);

‎src/uv-common.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ enum {
9090
UV_HANDLE_LISTENING = 0x00000040,
9191
UV_HANDLE_CONNECTION = 0x00000080,
9292
UV_HANDLE_SHUT = 0x00000200,
93-
UV_HANDLE_READ_PARTIAL = 0x00000400,
9493
UV_HANDLE_READ_EOF = 0x00000800,
9594

9695
/* Used by streams and UDP handles. */

‎test/test-list.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ TEST_DECLARE (tty_escape_sequence_processing)
7777
#endif
7878
TEST_DECLARE (tty_file)
7979
TEST_DECLARE (tty_pty)
80+
TEST_DECLARE (tty_pty_partial)
8081
TEST_DECLARE (stdio_over_pipes)
8182
TEST_DECLARE (stdio_emulate_iocp)
8283
TEST_DECLARE (ip6_pton)
@@ -665,6 +666,7 @@ TASK_LIST_START
665666
#endif
666667
TEST_ENTRY (tty_file)
667668
TEST_ENTRY (tty_pty)
669+
TEST_ENTRY (tty_pty_partial)
668670
TEST_ENTRY (stdio_over_pipes)
669671
TEST_ENTRY (stdio_emulate_iocp)
670672
TEST_ENTRY (ip6_pton)

‎test/test-tty.c‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,76 @@ TEST_IMPL(tty_pty) {
460460
#endif
461461
return 0;
462462
}
463+
464+
#if !defined(__ANDROID__) && !defined(_WIN32)
465+
static int tty_pty_partial_read_count;
466+
467+
static void tty_pty_partial_feeder(void *arg) {
468+
static char buf[1<<13];
469+
ssize_t n;
470+
ssize_t r;
471+
int fd;
472+
int i;
473+
474+
fd = *(int *)arg;
475+
memset(buf, 'x', sizeof(buf));
476+
for (i = 0; i < 8; i++) {
477+
for (n = 0; n < (int) sizeof(buf); n += r) {
478+
do
479+
r = write(fd, &buf[n], sizeof(buf) - n);
480+
while (r == -1 && errno == EINTR);
481+
ASSERT_GT(r, 0);
482+
}
483+
}
484+
ASSERT_OK(close(fd));
485+
}
486+
487+
static void tty_pty_partial_alloc_cb(uv_handle_t* handle,
488+
size_t suggested_size,
489+
uv_buf_t *buf) {
490+
static char slab[1<<16];
491+
*buf = uv_buf_init(slab, sizeof(slab));
492+
}
493+
494+
static void tty_pty_partial_read_cb(uv_stream_t* stream,
495+
ssize_t nread,
496+
const uv_buf_t *buf) {
497+
if (nread > 0)
498+
tty_pty_partial_read_count += nread;
499+
else
500+
uv_close((uv_handle_t*) stream, NULL);
501+
}
502+
#endif /* !defined(__ANDROID__) && !defined(_WIN32) */
503+
504+
TEST_IMPL(tty_pty_partial) {
505+
#if !defined(__ANDROID__) && !defined(_WIN32)
506+
int master_fd, slave_fd;
507+
uv_tty_t master_tty;
508+
uv_thread_t tid;
509+
uv_loop_t loop;
510+
int i;
511+
512+
/* This test is not 100% deterministic. If the bug it is testing for is
513+
* present, then it fails about 1 in 3 times, that's why it runs in a loop.
514+
*/
515+
for (i = 0; i < 10; i++) {
516+
if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL))
517+
RETURN_SKIP("No pty available, skipping.");
518+
519+
tty_pty_partial_read_count = 0;
520+
ASSERT_OK(uv_loop_init(&loop));
521+
ASSERT_OK(uv_tty_init(&loop, &master_tty, master_fd, 0));
522+
ASSERT_OK(uv_read_start((uv_stream_t*) &master_tty,
523+
tty_pty_partial_alloc_cb,
524+
tty_pty_partial_read_cb));
525+
ASSERT(uv_is_readable((uv_stream_t*) &master_tty));
526+
ASSERT(uv_is_writable((uv_stream_t*) &master_tty));
527+
ASSERT_OK(uv_thread_create(&tid, tty_pty_partial_feeder, &slave_fd));
528+
ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT));
529+
ASSERT_OK(uv_thread_join(&tid));
530+
ASSERT_EQ(tty_pty_partial_read_count, 65536);
531+
MAKE_VALGRIND_HAPPY(&loop);
532+
}
533+
#endif
534+
return 0;
535+
}

0 commit comments

Comments
 (0)