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.
This commit is contained in:
yhirose committed 2026-11-21 14:57:24 -05:00
1 parent dac2f48003
commit 907ac7c546
2 files changed
+55 -71

No files matched your search

+29 -39
View File
@@ -14470,8 +14470,9 @@ struct Server::ParkingState {
// The rest belongs to parking_loop(). A parked connection's token is its // The rest belongs to parking_loop(). A parked connection's token is its
// address, and `parked` lists them in the order they expire. // 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) { bool park(Connection conn, Clock::time_point deadline) {
auto &c = *conn; auto &c = *conn;
c.deadline = deadline; c.deadline = deadline;
@@ -14523,13 +14524,11 @@ struct Server::ParkingState {
} }
parked.clear(); parked.clear();
backlog.clear(); backlog.clear();
backlog_waiting = false;
} }
std::mutex mutex; std::mutex mutex;
std::vector<Connection> returned; std::vector<Connection> returned;
bool closed = false; bool closed = false;
std::atomic<bool> backlog_waiting{false};
std::list<Connection> parked; std::list<Connection> parked;
std::list<Connection> backlog; std::list<Connection> backlog;
@@ -16531,29 +16530,37 @@ inline bool Server::parking_loop(TaskQueue &task_queue) {
: 0); : 0);
auto next_idle = idle.count() > 0 ? Clock::now() + idle : never; 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) { auto dispatch = [&](const Connection &conn) {
return task_queue.enqueue([this, conn]() { resume_connection(conn); }); return task_queue.enqueue([this, conn]() { resume_connection(conn); });
}; };
// An established connection that finds the queue full waits in the backlog. // The only way into the queue besides the backlog, which nothing overtakes.
// The flag goes up before a second try, so that a worker taking a job in // When the queue is full, a new connection is turned away, as accept_loop()
// between either wakes this loop or leaves room for that try. // does, and an established one waits in the backlog, since closing it would
auto dispatch_established = [&](const Connection &conn) { // lose its request. A TaskQueue gives no sign when it has room again, so the
if (dispatch(conn)) { return true; } // backlog is retried every check_interval.
st.backlog_waiting = true; auto submit = [&](Connection conn) {
return dispatch(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<Connection> returned; std::vector<Connection> returned;
while (listening && svr_sock_ == listen_sock) { while (listening && svr_sock_ == listen_sock) {
auto now = Clock::now(); auto now = Clock::now();
if (now >= listen_rearm_at) { if (now >= listen_rearm_at) {
listen_rearm_at = poller.arm(listen_sock, listen_token) listen_rearm_at =
? never poller.arm(listen_sock, listen_token) ? never : now + check_interval;
: now + std::chrono::milliseconds(10);
} }
// 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, auto until = (std::min)({st.next_deadline(), listen_rearm_at, next_idle,
st.backlog.empty() ? never : now + check_interval, st.backlog.empty() ? never : now + check_interval,
now + std::chrono::seconds(60)}); now + std::chrono::seconds(60)});
@@ -16571,9 +16578,7 @@ inline bool Server::parking_loop(TaskQueue &task_queue) {
returned.swap(st.returned); returned.swap(st.returned);
} }
for (auto &conn : returned) { for (auto &conn : returned) {
if (!st.park(std::move(conn), now + keep_alive)) { park(std::move(conn), now);
output_error_log(Error::ResourceExhaustion, nullptr);
}
} }
returned.clear(); returned.clear();
@@ -16591,7 +16596,7 @@ inline bool Server::parking_loop(TaskQueue &task_queue) {
if (detail::is_would_block_error()) { break; } if (detail::is_would_block_error()) { break; }
if (detail::is_accept_resource_error()) { if (detail::is_accept_resource_error()) {
// Out of descriptors: let the deadlines free some first. // Out of descriptors: let the deadlines free some first.
listen_rearm_at = now + std::chrono::milliseconds(10); listen_rearm_at = now + check_interval;
break; break;
} }
if (detail::is_accept_transient_error()) { continue; } 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. // Some platforms make it non-blocking like the listening socket.
detail::set_nonblocking(sock, false); detail::set_nonblocking(sock, false);
prepare_accepted_socket(sock); prepare_accepted_socket(sock);
if (!st.park(new_keep_alive_connection(sock), now + keep_alive)) { park(new_keep_alive_connection(sock), now);
output_error_log(Error::ResourceExhaustion, nullptr);
}
if (idle.count() > 0) { next_idle = now + idle; } if (idle.count() > 0) { next_idle = now + idle; }
} }
continue; continue;
} }
auto conn = st.unpark(token); submit(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));
}
}
st.backlog_waiting = !st.backlog.empty();
if (fatal) { break; } if (fatal) { break; }
st.expire(now); st.expire(now);
@@ -16654,9 +16645,6 @@ Server::new_keep_alive_connection(socket_t sock) {
inline void inline void
Server::resume_connection(std::shared_ptr<KeepAliveConnection> conn) { Server::resume_connection(std::shared_ptr<KeepAliveConnection> conn) {
auto &st = *parking_; 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 &c = *conn;
auto ret = c.resume(); auto ret = c.resume();
if (c.turn.park) { if (c.turn.park) {
@@ -20177,6 +20165,7 @@ inline bool SSLServer::is_valid() const {
return ctx_ != nullptr && Server::is_valid(); return ctx_ != nullptr && Server::is_valid();
} }
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
namespace detail { namespace detail {
// Before these, SSL_free_buffers() could free a buffer that a record whose // 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 } // namespace detail
#endif
// A connection that keep-alive parking keeps for SSLServer. The worker that // 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 // first resumes it runs the handshake, so one that has not sent its
+26 -32
View File
@@ -95,9 +95,11 @@ macos_ssl_trust_settings_result(CFArrayRef trust_settings);
} // namespace impl } // namespace impl
#endif #endif
} // namespace tls } // namespace tls
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
namespace detail { namespace detail {
bool ssl_free_buffers_is_safe(unsigned long version); bool ssl_free_buffers_is_safe(unsigned long version);
} // namespace detail } // namespace detail
#endif
} // namespace httplib } // namespace httplib
#endif #endif
@@ -34167,10 +34169,14 @@ TEST(PollerFactoryTest, MakePollerWorks) {
namespace parking_test { 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 { struct RunningServer {
explicit RunningServer(Server &svr) : svr_(svr) { explicit RunningServer(
Server &svr, const std::function<void(int)> &before_listen = nullptr)
: svr_(svr) {
port = svr_.bind_to_any_port("127.0.0.1"); port = svr_.bind_to_any_port("127.0.0.1");
if (before_listen) { before_listen(port); }
thread = std::thread([this] { svr_.listen_after_bind(); }); thread = std::thread([this] { svr_.listen_after_bind(); });
svr_.wait_until_ready(); svr_.wait_until_ready();
} }
@@ -34367,27 +34373,16 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) {
// One worker and room for one queued task. // One worker and room for one queued task.
serve_ok(svr, [&] { return new CountingTaskQueue(1, 1, queued, full); }); serve_ok(svr, [&] { return new CountingTaskQueue(1, 1, queued, full); });
std::mutex m; Gate gate;
std::condition_variable cv;
auto entered = false;
auto released = false;
svr.Get("/block", [&](const Request &, Response &res) { svr.Get("/block", [&](const Request &, Response &res) {
std::unique_lock<std::mutex> lock(m); gate.wait();
entered = true;
cv.notify_all();
cv.wait_for(lock, std::chrono::seconds(3), [&] { return released; });
res.set_content("ok", "text/plain"); res.set_content("ok", "text/plain");
}); });
auto release = [&] {
std::lock_guard<std::mutex> guard(m);
released = true;
cv.notify_all();
};
RunningServer running(svr); RunningServer running(svr);
// Before the server stops, so that a failed run does not wait for the // Before the server stops, so that a failed run does not wait for the
// handler's timeout. // handler's timeout.
auto release_on_exit = detail::scope_exit([&] { release(); }); auto open_gate = detail::scope_exit([&] { gate.open(); });
Sockets socks; Sockets socks;
auto a = socks.connect(running.port); auto a = socks.connect(running.port);
@@ -34396,12 +34391,7 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) {
// B keeps the only worker busy. // B keeps the only worker busy.
auto b = socks.connect(running.port); auto b = socks.connect(running.port);
ASSERT_TRUE(send_get(b, "/block")); ASSERT_TRUE(send_get(b, "/block"));
ASSERT_TRUE(wait_until( ASSERT_TRUE(wait_until([&] { return gate.entered() == 1; }, 2000));
[&] {
std::lock_guard<std::mutex> guard(m);
return entered;
},
2000));
// A new connection C fills the queue. // A new connection C fills the queue.
auto c = socks.connect(running.port); auto c = socks.connect(running.port);
@@ -34413,7 +34403,7 @@ TEST(KeepAliveParkingTest, EstablishedConnectionWaitsForAFullQueue) {
ASSERT_TRUE(send_get(a)); ASSERT_TRUE(send_get(a));
ASSERT_TRUE(wait_until([&] { return full >= 1; }, 2000)); ASSERT_TRUE(wait_until([&] { return full >= 1; }, 2000));
release(); gate.open();
EXPECT_TRUE(receives_ok(b, 2000)); EXPECT_TRUE(receives_ok(b, 2000));
EXPECT_TRUE(receives_ok(c, 2000)); EXPECT_TRUE(receives_ok(c, 2000));
EXPECT_TRUE(receives_ok(a, 2000)); EXPECT_TRUE(receives_ok(a, 2000));
@@ -34465,6 +34455,7 @@ TEST(KeepAliveParkingTest, BacklogIsRetriedOnceTheQueueHasRoom) {
queue->pause_after_ms = 200; queue->pause_after_ms = 200;
first.open(); first.open();
EXPECT_TRUE(wait_until([&] { return queued == 5; }, 1000)); EXPECT_TRUE(wait_until([&] { return queued == 5; }, 1000));
queue->pause_after_ms = 0;
second.open(); second.open();
EXPECT_TRUE(receives_ok(a, 2000)); EXPECT_TRUE(receives_ok(a, 2000));
@@ -34475,16 +34466,17 @@ TEST(KeepAliveParkingTest, ZeroKeepAliveTimeoutServesTheFirstRequest) {
Server svr; Server svr;
serve_ok(svr, threads(1)); serve_ok(svr, threads(1));
svr.set_keep_alive_timeout(0); 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 // The request is on the socket before the server accepts the connection,
// can miss once. Without the floor every attempt fails. // so it does not race the 10 ms floor. Without the floor the connection is
auto served = false; // closed unread.
for (int i = 0; i < 5 && !served; i++) { Sockets socks;
served = get_ok(socks.connect(running.port), 1000); auto sock = INVALID_SOCKET;
} RunningServer running(svr, [&](int port) {
EXPECT_TRUE(served); sock = socks.connect(port);
send_get(sock);
});
EXPECT_TRUE(receives_ok(sock, 1000));
} }
TEST(KeepAliveParkingTest, StopClosesParkedConnectionsPromptly) { TEST(KeepAliveParkingTest, StopClosesParkedConnectionsPromptly) {
@@ -34548,6 +34540,7 @@ bool tls_get_ok(Client &cli) {
} // namespace parking_test } // namespace parking_test
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
TEST(KeepAliveParkingTest, SslFreeBuffersOnlyWhereTheCveIsFixed) { TEST(KeepAliveParkingTest, SslFreeBuffersOnlyWhereTheCveIsFixed) {
struct { struct {
unsigned long version; unsigned long version;
@@ -34563,6 +34556,7 @@ TEST(KeepAliveParkingTest, SslFreeBuffersOnlyWhereTheCveIsFixed) {
<< std::hex << c.version; << std::hex << c.version;
} }
} }
#endif
TEST(KeepAliveParkingTest, IdleTlsConnectionsDoNotHoldTheWorkers) { TEST(KeepAliveParkingTest, IdleTlsConnectionsDoNotHoldTheWorkers) {
using namespace parking_test; using namespace parking_test;