From 4296dde28757d88a7076847484669fb202b47bc8 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Fri, 2 Sep 2022 12:50:12 +0100 Subject: Prevent data race for `pathHandlers` --- src/httpserver.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/httpserver.cpp') diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 8e00a6278f..e68436cc2c 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -142,7 +142,8 @@ static std::vector rpc_allow_subnets; //! Work queue for handling longer requests off the event loop thread static std::unique_ptr> g_work_queue{nullptr}; //! Handlers for (sub)paths -static std::vector pathHandlers; +static GlobalMutex g_httppathhandlers_mutex; +static std::vector pathHandlers GUARDED_BY(g_httppathhandlers_mutex); //! Bound listening sockets static std::vector boundSockets; @@ -243,6 +244,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg) // Find registered handler for prefix std::string strURI = hreq->GetURI(); std::string path; + LOCK(g_httppathhandlers_mutex); std::vector::const_iterator i = pathHandlers.begin(); std::vector::const_iterator iend = pathHandlers.end(); for (; i != iend; ++i) { @@ -674,11 +676,13 @@ std::optional GetQueryParameterFromUri(const char* uri, const std:: void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler) { LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch); + LOCK(g_httppathhandlers_mutex); pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler)); } void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch) { + LOCK(g_httppathhandlers_mutex); std::vector::iterator i = pathHandlers.begin(); std::vector::iterator iend = pathHandlers.end(); for (; i != iend; ++i) -- cgit v1.2.3