Skip to content

Commit 3ef34c1

Browse files
hebastopsgreco
authored andcommitted
Prevent data race for pathHandlers
Github-Pull: bitcoin/bitcoin#25983 Rebased-From: 4296dde (cherry picked from commit 2c6c628)
1 parent 19f224f commit 3ef34c1

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/httpserver.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ static std::vector<CSubNet> rpc_allow_subnets;
138138
//! Work queue for handling longer requests off the event loop thread
139139
static std::unique_ptr<WorkQueue<HTTPClosure>> g_work_queue{nullptr};
140140
//! Handlers for (sub)paths
141-
static std::vector<HTTPPathHandler> pathHandlers;
141+
static Mutex g_httppathhandlers_mutex;
142+
static std::vector<HTTPPathHandler> pathHandlers GUARDED_BY(g_httppathhandlers_mutex);
142143
//! Bound listening sockets
143144
static std::vector<evhttp_bound_socket *> boundSockets;
144145

@@ -239,6 +240,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
239240
// Find registered handler for prefix
240241
std::string strURI = hreq->GetURI();
241242
std::string path;
243+
LOCK(g_httppathhandlers_mutex);
242244
std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
243245
std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
244246
for (; i != iend; ++i) {
@@ -633,11 +635,13 @@ HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod() const
633635
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
634636
{
635637
LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
638+
LOCK(g_httppathhandlers_mutex);
636639
pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler));
637640
}
638641

639642
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
640643
{
644+
LOCK(g_httppathhandlers_mutex);
641645
std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin();
642646
std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end();
643647
for (; i != iend; ++i)

0 commit comments

Comments
 (0)