Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c151978

Browse files
hebastopsgreco
authored andcommittedMar 5, 2025·
Prevent data race for pathHandlers
Github-Pull: bitcoin/bitcoin#25983 Rebased-From: 4296dde
1 parent f85a694 commit c151978

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
@@ -143,7 +143,8 @@ static std::vector<CSubNet> rpc_allow_subnets;
143143
//! Work queue for handling longer requests off the event loop thread
144144
static std::unique_ptr<WorkQueue<HTTPClosure>> g_work_queue{nullptr};
145145
//! Handlers for (sub)paths
146-
static std::vector<HTTPPathHandler> pathHandlers;
146+
static Mutex g_httppathhandlers_mutex;
147+
static std::vector<HTTPPathHandler> pathHandlers GUARDED_BY(g_httppathhandlers_mutex);
147148
//! Bound listening sockets
148149
static std::vector<evhttp_bound_socket *> boundSockets;
149150

@@ -244,6 +245,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
244245
// Find registered handler for prefix
245246
std::string strURI = hreq->GetURI();
246247
std::string path;
248+
LOCK(g_httppathhandlers_mutex);
247249
std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
248250
std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
249251
for (; i != iend; ++i) {
@@ -642,11 +644,13 @@ HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod() const
642644
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
643645
{
644646
LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
647+
LOCK(g_httppathhandlers_mutex);
645648
pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler));
646649
}
647650

648651
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
649652
{
653+
LOCK(g_httppathhandlers_mutex);
650654
std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin();
651655
std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end();
652656
for (; i != iend; ++i)

0 commit comments

Comments
 (0)
Please sign in to comment.