Skip to content

Commit

Permalink
handle case where the socket in on_uv_poll may be closed by a send or…
Browse files Browse the repository at this point in the history
… receive callback (#180)
  • Loading branch information
jthomas43 committed May 2, 2024
1 parent 78c1bc6 commit 4deb65f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/io_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ udx__on_writable (udx_socket_t *socket) {
#ifdef UDX_PLATFORM_HAS_SENDMMSG
bool finished = false;

assert((socket->status & UDX_SOCKET_CLOSING_HANDLES) == 0);

while (!finished) {
udx_packet_t *batch[UDX_SENDMMSG_BATCH_SIZE];
struct mmsghdr h[UDX_SENDMMSG_BATCH_SIZE];
Expand Down Expand Up @@ -180,7 +182,7 @@ udx__on_writable (udx_socket_t *socket) {
udx__confirm_packet(batch[i]);
}

if (rc == UV_EAGAIN) {
if (rc == UV_EAGAIN || socket->status & UDX_SOCKET_CLOSING_HANDLES) {
finished = true;
}
}
Expand Down Expand Up @@ -209,6 +211,9 @@ udx__on_writable (udx_socket_t *socket) {
// todo: set in confirm packet with uv_now()
pkt->time_sent = uv_now(socket->udx->loop);
udx__confirm_packet(pkt);
if (socket->status & UDX_SOCKET_CLOSING_HANDLES) {
break;
}
}
#endif
}
4 changes: 4 additions & 0 deletions src/io_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ udx__recvmsg (udx_socket_t *socket, uv_buf_t *buf, struct sockaddr *addr, int ad

void
udx__on_writable (udx_socket_t *socket) {
assert((socket->status & UDX_SOCKET_CLOSING_HANDLES) == 0);
while (true) {
udx_packet_t *pkt = udx__shift_packet(socket);
if (pkt == NULL) break;
Expand All @@ -108,5 +109,8 @@ udx__on_writable (udx_socket_t *socket) {
}
pkt->time_sent = uv_now(socket->udx->loop);
udx__confirm_packet(pkt);
if (socket->status & UDX_SOCKET_CLOSING_HANDLES) {
break;
}
}
}
4 changes: 2 additions & 2 deletions src/udx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,7 @@ on_uv_poll (uv_poll_t *handle, int status, int events) {
buf.base = (char *) &b;
buf.len = 2048;

while ((size = udx__recvmsg(socket, &buf, (struct sockaddr *) &addr, addr_len)) >= 0) {
while (!(socket->status & UDX_SOCKET_CLOSING_HANDLES) && (size = udx__recvmsg(socket, &buf, (struct sockaddr *) &addr, addr_len)) >= 0) {
if (!process_packet(socket, b, size, (struct sockaddr *) &addr) && socket->on_recv != NULL) {
buf.len = size;

Expand All @@ -1613,7 +1613,7 @@ on_uv_poll (uv_poll_t *handle, int status, int events) {
}
}

if (events & UV_WRITABLE) {
if (events & UV_WRITABLE && !(socket->status & UDX_SOCKET_CLOSING_HANDLES)) {
if (events & UV_READABLE) {
// compensate for potentially long-running read callbacks
uv_update_time(handle->loop);
Expand Down

0 comments on commit 4deb65f

Please sign in to comment.