Net: actually enforce Request::timeout on HTTP socket I/O

Request::timeout was declared but never used — every sync Asio
operation (TLS handshake, read, write) blocked without bound, so a
stalled peer hung the calling thread indefinitely (observed: a stalled
CDN download blocking a host's shutdown join forever).

SO_RCVTIMEO cannot fix this: Asio's sync emulation treats the
resulting EAGAIN as would-block and poll()s with no deadline. Instead
a per-request DeadlineGuard watchdog ::shutdown()s the descriptor on
expiry, which wakes any blocked sync op with EOF and fails the
request. Declaration order in send() keeps the descriptor owners alive
past the guard. Resolution and connect() remain on kernel defaults.

Regression test: a stalled localhost server is bounded at ~1s.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018yj92cCihyaYtf4gKCT3nv
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-07-12 00:20:49 +03:00
1 parent a06f639393
commit 9a54f15e7e
2 files changed
+152 -12

No files matched your search

+54
View File
@@ -110,3 +110,57 @@ Void::Testing::Test http_client_connection_refused_is_error()
const auto resp = client.get("http://127.0.0.1:1/");
Void::Testing::expect(!resp.has_value());
}
Void::Testing::Test http_client_timeout_bounds_a_stalled_server()
{
// A server that accepts and then never responds. Without an enforced
// Request::timeout the sync read blocks forever (Asio's sync emulation
// poll()s on would-block with no deadline).
struct StallServer {
int fd = -1;
std::uint16_t port = 0;
int conn = -1;
std::thread worker;
StallServer()
{
fd = ::socket(AF_INET, SOCK_STREAM, 0);
int yes = 1;
::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = 0;
::bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
socklen_t len = sizeof(addr);
::getsockname(fd, reinterpret_cast<sockaddr*>(&addr), &len);
port = ntohs(addr.sin_port);
::listen(fd, 1);
worker = std::thread([this] {
conn = ::accept(fd, nullptr, nullptr);
// Never write a byte; hold the connection open.
});
}
~StallServer()
{
if (conn >= 0) ::close(conn);
if (worker.joinable()) worker.join();
if (fd >= 0) ::close(fd);
}
};
StallServer server;
Void::Net::HttpClient client;
Void::Net::Request req;
req.url = "http://127.0.0.1:" + std::to_string(server.port) + "/stall";
req.timeout = std::chrono::seconds{1};
const auto start = std::chrono::steady_clock::now();
const auto resp = client.send(req);
const auto elapsed = std::chrono::steady_clock::now() - start;
Void::Testing::expect(!resp.has_value());
// Well under the old "forever": the deadline tripped around 1s.
Void::Testing::expect(elapsed < std::chrono::seconds{5});
}