Skip to content

Commit 833497e

Browse files
committed
Always read again immediately after reading a full recv buffer
1 parent 2c1c6b5 commit 833497e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/loop.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,19 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int events)
351351
}
352352
}
353353

354-
int length = bsd_recv(us_poll_fd(&s->p), s->context->loop->data.recv_buf + LIBUS_RECV_BUFFER_PADDING, LIBUS_RECV_BUFFER_LENGTH, 0);
354+
int length;
355+
read_more:
356+
length = bsd_recv(us_poll_fd(&s->p), s->context->loop->data.recv_buf + LIBUS_RECV_BUFFER_PADDING, LIBUS_RECV_BUFFER_LENGTH, 0);
355357
if (length > 0) {
356358
s = s->context->on_data(s, s->context->loop->data.recv_buf + LIBUS_RECV_BUFFER_PADDING, length);
359+
360+
/* If we filled the entire recv buffer, we need to immediately read again since otherwise a
361+
* pending hangup event in the same even loop iteration can close the socket before we get
362+
* the chance to read again next iteration */
363+
if (length == LIBUS_RECV_BUFFER_LENGTH && s && !us_socket_is_closed(0, s)) {
364+
goto read_more;
365+
}
366+
357367
} else if (!length) {
358368
if (us_socket_is_shut_down(0, s)) {
359369
/* We got FIN back after sending it */

0 commit comments

Comments
 (0)