From 8c1aaf44eabc2bfe8630fb5a313a0eb1485099c6 Mon Sep 17 00:00:00 2001 From: radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:53:57 +0000 Subject: [PATCH] Preserve queued TCP writes after peer EOF --- Makefile | 1 + event/nio.c | 10 +++++ scripts/unittest.sh | 1 + unittest/CMakeLists.txt | 6 +++ unittest/tcp_eof_drain_test.c | 84 +++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+) create mode 100644 unittest/tcp_eof_drain_test.c diff --git a/Makefile b/Makefile index 34817430b..8796cfb5e 100644 --- a/Makefile +++ b/Makefile @@ -411,6 +411,7 @@ unittest: prepare libhv $(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Iprotocol -Iutil -o bin/sendmail unittest/sendmail_test.c protocol/smtp.c base/hsocket.c base/htime.c util/base64.c $(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Issl -Ievent -o bin/hdns_test unittest/hdns_test.c -Llib -lhv -pthread $(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Issl -Ievent -o bin/hdns_benchmark unittest/hdns_benchmark.c -Llib -lhv -pthread + $(CC) -g -Wall -O0 -std=c99 -I. -Ibase -Issl -Ievent -o bin/tcp_eof_drain_test unittest/tcp_eof_drain_test.c -Llib -lhv -pthread ifeq ($(WITH_EVPP), yes) $(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -o bin/tcpclient_dns_test unittest/tcpclient_dns_test.cpp -Llib -lhv -pthread endif diff --git a/event/nio.c b/event/nio.c index 274834f74..e09d33823 100644 --- a/event/nio.c +++ b/event/nio.c @@ -296,6 +296,16 @@ static void nio_read(hio_t* io) { } } if (nread == 0 && (io->io_type & HIO_TYPE_SOCK_STREAM)) { + if (io->io_type == HIO_TYPE_TCP) { + // FIN stays readable, but the peer can still receive queued writes. + if (hio_read_stop(io) != 0) { + io->error = socket_errno(); + } + else if (io->close && io->error == 0) { + // A graceful close is already draining with its own timeout. + return; + } + } goto disconnect; } if (nread < len) { diff --git a/scripts/unittest.sh b/scripts/unittest.sh index ca1046e8d..c62587b06 100755 --- a/scripts/unittest.sh +++ b/scripts/unittest.sh @@ -71,6 +71,7 @@ fi if [ -x bin/lua_mqtt_test ]; then bin/lua_mqtt_test || exit $? fi +bin/tcp_eof_drain_test || exit $? if [ -x bin/hdns_test ]; then bin/hdns_test fi diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 6e17408b3..a6c8ca5f5 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -180,6 +180,11 @@ add_executable(hdns_benchmark hdns_benchmark.c) target_include_directories(hdns_benchmark PRIVATE .. ../base ../ssl ../event) target_link_libraries(hdns_benchmark ${HV_LIBRARIES}) +# ------event: graceful TCP close------ +add_executable(tcp_eof_drain_test tcp_eof_drain_test.c) +target_include_directories(tcp_eof_drain_test PRIVATE .. ../base ../ssl ../event) +target_link_libraries(tcp_eof_drain_test ${HV_LIBRARIES}) + # ------redis------ if(WITH_EVPP AND WITH_REDIS) add_executable(redis_protocol_test redis_protocol_test.cpp ../redis/RedisMessage.cpp) @@ -219,6 +224,7 @@ add_custom_target(unittest DEPENDS hmutex_test connect_test socketpair_test + tcp_eof_drain_test base64 md5 sha1 diff --git a/unittest/tcp_eof_drain_test.c b/unittest/tcp_eof_drain_test.c new file mode 100644 index 000000000..7e1842dd2 --- /dev/null +++ b/unittest/tcp_eof_drain_test.c @@ -0,0 +1,84 @@ +#include "hloop.h" +#include "hsocket.h" + +static char response[512 * 1024]; +static size_t received; +static int server_closes; + +static void require(int condition, const char* message) { + if (!condition) { + fprintf(stderr, "tcp_eof_drain_test: %s\n", message); + exit(EXIT_FAILURE); + } +} + +static void on_read(hio_t* peer, void* data, int len) { + (void)peer; + require(received + len <= sizeof(response) && memcmp(response + received, data, len) == 0, "unexpected response bytes"); + received += len; +} + +static void on_server_close(hio_t* io) { + require(hio_error(io) == 0, "server closed with an error"); + ++server_closes; +} + +static void on_peer_close(hio_t* peer) { + require(hio_error(peer) == 0, "peer closed with an error"); + hloop_stop(hevent_loop(peer)); +} + +static void on_timeout(htimer_t* timer) { + (void)timer; + require(0, "response did not drain before timeout"); +} + +static int check_drain(int close_before_eof) { + hloop_t* loop = hloop_new(0); + int listener = Listen(0, "127.0.0.1"); + struct sockaddr_in addr; + socklen_t addrlen = sizeof(addr); + require(listener >= 0 && getsockname(listener, (struct sockaddr*)&addr, &addrlen) == 0, "could not listen on loopback"); + int client = socket(AF_INET, SOCK_STREAM, 0); + require(client >= 0 && so_rcvbuf(client, 4096) == 0, "could not set up client"); + require(connect(client, (struct sockaddr*)&addr, addrlen) == 0, "could not connect client"); + int server = accept(listener, NULL, NULL); + closesocket(listener); + require(server >= 0 && so_sndbuf(server, 4096) == 0, "could not set up server"); + + // Wait for the client's FIN before either endpoint starts processing events. +#ifdef OS_WIN + require(shutdown(client, SD_SEND) == 0, "client half-close failed"); +#else + require(shutdown(client, SHUT_WR) == 0, "client half-close failed"); +#endif + char byte; + require(recv(server, &byte, 1, MSG_PEEK) == 0, "server did not receive EOF"); + + hio_t* io = hio_get(loop, server); + hio_t* peer = hio_get(loop, client); + received = 0; + server_closes = 0; + hio_setcb_close(io, on_server_close); + hio_setcb_read(peer, on_read); + hio_setcb_close(peer, on_peer_close); + require(hio_read(io) == 0 && hio_read(peer) == 0, "could not watch both sockets"); + require(hio_write(io, response, sizeof(response)) >= 0 && hio_write_bufsize(io) > 0, "response was not queued"); + if (close_before_eof) hio_close(io); + htimer_add(loop, on_timeout, 10000, 1); + hloop_run(loop); + hloop_free(&loop); + if (received != sizeof(response) || server_closes != 1) { + fprintf(stderr, "%s: received %zu of %zu bytes, server closed %d times\n", close_before_eof ? "local close first" : "EOF first", received, + sizeof(response), server_closes); + return 1; + } + return 0; +} + +int main(void) { + for (size_t i = 0; i < sizeof(response); ++i) response[i] = (char)(i % 251); + int errors = check_drain(0); + errors += check_drain(1); + return errors ? EXIT_FAILURE : EXIT_SUCCESS; +}