http: Add logged request ids

Could aid debugging when an HTTP request gets stuck at shutdown.
This commit is contained in:
Hodlinator 2025-02-21 16:01:02 +01:00
parent a00dfd2046
commit 24558c2cf1
No known key found for this signature in database
3 changed files with 15 additions and 9 deletions

View file

@ -258,6 +258,8 @@ std::string RequestMethodString(HTTPRequest::RequestMethod m)
assert(false); assert(false);
} }
static std::atomic_uint32_t g_request_id{0};
/** HTTP request callback */ /** HTTP request callback */
static void http_request_cb(struct evhttp_request* req, void* arg) static void http_request_cb(struct evhttp_request* req, void* arg)
{ {
@ -284,7 +286,8 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
} }
} }
} }
auto hreq{std::make_unique<HTTPRequest>(req, *static_cast<const util::SignalInterrupt*>(arg))}; const uint32_t request_id = g_request_id++;
auto hreq{std::make_unique<HTTPRequest>(req, *static_cast<const util::SignalInterrupt*>(arg), request_id, /*replySent=*/false)};
// Early address-based allow check // Early address-based allow check
if (!ClientAllowed(hreq->GetPeer())) { if (!ClientAllowed(hreq->GetPeer())) {
@ -302,8 +305,8 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
return; return;
} }
LogDebug(BCLog::HTTP, "Received a %s request for %s from %s\n", LogDebug(BCLog::HTTP, "Received a %s request for %s from %s, id: %d\n",
RequestMethodString(hreq->GetRequestMethod()), SanitizeString(hreq->GetURI(), SAFE_CHARS_URI).substr(0, 100), hreq->GetPeer().ToStringAddrPort()); RequestMethodString(hreq->GetRequestMethod()), SanitizeString(hreq->GetURI(), SAFE_CHARS_URI).substr(0, 100), hreq->GetPeer().ToStringAddrPort(), request_id);
// Find registered handler for prefix // Find registered handler for prefix
std::string strURI = hreq->GetURI(); std::string strURI = hreq->GetURI();
@ -609,8 +612,8 @@ void HTTPEvent::trigger(struct timeval* tv)
else else
evtimer_add(ev, tv); // trigger after timeval passed evtimer_add(ev, tv); // trigger after timeval passed
} }
HTTPRequest::HTTPRequest(struct evhttp_request* _req, const util::SignalInterrupt& interrupt, bool _replySent) HTTPRequest::HTTPRequest(struct evhttp_request* _req, const util::SignalInterrupt& interrupt, uint32_t id, bool _replySent)
: req(_req), m_interrupt(interrupt), replySent(_replySent) : req(_req), m_interrupt(interrupt), m_id(id), replySent(_replySent)
{ {
} }
@ -671,7 +674,7 @@ void HTTPRequest::WriteReply(int nStatus, std::span<const std::byte> reply)
{ {
assert(!replySent && req); assert(!replySent && req);
if (m_interrupt) { if (m_interrupt) {
LogDebug(BCLog::HTTP, "Instructing client to close their TCP connection to us."); LogDebug(BCLog::HTTP, "Instructing client to close their TCP connection to us (request id %d).", m_id);
WriteHeader("Connection", "close"); WriteHeader("Connection", "close");
} }
// Send event to main http thread to send reply message // Send event to main http thread to send reply message
@ -679,8 +682,9 @@ void HTTPRequest::WriteReply(int nStatus, std::span<const std::byte> reply)
assert(evb); assert(evb);
evbuffer_add(evb, reply.data(), reply.size()); evbuffer_add(evb, reply.data(), reply.size());
auto req_copy = req; auto req_copy = req;
HTTPEvent* ev = new HTTPEvent(eventBase, true, [req_copy, nStatus]{ HTTPEvent* ev = new HTTPEvent(eventBase, true, [req_copy, nStatus, id = m_id](){
evhttp_send_reply(req_copy, nStatus, nullptr, nullptr); evhttp_send_reply(req_copy, nStatus, nullptr, nullptr);
LogDebug(BCLog::HTTP, "Replied to request: %d", id);
// Re-enable reading from the socket. This is the second part of the libevent // Re-enable reading from the socket. This is the second part of the libevent
// workaround above. // workaround above.
if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02010900) { if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02010900) {

View file

@ -5,6 +5,7 @@
#ifndef BITCOIN_HTTPSERVER_H #ifndef BITCOIN_HTTPSERVER_H
#define BITCOIN_HTTPSERVER_H #define BITCOIN_HTTPSERVER_H
#include <cstdint>
#include <functional> #include <functional>
#include <optional> #include <optional>
#include <span> #include <span>
@ -72,10 +73,11 @@ class HTTPRequest
private: private:
struct evhttp_request* req; struct evhttp_request* req;
const util::SignalInterrupt& m_interrupt; const util::SignalInterrupt& m_interrupt;
const uint32_t m_id;
bool replySent; bool replySent;
public: public:
explicit HTTPRequest(struct evhttp_request* req, const util::SignalInterrupt& interrupt, bool replySent = false); explicit HTTPRequest(struct evhttp_request* req, const util::SignalInterrupt& interrupt, uint32_t id, bool replySent);
~HTTPRequest(); ~HTTPRequest();
enum RequestMethod { enum RequestMethod {

View file

@ -49,7 +49,7 @@ FUZZ_TARGET(http_request)
} }
util::SignalInterrupt interrupt; util::SignalInterrupt interrupt;
HTTPRequest http_request{evreq, interrupt, true}; HTTPRequest http_request{evreq, interrupt, /*id=*/0, /*replySent=*/true};
const HTTPRequest::RequestMethod request_method = http_request.GetRequestMethod(); const HTTPRequest::RequestMethod request_method = http_request.GetRequestMethod();
(void)RequestMethodString(request_method); (void)RequestMethodString(request_method);
(void)http_request.GetURI(); (void)http_request.GetURI();