Merge bitcoin/bitcoin#34577: http: fix submission during shutdown race

726b3663cc http: properly respond to HTTP request during shutdown (furszy)
59d24bd5dd threadpool: make Submit return Expected instead of throwing (furszy)

Pull request description:

  Fixes #34573.

  As mentioned in https://github.com/bitcoin/bitcoin/issues/34573#issuecomment-3891596958, the ThreadPool PR (#33689) revealed an existing issue.

  Before that PR, we were returning an incorrect error "Request rejected because http work queue depth exceeded" during shutdown for unhandled requests (we were not differentiating between "queue depth exceeded" and "server interrupted" errors). Now, with the ThreadPool inclusion, we return the proper error but we don't handle it properly.

  This PR improves exactly that. Handling the missing error and properly returning it to the user.

  The race can be reproduced as follows:

  1) The server receives an http request.
  2) Processing of the request is delayed, and shutdown is triggered in the meantime.
  3) During shutdown, the libevent callback is unregistered and the threadpool interrupted.
  4) The delayed request (step 2) resumes and tries to submit a task to the now-interrupted server.

  Reproduction test can be found https://github.com/bitcoin/bitcoin/pull/34577#issuecomment-3902672521.

  Also, to prevent this kind of issue from happening again, this PR changes task submission
  to return the error as part of the function's return value using `util::Expected` instead of
  throwing the exception. Unlike exceptions, which require extra try-catch blocks and can be
  ignored, returning `Expected` forces callers to explicitly handle failures, and attributes
  like `[[nodiscard]]` allow us catch unhandled ones at compile time.

ACKs for top commit:
  achow101:
    ACK 726b3663cc
  sedited:
    ACK 726b3663cc
  pinheadmz:
    re-ACK 726b3663cc
  andrewtoth:
    ACK 726b3663cc
  hodlinator:
    re-ACK 726b3663cc

Tree-SHA512: ef026e299adde1148c9fc575e7d937e957bf0ddedfc1cf081941b568736417c2eefcd8bc8c8aea795d7347040ed05da4371bddcdbda7d385e04bf4dc8d875780
This commit is contained in:
Ava Chow
2026-02-19 12:41:12 -08:00
4 changed files with 73 additions and 38 deletions

View File

@@ -211,7 +211,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
}
}
}
auto hreq{std::make_unique<HTTPRequest>(req, *static_cast<const util::SignalInterrupt*>(arg))};
auto hreq{std::make_shared<HTTPRequest>(req, *static_cast<const util::SignalInterrupt*>(arg))};
// Early address-based allow check
if (!ClientAllowed(hreq->GetPeer())) {
@@ -258,7 +258,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
return;
}
auto item = [req = std::move(hreq), in_path = std::move(path), fn = i->handler]() {
auto item = [req = hreq, in_path = std::move(path), fn = i->handler]() {
std::string err_msg;
try {
fn(req.get(), in_path);
@@ -276,7 +276,13 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
req->WriteReply(HTTP_INTERNAL_SERVER_ERROR, err_msg);
};
[[maybe_unused]] auto _{g_threadpool_http.Submit(std::move(item))};
if (auto res = g_threadpool_http.Submit(std::move(item)); !res.has_value()) {
Assume(hreq.use_count() == 1); // ensure request will be deleted
// Both SubmitError::Inactive and SubmitError::Interrupted mean shutdown
LogWarning("HTTP request rejected during server shutdown: '%s'", SubmitErrorString(res.error()));
hreq->WriteReply(HTTP_SERVICE_UNAVAILABLE, "Request rejected during server shutdown");
return;
}
} else {
hreq->WriteReply(HTTP_NOT_FOUND);
}