From 42fc5bdda4d44c622a585114dd71b65452abefbd Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Sun, 29 Nov 2026 13:03:56 +0300 Subject: [PATCH] http_client: don't arm the websocket timeout after a failed connect start_ws_connection() armed its timer after resolve_and_connect() even when the connection had already failed inside it and its callback had run. Nothing cleared that timer: when it fired, it failed the client's next request with "Websocket connection timed out". Arm it only while the connection is in flight, as send_request() does. Also test the case where the failed request's callback sends the next request on the same client. --- src/client/http_client.cpp | 3 ++- src/test/test_http_client.cpp | 51 ++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/client/http_client.cpp b/src/client/http_client.cpp index c28c666f..9fe6a85e 100644 --- a/src/client/http_client.cpp +++ b/src/client/http_client.cpp @@ -582,7 +582,8 @@ void http_co_t::start_ws_connection() { stackin(); resolve_and_connect(); - if (request_timeout > 0) + // As in send_request(): skip it if the connection has already failed + if (request_timeout > 0 && response_callback && timeout_id < 0) { timeout_id = tfd->set_timer(request_timeout, false, [this](int timer_id) { diff --git a/src/test/test_http_client.cpp b/src/test/test_http_client.cpp index 5d3e9366..18b8b556 100644 --- a/src/test/test_http_client.cpp +++ b/src/test/test_http_client.cpp @@ -10,13 +10,16 @@ static const std::string request = "GET / HTTP/1.1\r\nHost: test\r\nConnection: keep-alive\r\n\r\n"; -// A request that fails inside http_request() must not leave its timeout timer armed. +enum { FAIL_REQUEST, FAIL_REQUEST_NEXT_IN_CB, FAIL_WEBSOCKET }; + +// A request or websocket that fails inside http_request() or open_websocket() must not +// leave its timeout timer armed, also when its callback sends the next request. // TCP connect() to a multicast address fails at once with ENETUNREACH on Linux. // The leftover timer used to fail the client's next request with a false timeout // (or run on a freed client when the failed request's callback closed it) -static void test_sync_error_leaves_no_timer() +static void test_sync_error_leaves_no_timer(int mode) { - printf("test_sync_error_leaves_no_timer\n"); + printf("test_sync_error_leaves_no_timer %d\n", mode); // Timers fire only on tick(), sockets are never polled timerfd_manager_t tfd(nullptr); tfd.set_fd_handler = [](int, bool, std::function) {}; @@ -25,32 +28,48 @@ static void test_sync_error_leaves_no_timer() assert(ctx && err == ""); http_co_t *cli = http_init(ctx); - std::string err1; - http_request(cli, "224.0.0.1:1", request, { .timeout = 1000, .keepalive = true }, [&](http_message_t *msg) + // Stays connecting: only its own 5 s timeout can end it + std::string err1, err2, err3; + auto send_next = [&](std::string & err_out) + { + http_request(cli, "127.0.0.1:1", request, { .timeout = 5000, .keepalive = true }, [&](http_message_t *msg) + { + err_out = msg->error; + }); + }; + auto on_fail = [&](http_message_t *msg) { err1 = msg->error; - }); + if (mode == FAIL_REQUEST_NEXT_IN_CB) + send_next(err2); + }; + if (mode == FAIL_WEBSOCKET) + open_websocket(cli, "224.0.0.1:1", "test", "/", { .timeout = 1000 }, on_fail); + else + http_request(cli, "224.0.0.1:1", request, { .timeout = 1000, .keepalive = true }, on_fail); assert(err1 != ""); - - // Stays connecting: only its own 5 s timeout can end it - std::string err2; - http_request(cli, "127.0.0.1:1", request, { .timeout = 5000, .keepalive = true }, [&](http_message_t *msg) - { - err2 = msg->error; - }); + if (mode != FAIL_REQUEST_NEXT_IN_CB) + send_next(err2); tfd.tick((timespec){ .tv_sec = 2 }); assert(err2 == ""); + // End the second request early: a second timer armed for it would fail the third one + http_close(cli); + send_next(err3); tfd.tick((timespec){ .tv_sec = 4 }); - assert(err2 == "HTTP request timed out"); + assert(err3 == ""); + tfd.tick((timespec){ .tv_sec = 2 }); + assert(err3 == "HTTP request timed out"); http_close(cli); http_destroy(cli); http_context_destroy(ctx); - printf("test_sync_error_leaves_no_timer passed\n"); + printf("test_sync_error_leaves_no_timer %d passed\n", mode); } int main(int narg, char *args[]) { - test_sync_error_leaves_no_timer(); + 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); return 0; } -- 2.47.3