Skip to content

Commit 05a003a

Browse files
santigimenosaghul
authored andcommitted
stream: squelch ECONNRESET error if already closed
Add new UV__POLLRDHUP event to be emitted when EPOLLRDHUP(in Linux) or EV_EOF(in BSD / OSX) is detected and only if UV_READABLE is set. When a read returns ECONNRESET after a UV__POLLRDHUP event, emit EOF instead of the error. Add tcp-squelch-connreset test. Not to be run on Windows as it returns ECONNRESET error. Fixes in test-poll and test-tcp-open so they pass after these changes. PR-URL: libuv#403 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
1 parent e110c46 commit 05a003a

File tree

12 files changed

+161
-12
lines changed

12 files changed

+161
-12
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \
224224
test/test-tcp-open.c \
225225
test/test-tcp-read-stop.c \
226226
test/test-tcp-shutdown-after-write.c \
227+
test/test-tcp-squelch-connreset.c \
227228
test/test-tcp-unexpected-read.c \
228229
test/test-tcp-oob.c \
229230
test/test-tcp-write-to-half-open-connection.c \

src/unix/internal.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@
8989
#endif
9090

9191
#if defined(__linux__)
92-
# define UV__POLLIN UV__EPOLLIN
93-
# define UV__POLLOUT UV__EPOLLOUT
94-
# define UV__POLLERR UV__EPOLLERR
95-
# define UV__POLLHUP UV__EPOLLHUP
92+
# define UV__POLLIN UV__EPOLLIN
93+
# define UV__POLLOUT UV__EPOLLOUT
94+
# define UV__POLLERR UV__EPOLLERR
95+
# define UV__POLLHUP UV__EPOLLHUP
96+
# define UV__POLLRDHUP UV__EPOLLRDHUP
9697
#endif
9798

9899
#if defined(__sun) || defined(_AIX)
@@ -118,6 +119,10 @@
118119
# define UV__POLLHUP 8
119120
#endif
120121

122+
#ifndef UV__POLLRDHUP
123+
# define UV__POLLRDHUP 0x200
124+
#endif
125+
121126
#if !defined(O_CLOEXEC) && defined(__FreeBSD__)
122127
/*
123128
* It may be that we are just missing `__POSIX_VISIBLE >= 200809`.
@@ -143,6 +148,7 @@ enum {
143148
UV_TCP_NODELAY = 0x400, /* Disable Nagle. */
144149
UV_TCP_KEEPALIVE = 0x800, /* Turn on keep-alive. */
145150
UV_TCP_SINGLE_ACCEPT = 0x1000, /* Only accept() when idle. */
151+
UV_STREAM_DISCONNECT = 0x2000, /* Remote end is forcibly closed */
146152
UV_HANDLE_IPV6 = 0x10000, /* Handle is bound to a IPv6 socket. */
147153
UV_UDP_PROCESSING = 0x20000 /* Handle is running the send callback queue. */
148154
};

src/unix/kqueue.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {
241241
if (ev->flags & EV_ERROR)
242242
revents |= UV__POLLERR;
243243

244+
if ((w->pevents & UV__POLLIN) && (ev->flags & EV_EOF))
245+
revents |= UV__POLLRDHUP;
246+
244247
if (revents == 0)
245248
continue;
246249

src/unix/linux-core.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {
184184
assert(w->fd < (int) loop->nwatchers);
185185

186186
e.events = w->pevents;
187+
if (w->pevents & UV__POLLIN)
188+
e.events |= UV__POLLRDHUP;
187189
e.data = w->fd;
188190

189191
if (w->events == 0)
@@ -321,7 +323,7 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {
321323
* the current watcher. Also, filters out events that users has not
322324
* requested us to watch.
323325
*/
324-
pe->events &= w->pevents | UV__POLLERR | UV__POLLHUP;
326+
pe->events &= w->pevents | UV__POLLERR | UV__POLLHUP | UV__POLLRDHUP;
325327

326328
/* Work around an epoll quirk where it sometimes reports just the
327329
* EPOLLERR or EPOLLHUP event. In order to force the event loop to

src/unix/linux-syscalls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#define UV__EPOLLOUT 4
7777
#define UV__EPOLLERR 8
7878
#define UV__EPOLLHUP 16
79+
#define UV__EPOLLRDHUP 0x2000
7980
#define UV__EPOLLONESHOT 0x40000000
8081
#define UV__EPOLLET 0x80000000
8182

src/unix/poll.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static void uv__poll_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
4141
}
4242

4343
pevents = 0;
44-
if (events & UV__POLLIN)
44+
if (events & (UV__POLLIN | UV__POLLRDHUP))
4545
pevents |= UV_READABLE;
4646
if (events & UV__POLLOUT)
4747
pevents |= UV_WRITABLE;

src/unix/stream.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,8 @@ static void uv__read(uv_stream_t* stream) {
11421142
uv__stream_osx_interrupt_select(stream);
11431143
}
11441144
stream->read_cb(stream, 0, &buf);
1145+
} else if (errno == ECONNRESET && (stream->flags & UV_STREAM_DISCONNECT)) {
1146+
uv__stream_eof(stream, &buf);
11451147
} else {
11461148
/* Error. User should call uv_close(). */
11471149
stream->read_cb(stream, -errno, &buf);
@@ -1230,8 +1232,11 @@ static void uv__stream_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
12301232
assert(uv__stream_fd(stream) >= 0);
12311233

12321234
/* Ignore POLLHUP here. Even it it's set, there may still be data to read. */
1233-
if (events & (UV__POLLIN | UV__POLLERR | UV__POLLHUP))
1235+
if (events & (UV__POLLIN | UV__POLLERR | UV__POLLHUP | UV__POLLRDHUP)) {
1236+
if (events & UV__POLLRDHUP)
1237+
stream->flags |= UV_STREAM_DISCONNECT;
12341238
uv__read(stream);
1239+
}
12351240

12361241
if (uv__stream_fd(stream) == -1)
12371242
return; /* read_cb closed stream. */

test/test-list.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ TEST_DECLARE (tcp_bind_invalid_flags)
7979
TEST_DECLARE (tcp_listen_without_bind)
8080
TEST_DECLARE (tcp_connect_error_fault)
8181
TEST_DECLARE (tcp_connect_timeout)
82+
#ifndef _WIN32
83+
TEST_DECLARE (tcp_squelch_connreset)
84+
#endif
8285
TEST_DECLARE (tcp_close_while_connecting)
8386
TEST_DECLARE (tcp_close)
8487
TEST_DECLARE (tcp_create_early)
@@ -420,6 +423,9 @@ TASK_LIST_START
420423
TEST_ENTRY (tcp_listen_without_bind)
421424
TEST_ENTRY (tcp_connect_error_fault)
422425
TEST_ENTRY (tcp_connect_timeout)
426+
#ifndef _WIN32
427+
TEST_ENTRY (tcp_squelch_connreset)
428+
#endif
423429
TEST_ENTRY (tcp_close_while_connecting)
424430
TEST_ENTRY (tcp_close)
425431
TEST_ENTRY (tcp_create_early)

test/test-poll.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,15 @@ static void connection_poll_cb(uv_poll_t* handle, int status, int events) {
204204
/* Read a couple of bytes. */
205205
static char buffer[74];
206206
r = recv(context->sock, buffer, sizeof buffer, 0);
207-
ASSERT(r >= 0);
208207

209208
if (r > 0) {
210209
context->read += r;
211-
} else {
210+
} else if (r == 0) {
212211
/* Got FIN. */
213212
context->got_fin = 1;
214213
new_events &= ~UV_READABLE;
214+
} else {
215+
ASSERT(got_eagain());
215216
}
216217

217218
break;
@@ -222,7 +223,6 @@ static void connection_poll_cb(uv_poll_t* handle, int status, int events) {
222223
/* Read until EAGAIN. */
223224
static char buffer[931];
224225
r = recv(context->sock, buffer, sizeof buffer, 0);
225-
ASSERT(r >= 0);
226226

227227
while (r > 0) {
228228
context->read += r;

test/test-tcp-open.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static uv_connect_t connect_req;
3838
static uv_shutdown_t shutdown_req;
3939
static uv_write_t write_req;
4040

41+
static int read_bytes = 0;
4142

4243
static void startup(void) {
4344
#ifdef _WIN32
@@ -111,11 +112,15 @@ static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
111112
ASSERT(tcp != NULL);
112113

113114
if (nread >= 0) {
114-
ASSERT(nread == 4);
115-
ASSERT(memcmp("PING", buf->base, nread) == 0);
115+
read_bytes += nread;
116+
if (nread > 0) {
117+
ASSERT(nread == 4);
118+
ASSERT(memcmp("PING", buf->base, nread) == 0);
119+
}
116120
}
117121
else {
118122
ASSERT(nread == UV_EOF);
123+
ASSERT(read_bytes == 4);
119124
printf("GOT EOF\n");
120125
uv_close((uv_handle_t*)tcp, close_cb);
121126
}

0 commit comments

Comments
 (0)