From 907ac7c5468fbeb4f42442db223aebcedbd96215 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 21 Nov 2026 14:57:24 -0500 Subject: [PATCH] Retry a parking backlog on a timer only, and tidy up its tests The wake a worker sent when it started a job only shortened the wait of a backlog from at most 10 ms, and needed an atomic flag and a second enqueue try to be safe. The 10 ms retry, which works with any TaskQueue, now does it alone, and one submit() decides who may enter the queue. The listen retries use the same interval, and the park failure is logged in one place. The zero-timeout test sends its request before the server accepts, which removes its race with the 10 ms floor. --- httplib.h | 68 ++++++++++++++++++++++------------------------------ test/test.cc | 58 ++++++++++++++++++++------------------------ 2 files changed, 55 insertions(+), 71 deletions(-) diff --git a/httplib.h b/httplib.h index e98f8c71..a7a08a49 100644 --- a/httplib.h +++ b/httplib.h @@ -14470,8 +14470,9 @@ struct Server::ParkingState { // The rest belongs to parking_loop(). A parked connection's token is its // address, and `parked` lists them in the order they expire. - // False when the poller cannot watch it, which closes it: unwatched, it - // would only wait for its deadline. + + // Closes it and returns false when the poller cannot watch it: unwatched, + // it would only wait for its deadline. bool park(Connection conn, Clock::time_point deadline) { auto &c = *conn; c.deadline = deadline; @@ -14523,13 +14524,11 @@ struct Server::ParkingState { } parked.clear(); backlog.clear(); - backlog_waiting = false; } std::mutex mutex; std::vector returned; bool closed = false; - std::atomic backlog_waiting{false}; std::list parked; std::list backlog; @@ -16531,29 +16530,37 @@ inline bool Server::parking_loop(TaskQueue &task_queue) { : 0); auto next_idle = idle.count() > 0 ? Clock::now() + idle : never; + auto park = [&](Connection conn, Clock::time_point now) { + if (!st.park(std::move(conn), now + keep_alive)) { + output_error_log(Error::ResourceExhaustion, nullptr); + } + }; auto dispatch = [&](const Connection &conn) { return task_queue.enqueue([this, conn]() { resume_connection(conn); }); }; - // An established connection that finds the queue full waits in the backlog. - // The flag goes up before a second try, so that a worker taking a job in - // between either wakes this loop or leaves room for that try. - auto dispatch_established = [&](const Connection &conn) { - if (dispatch(conn)) { return true; } - st.backlog_waiting = true; - return dispatch(conn); + // The only way into the queue besides the backlog, which nothing overtakes. + // When the queue is full, a new connection is turned away, as accept_loop() + // does, and an established one waits in the backlog, since closing it would + // lose its request. A TaskQueue gives no sign when it has room again, so the + // backlog is retried every check_interval. + auto submit = [&](Connection conn) { + if (st.backlog.empty() && dispatch(conn)) { return; } + if (conn->opened) { + st.backlog.push_back(std::move(conn)); + return; + } + conn->close_idle(); + output_error_log(Error::ResourceExhaustion, nullptr); }; std::vector returned; while (listening && svr_sock_ == listen_sock) { auto now = Clock::now(); if (now >= listen_rearm_at) { - listen_rearm_at = poller.arm(listen_sock, listen_token) - ? never - : now + std::chrono::milliseconds(10); + listen_rearm_at = + poller.arm(listen_sock, listen_token) ? never : now + check_interval; } - // A worker wakes this loop when it takes a job off the queue, but a - // TaskQueue may make room only later, so a backlog is also retried. auto until = (std::min)({st.next_deadline(), listen_rearm_at, next_idle, st.backlog.empty() ? never : now + check_interval, now + std::chrono::seconds(60)}); @@ -16571,9 +16578,7 @@ inline bool Server::parking_loop(TaskQueue &task_queue) { returned.swap(st.returned); } for (auto &conn : returned) { - if (!st.park(std::move(conn), now + keep_alive)) { - output_error_log(Error::ResourceExhaustion, nullptr); - } + park(std::move(conn), now); } returned.clear(); @@ -16591,7 +16596,7 @@ inline bool Server::parking_loop(TaskQueue &task_queue) { if (detail::is_would_block_error()) { break; } if (detail::is_accept_resource_error()) { // Out of descriptors: let the deadlines free some first. - listen_rearm_at = now + std::chrono::milliseconds(10); + listen_rearm_at = now + check_interval; break; } if (detail::is_accept_transient_error()) { continue; } @@ -16601,28 +16606,14 @@ inline bool Server::parking_loop(TaskQueue &task_queue) { // Some platforms make it non-blocking like the listening socket. detail::set_nonblocking(sock, false); prepare_accepted_socket(sock); - if (!st.park(new_keep_alive_connection(sock), now + keep_alive)) { - output_error_log(Error::ResourceExhaustion, nullptr); - } + park(new_keep_alive_connection(sock), now); if (idle.count() > 0) { next_idle = now + idle; } } continue; } - auto conn = st.unpark(token); - if (!conn->opened) { - // A new connection is turned away when the queue is full, as - // accept_loop() does, and may not take room the backlog waits for. - if (!st.backlog.empty() || !dispatch(conn)) { - conn->close_idle(); - output_error_log(Error::ResourceExhaustion, nullptr); - } - } else if (!st.backlog.empty() || !dispatch_established(conn)) { - // Closing an established connection would lose its request. - st.backlog.push_back(std::move(conn)); - } + submit(st.unpark(token)); } - st.backlog_waiting = !st.backlog.empty(); if (fatal) { break; } st.expire(now); @@ -16654,9 +16645,6 @@ Server::new_keep_alive_connection(socket_t sock) { inline void Server::resume_connection(std::shared_ptr conn) { auto &st = *parking_; - // Taking this job off the queue made room for one that waits in the backlog. - if (st.backlog_waiting) { st.poller->wake(); } - auto &c = *conn; auto ret = c.resume(); if (c.turn.park) { @@ -20177,6 +20165,7 @@ inline bool SSLServer::is_valid() const { return ctx_ != nullptr && Server::is_valid(); } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT namespace detail { // Before these, SSL_free_buffers() could free a buffer that a record whose @@ -20191,6 +20180,7 @@ inline bool ssl_free_buffers_is_safe(unsigned long version) { } } // namespace detail +#endif // A connection that keep-alive parking keeps for SSLServer. The worker that // first resumes it runs the handshake, so one that has not sent its diff --git a/test/test.cc b/test/test.cc index 7f26e98a..74567477 100644 --- a/test/test.cc +++ b/test/test.cc @@ -95,9 +95,11 @@ macos_ssl_trust_settings_result(CFArrayRef trust_settings); } // namespace impl #endif } // namespace tls +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT namespace detail { bool ssl_free_buffers_is_safe(unsigned long version); } // namespace detail +#endif } // namespace httplib #endif @@ -34167,10 +34169,14 @@ TEST(PollerFactoryTest, MakePollerWorks) { namespace parking_test { -// Runs `svr` on a free port and stops it when it goes away. +// Runs `svr` on a free port and stops it when it goes away. `before_listen` +// runs once the port is bound and listening, before the server accepts. struct RunningServer { - explicit RunningServer(Server &svr) : svr_(svr) { + explicit RunningServer( + Server &svr, const std::function &before_listen = nullptr) + : svr_(svr) { port = svr_.bind_to_any_port("127.0.0.1"); + if (before_listen) { before_listen(port); } thread = std::thread([this] { svr_.listen_after_bind(); }); svr_.wait_until_ready(); } @@ -34367,27 +34373,16 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) { // One worker and room for one queued task. serve_ok(svr, [&] { return new CountingTaskQueue(1, 1, queued, full); }); - std::mutex m; - std::condition_variable cv; - auto entered = false; - auto released = false; + Gate gate; svr.Get("/block", [&](const Request &, Response &res) { - std::unique_lock lock(m); - entered = true; - cv.notify_all(); - cv.wait_for(lock, std::chrono::seconds(3), [&] { return released; }); + gate.wait(); res.set_content("ok", "text/plain"); }); - auto release = [&] { - std::lock_guard guard(m); - released = true; - cv.notify_all(); - }; RunningServer running(svr); // Before the server stops, so that a failed run does not wait for the // handler's timeout. - auto release_on_exit = detail::scope_exit([&] { release(); }); + auto open_gate = detail::scope_exit([&] { gate.open(); }); Sockets socks; auto a = socks.connect(running.port); @@ -34396,12 +34391,7 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) { // B keeps the only worker busy. auto b = socks.connect(running.port); ASSERT_TRUE(send_get(b, "/block")); - ASSERT_TRUE(wait_until( - [&] { - std::lock_guard guard(m); - return entered; - }, - 2000)); + ASSERT_TRUE(wait_until([&] { return gate.entered() == 1; }, 2000)); // A new connection C fills the queue. auto c = socks.connect(running.port); @@ -34413,7 +34403,7 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) { ASSERT_TRUE(send_get(a)); ASSERT_TRUE(wait_until([&] { return full >= 1; }, 2000)); - release(); + gate.open(); EXPECT_TRUE(receives_ok(b, 2000)); EXPECT_TRUE(receives_ok(c, 2000)); EXPECT_TRUE(receives_ok(a, 2000)); @@ -34465,6 +34455,7 @@ TEST(KeepAliveParkingTest, BacklogIsRetriedOnceTheQueueHasRoom) { queue->pause_after_ms = 200; first.open(); EXPECT_TRUE(wait_until([&] { return queued == 5; }, 1000)); + queue->pause_after_ms = 0; second.open(); EXPECT_TRUE(receives_ok(a, 2000)); @@ -34475,16 +34466,17 @@ TEST(KeepAliveParkingTest, ZeroKeepAliveTimeoutServesTheFirstRequest) { Server svr; serve_ok(svr, threads(1)); svr.set_keep_alive_timeout(0); - RunningServer running(svr); - Sockets socks; - // The request has to arrive within the 10 ms floor, which a busy machine - // can miss once. Without the floor every attempt fails. - auto served = false; - for (int i = 0; i < 5 && !served; i++) { - served = get_ok(socks.connect(running.port), 1000); - } - EXPECT_TRUE(served); + // The request is on the socket before the server accepts the connection, + // so it does not race the 10 ms floor. Without the floor the connection is + // closed unread. + Sockets socks; + auto sock = INVALID_SOCKET; + RunningServer running(svr, [&](int port) { + sock = socks.connect(port); + send_get(sock); + }); + EXPECT_TRUE(receives_ok(sock, 1000)); } TEST(KeepAliveParkingTest, StopClosesParkedConnectionsPromptly) { @@ -34548,6 +34540,7 @@ bool tls_get_ok(Client &cli) { } // namespace parking_test +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT TEST(KeepAliveParkingTest, SslFreeBuffersOnlyWhereTheCveIsFixed) { struct { unsigned long version; @@ -34563,6 +34556,7 @@ TEST(KeepAliveParkingTest, SslFreeBuffersOnlyWhereTheCveIsFixed) { << std::hex << c.version; } } +#endif TEST(KeepAliveParkingTest, IdleTlsConnectionsDoNotHoldTheWorkers) { using namespace parking_test;