Skip to content

Commit 4296dde

Browse files
committed
Prevent data race for pathHandlers
1 parent ea67232 commit 4296dde

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)