diff --git a/src/client/http_client.cpp b/src/client/http_client.cpp index 4f544def..c28c666f 100644 --- a/src/client/http_client.cpp +++ b/src/client/http_client.cpp @@ -436,8 +436,10 @@ void http_co_t::send_request(const std::string & host, const std::string & reque resolve_and_connect(); } // Do it _after_ state assignment because set_timer() can actually trigger - // other timers and requests (reenterability is our friend) - if (request_timeout > 0) + // other timers and requests (reenterability is our friend). + // Skip it if the request has already failed: its callback may have closed + // the client or started the next request, which armed its own timer + if (request_timeout > 0 && this->response_callback && timeout_id < 0) { timeout_id = tfd->set_timer(request_timeout, false, [this](int timer_id) { diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index a79fc4e5..30e47113 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -42,6 +42,20 @@ add_dependencies(build_tests test_msgr_stop) add_test(NAME test_msgr_stop COMMAND test_msgr_stop) set_tests_properties(test_msgr_stop PROPERTIES SKIP_RETURN_CODE 77) +# test_http_client +add_executable(test_http_client + EXCLUDE_FROM_ALL + test_http_client.cpp +) +target_link_libraries(test_http_client + vitastor_net + ${LIBURING_LIBRARIES} + ${IBVERBS_LIBRARIES} + ${RDMACM_LIBRARIES} +) +add_dependencies(build_tests test_http_client) +add_test(NAME test_http_client COMMAND test_http_client) + # test_allocator add_executable(test_allocator EXCLUDE_FROM_ALL test_allocator.cpp) target_link_libraries(test_allocator util) diff --git a/src/test/test_http_client.cpp b/src/test/test_http_client.cpp new file mode 100644 index 00000000..7fa30807 --- /dev/null +++ b/src/test/test_http_client.cpp @@ -0,0 +1,55 @@ +// Copyright (c) Vitaliy Filippov, 2019+ +// License: VNPL-1.1 (see README.md for details) + +#undef NDEBUG +#include +#include + +#include "http_client.h" +#include "timerfd_manager.h" + +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. +// 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() +{ + printf("test_sync_error_leaves_no_timer\n"); + // Timers fire only on tick(), sockets are never polled + timerfd_manager_t tfd(nullptr); + tfd.set_fd_handler = [](int, bool, std::function) {}; + std::string err; + http_context_t *ctx = http_context_init(&tfd, "", "", "", false, err); + 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) + { + err1 = msg->error; + }); + 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; + }); + tfd.tick((timespec){ .tv_sec = 2 }); + assert(err2 == ""); + tfd.tick((timespec){ .tv_sec = 4 }); + assert(err2 == "HTTP request timed out"); + + http_close(cli); + http_context_destroy(ctx); + printf("test_sync_error_leaves_no_timer passed\n"); +} + +int main(int narg, char *args[]) +{ + test_sync_error_leaves_no_timer(); + return 0; +}