From 4ae08c0c0dacc889545ba617091150d33240777a Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 1 Dec 2026 21:25:04 +0300 Subject: [PATCH 1/4] http_client: Test a stray event while connecting 435301c1 stopped http_co_t::handle_events() from spinning forever when a connecting client gets an epoll event without EPOLLOUT or EPOLLERR. Add a test that delivers EPOLLIN and EPOLLRDHUP to a connecting client and checks that it returns and still completes the connect afterwards. --- src/test/test_http_client.cpp | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/test/test_http_client.cpp b/src/test/test_http_client.cpp index 18b8b556..316ddc9b 100644 --- a/src/test/test_http_client.cpp +++ b/src/test/test_http_client.cpp @@ -4,6 +4,8 @@ #undef NDEBUG #include #include +#include +#include #include "http_client.h" #include "timerfd_manager.h" @@ -66,10 +68,49 @@ static void test_sync_error_leaves_no_timer(int mode) printf("test_sync_error_leaves_no_timer %d passed\n", mode); } +// An event without EPOLLOUT or EPOLLERR while connect() is in progress must not spin the event loop +static void test_stray_event_while_connecting() +{ + printf("test_stray_event_while_connecting\n"); + timerfd_manager_t tfd(nullptr); + std::function fd_cb; + int cb_fd = -1; + tfd.set_fd_handler = [&](int fd, bool, std::function cb) + { + if (cb) + { + cb_fd = fd; + fd_cb = cb; + } + }; + std::string err; + http_context_t *ctx = http_context_init(&tfd, "", "", "", false, err); + assert(ctx && err == ""); + http_co_t *cli = http_init(ctx); + http_request(cli, "127.0.0.1:1", request, { .timeout = 5000, .keepalive = true }, [&](http_message_t *msg) + { + err = msg->error; + }); + assert(fd_cb); + fd_cb(cb_fd, EPOLLIN); + fd_cb(cb_fd, EPOLLRDHUP); + assert(err == ""); + // The refused connect still completes normally afterwards + struct pollfd pfd = { .fd = cb_fd, .events = POLLOUT }; + assert(poll(&pfd, 1, 1000) == 1); + fd_cb(cb_fd, EPOLLOUT); + assert(err != "" && err != "HTTP request timed out"); + http_close(cli); + http_destroy(cli); + http_context_destroy(ctx); + printf("test_stray_event_while_connecting passed\n"); +} + int main(int narg, char *args[]) { test_sync_error_leaves_no_timer(FAIL_REQUEST); test_sync_error_leaves_no_timer(FAIL_REQUEST_NEXT_IN_CB); test_sync_error_leaves_no_timer(FAIL_WEBSOCKET); + test_stray_event_while_connecting(); return 0; } -- 2.47.3 From 4f1e12aebdfc2b6e741dbacf8f23775f03dc0d25 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 2 Dec 2026 00:02:59 +0300 Subject: [PATCH 2/4] http_client: Drop a timeout check that cannot fail in the stray event test The mock timerfd fires timers only from tick(), which this test never calls. --- src/test/test_http_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/test_http_client.cpp b/src/test/test_http_client.cpp index 316ddc9b..9f061e3c 100644 --- a/src/test/test_http_client.cpp +++ b/src/test/test_http_client.cpp @@ -99,7 +99,7 @@ static void test_stray_event_while_connecting() struct pollfd pfd = { .fd = cb_fd, .events = POLLOUT }; assert(poll(&pfd, 1, 1000) == 1); fd_cb(cb_fd, EPOLLOUT); - assert(err != "" && err != "HTTP request timed out"); + assert(err != ""); http_close(cli); http_destroy(cli); http_context_destroy(ctx); -- 2.47.3 From b6b6ddc5368ec1e978dc49efdfd5a82da09be5b4 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 2 Dec 2026 02:57:32 +0300 Subject: [PATCH 3/4] http_client: Test the stray event with a connect that succeeds A refused connect fails the same way whether or not a stray EPOLLRDHUP is dropped once connect() completes. Connect to a listener and check that the request is still pending after the stray events and the connect. --- src/test/test_http_client.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/test/test_http_client.cpp b/src/test/test_http_client.cpp index 9f061e3c..4accaf85 100644 --- a/src/test/test_http_client.cpp +++ b/src/test/test_http_client.cpp @@ -5,7 +5,10 @@ #include #include #include +#include +#include #include +#include #include "http_client.h" #include "timerfd_manager.h" @@ -83,26 +86,37 @@ static void test_stray_event_while_connecting() fd_cb = cb; } }; + int listen_fd = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + socklen_t addr_len = sizeof(addr); + assert(listen_fd >= 0); + assert(bind(listen_fd, (sockaddr*)&addr, addr_len) == 0); + assert(listen(listen_fd, 1) == 0); + assert(getsockname(listen_fd, (sockaddr*)&addr, &addr_len) == 0); std::string err; http_context_t *ctx = http_context_init(&tfd, "", "", "", false, err); assert(ctx && err == ""); http_co_t *cli = http_init(ctx); - http_request(cli, "127.0.0.1:1", request, { .timeout = 5000, .keepalive = true }, [&](http_message_t *msg) + bool done = false; + http_request(cli, "127.0.0.1:"+std::to_string(ntohs(addr.sin_port)), request, { .timeout = 5000, .keepalive = true }, [&](http_message_t *) { - err = msg->error; + done = true; }); assert(fd_cb); fd_cb(cb_fd, EPOLLIN); fd_cb(cb_fd, EPOLLRDHUP); - assert(err == ""); - // The refused connect still completes normally afterwards + assert(!done); + // The connect still completes, and the stray EPOLLRDHUP does not end the connection struct pollfd pfd = { .fd = cb_fd, .events = POLLOUT }; assert(poll(&pfd, 1, 1000) == 1); fd_cb(cb_fd, EPOLLOUT); - assert(err != ""); + assert(!done); http_close(cli); http_destroy(cli); http_context_destroy(ctx); + close(listen_fd); printf("test_stray_event_while_connecting passed\n"); } -- 2.47.3 From 75135a22e63870120f23a1ed6d921a5b6d9ef016 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 2 Dec 2026 04:47:59 +0300 Subject: [PATCH 4/4] http_client: Check that the request goes out after the stray events Every check in the stray event test was negative, so it also passed if the client never sent the request once the connect completed. Accept the connection on the listener and check that the request arrives. --- src/test/test_http_client.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/test_http_client.cpp b/src/test/test_http_client.cpp index 4accaf85..3f289092 100644 --- a/src/test/test_http_client.cpp +++ b/src/test/test_http_client.cpp @@ -113,6 +113,13 @@ static void test_stray_event_while_connecting() assert(poll(&pfd, 1, 1000) == 1); fd_cb(cb_fd, EPOLLOUT); assert(!done); + int peer_fd = accept(listen_fd, NULL, NULL); + assert(peer_fd >= 0); + struct pollfd rfd = { .fd = peer_fd, .events = POLLIN }; + assert(poll(&rfd, 1, 1000) == 1); + char buf[4]; + assert(read(peer_fd, buf, 4) == 4 && std::string(buf, 4) == "GET "); + close(peer_fd); http_close(cli); http_destroy(cli); http_context_destroy(ctx); -- 2.47.3