Skip to content

Commit fc44d17

Browse files
author
MacroFake
committed
Merge bitcoin#25983: Prevent data race for pathHandlers
4296dde Prevent data race for `pathHandlers` (Hennadii Stepanov) Pull request description: Fixes bitcoin#19341. ACKs for top commit: ryanofsky: Code review ACK 4296dde. This should protect the vector. It also seems to make the http_request_cb callback single threaded, but that seems ok, since it is just adding work queue items not actually processing requests. Tree-SHA512: 1c3183100bbc80d8e83543da090b8f4521921cf30d444e3e4c87102bf7a1e67ccc4dfea7e9990ac49741b2a5708f259f4eced9d4049c20ae4e531461532a6aef
2 parents 124e75a + 4296dde commit fc44d17

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/httpserver.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ static std::vector<CSubNet> rpc_allow_subnets;
142142
//! Work queue for handling longer requests off the event loop thread
143143
static std::unique_ptr<WorkQueue<HTTPClosure>> g_work_queue{nullptr};
144144
//! Handlers for (sub)paths
145-
static std::vector<HTTPPathHandler> pathHandlers;
145+
static GlobalMutex g_httppathhandlers_mutex;
146+
static std::vector<HTTPPathHandler> pathHandlers GUARDED_BY(g_httppathhandlers_mutex);
146147
//! Bound listening sockets
147148
static std::vector<evhttp_bound_socket *> boundSockets;
148149

@@ -243,6 +244,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
243244
// Find registered handler for prefix
244245
std::string strURI = hreq->GetURI();
245246
std::string path;
247+
LOCK(g_httppathhandlers_mutex);
246248
std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
247249
std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
248250
for (; i != iend; ++i) {
@@ -674,11 +676,13 @@ std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::
674676
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
675677
{
676678
LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
679+
LOCK(g_httppathhandlers_mutex);
677680
pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler));
678681
}
679682

680683
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
681684
{
685+
LOCK(g_httppathhandlers_mutex);
682686
std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin();
683687
std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end();
684688
for (; i != iend; ++i)

0 commit comments

Comments
 (0)