Stop waiting for a TLS handshake once the server is stopping

SSLServer::process_and_close_socket() waits for the handshake of an
accepted connection for up to the read timeout, and that wait ignored
stop(). A client that connects and never sends a ClientHello kept
listen() from returning for the full timeout (4.7 seconds with the
default 5-second read timeout).

Give tls::accept_nonblocking() the same `interrupted` parameter that
connect_nonblocking() already has, and pass `is_stopping()` from
start_session(). Keep-alive parking was not affected, since it waits for
the ClientHello in its event loop, which stop() wakes.
This commit is contained in:
yhirose authored and yhirose committed 2026-11-24 23:58:31 -05:00
1 parent baf27c5993
commit 4995333ef2
2 files changed
+64 -15

No files matched your search

+25 -12
View File
@@ -5647,7 +5647,8 @@ bool connect_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
time_t timeout_usec, TlsError *err,
const std::function<bool()> &interrupted = nullptr);
bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
time_t timeout_usec, TlsError *err);
time_t timeout_usec, TlsError *err,
const std::function<bool()> &interrupted = nullptr);
// I/O (non-blocking capable)
ssize_t read(session_t session, void *buf, size_t len, TlsError &err);
@@ -20278,8 +20279,9 @@ inline tls::session_t SSLServer::start_session(socket_t sock) {
std::lock_guard<std::mutex> guard(ctx_mutex_);
if (--handshakes_in_progress_ == 0) { ctx_cv_.notify_all(); }
});
accepted = accept_nonblocking(session, sock, read_timeout_sec_,
read_timeout_usec_, &tls_err);
accepted =
accept_nonblocking(session, sock, read_timeout_sec_, read_timeout_usec_,
&tls_err, [this]() { return is_stopping(); });
}
if (!accepted) {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@@ -21694,7 +21696,8 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
inline bool accept_nonblocking(session_t session, socket_t sock,
time_t timeout_sec, time_t timeout_usec,
TlsError *err) {
TlsError *err,
const std::function<bool()> &interrupted) {
if (!session) {
if (err) { err->code = ErrorCode::Fatal; }
return false;
@@ -21719,10 +21722,14 @@ inline bool accept_nonblocking(session_t session, socket_t sock,
auto ssl_err = SSL_get_error(ssl, res);
switch (ssl_err) {
case SSL_ERROR_WANT_READ:
if (detail::select_until(sock, POLLIN, deadline) > 0) { continue; }
if (detail::select_until(sock, POLLIN, deadline, interrupted) > 0) {
continue;
}
break;
case SSL_ERROR_WANT_WRITE:
if (detail::select_until(sock, POLLOUT, deadline) > 0) { continue; }
if (detail::select_until(sock, POLLOUT, deadline, interrupted) > 0) {
continue;
}
break;
default: break;
}
@@ -23049,13 +23056,14 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
inline bool accept_nonblocking(session_t session, socket_t sock,
time_t timeout_sec, time_t timeout_usec,
TlsError *err) {
TlsError *err,
const std::function<bool()> &interrupted) {
// A failed handshake leaves its SNI behind, so start without it
impl::mbedpending_sni().clear();
// Same implementation as connect for Mbed TLS
bool result =
connect_nonblocking(session, sock, timeout_sec, timeout_usec, err);
bool result = connect_nonblocking(session, sock, timeout_sec, timeout_usec,
err, interrupted);
// After successful handshake, capture SNI from thread-local storage
if (result && session) {
@@ -24230,7 +24238,8 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
inline bool accept_nonblocking(session_t session, socket_t sock,
time_t timeout_sec, time_t timeout_usec,
TlsError *err) {
TlsError *err,
const std::function<bool()> &interrupted) {
if (!session) {
if (err) { err->code = ErrorCode::Fatal; }
return false;
@@ -24248,9 +24257,13 @@ inline bool accept_nonblocking(session_t session, socket_t sock,
while ((ret = wolfSSL_accept(wsession->ssl)) != SSL_SUCCESS) {
int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
if (ssl_error == SSL_ERROR_WANT_READ) {
if (detail::select_until(sock, POLLIN, deadline) > 0) { continue; }
if (detail::select_until(sock, POLLIN, deadline, interrupted) > 0) {
continue;
}
} else if (ssl_error == SSL_ERROR_WANT_WRITE) {
if (detail::select_until(sock, POLLOUT, deadline) > 0) { continue; }
if (detail::select_until(sock, POLLOUT, deadline, interrupted) > 0) {
continue;
}
}
// Error or timeout
+39 -3
View File
@@ -84,7 +84,8 @@ bool connect_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
time_t timeout_usec, TlsError *err,
const std::function<bool()> &interrupted);
bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
time_t timeout_usec, TlsError *err);
time_t timeout_usec, TlsError *err,
const std::function<bool()> &interrupted);
ssize_t read(session_t session, void *buf, size_t len, TlsError &err);
ssize_t write(session_t session, const void *buf, size_t len, TlsError &err);
void shutdown(session_t session, bool graceful);
@@ -5968,6 +5969,40 @@ TEST(SSLClientServerTest, UpdateCertsPemWaitsForHandshakeInProgress) {
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
}
TEST(SSLClientServerTest, StopDoesNotWaitForHandshakeInProgress) {
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
ASSERT_TRUE(svr.is_valid());
// Keep-alive parking would wait for the ClientHello without a handshake.
svr.set_keep_alive_parking(false);
svr.set_read_timeout(10, 0);
thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
auto se = detail::scope_exit([&] {
svr.stop();
if (t.joinable()) { t.join(); }
});
svr.wait_until_ready();
// A connection that sends no ClientHello keeps its handshake in progress,
// once the server has had a moment to start it.
auto error = Error::Success;
auto sock = detail::create_client_socket(
HOST, "", PORT, AF_UNSPEC, false, false, nullptr,
/*connection_timeout_sec=*/5, 0,
/*read_timeout_sec=*/5, 0,
/*write_timeout_sec=*/5, 0, std::string(), error);
ASSERT_NE(INVALID_SOCKET, sock);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
auto start = std::chrono::steady_clock::now();
svr.stop();
t.join();
auto elapsed = std::chrono::steady_clock::now() - start;
detail::close_socket(sock);
EXPECT_LT(elapsed, std::chrono::seconds(3));
}
#endif
TEST(ErrorHandlerTest, ContentLength) {
@@ -13802,7 +13837,8 @@ static void expect_no_reuse_with_unread_data(bool use_tls, bool same_write) {
if (use_tls) {
peer.session = tls::create_session(ctx, peer.sock);
tls::TlsError err;
if (!tls::accept_nonblocking(peer.session, peer.sock, 5, 0, &err)) {
if (!tls::accept_nonblocking(peer.session, peer.sock, 5, 0, &err,
nullptr)) {
close_peer(peer);
return Peer();
}
@@ -25182,7 +25218,7 @@ static void with_single_tls_response(const std::string &response,
nullptr);
auto session = tls::create_session(ctx, cli);
tls::TlsError err;
if (tls::accept_nonblocking(session, cli, 5, 0, &err)) {
if (tls::accept_nonblocking(session, cli, 5, 0, &err, nullptr)) {
// The request head first, so that closing the socket sends a FIN
// rather than an RST for the unread bytes.
std::string req;