Skip to content

Commit e7765ce

Browse files
committed
examples/proxy: handle shutdown more gracefully
If not we can shutdown prematurely, and not finish sending the bytes we already received. Signed-off-by: Jens Axboe <[email protected]>
1 parent 5a55634 commit e7765ce

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

examples/proxy.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,27 @@ static int pending_shutdown(struct conn *c)
508508
return c->cd[0].pending_shutdown + c->cd[1].pending_shutdown;
509509
}
510510

511+
static bool should_shutdown(struct conn *c)
512+
{
513+
int i;
514+
515+
if (!pending_shutdown(c))
516+
return false;
517+
if (is_sink)
518+
return true;
519+
if (!bidi)
520+
return c->cd[0].rcv == c->cd[1].snd;
521+
522+
for (i = 0; i < 2; i++) {
523+
if (c->cd[0].rcv != c->cd[1].snd)
524+
return false;
525+
if (c->cd[1].rcv != c->cd[0].snd)
526+
return false;
527+
}
528+
529+
return true;
530+
}
531+
511532
static void __close_conn(struct io_uring *ring, struct conn *c)
512533
{
513534
printf("Client %d: queueing shutdown\n", c->tid);
@@ -540,7 +561,7 @@ static void __queue_send(struct io_uring *ring, struct conn *c, int fd,
540561
}
541562

542563
sqe = get_sqe(ring);
543-
io_uring_prep_send(sqe, fd, data, len, MSG_WAITALL);
564+
io_uring_prep_send(sqe, fd, data, len, MSG_WAITALL | MSG_NOSIGNAL);
544565
encode_userdata(sqe, c, __SEND, bgid, bid, fd);
545566
if (fixed_files)
546567
sqe->flags |= IOSQE_FIXED_FILE;
@@ -1055,7 +1076,7 @@ static void check_for_close(struct io_uring *ring)
10551076

10561077
if (c->flags & (CONN_F_DISCONNECTING | CONN_F_DISCONNECTED))
10571078
continue;
1058-
if (pending_shutdown(c)) {
1079+
if (should_shutdown(c)) {
10591080
__close_conn(ring, c);
10601081
c->flags |= CONN_F_DISCONNECTING;
10611082
}

0 commit comments

Comments
 (0)