Merge bitcoin/bitcoin#26034: [22.x] Prevent data race for pathHandlers

2c6c628ab9 Prevent data race for `pathHandlers` (Hennadii Stepanov)

Pull request description:

  Backport of https://github.com/bitcoin/bitcoin/pull/25983 to the 22.x branch.

ACKs for top commit:
  dergoegge:
    ACK 2c6c628ab9

Tree-SHA512: 9b172f73407fdd5a79e67ed1b2b3b7c6e7989ea1de6757c839ee7040d62ebbd87d10770c6fcb39709a07d52345dc9b7b244ef2b490c9ad8a656ff8ad4d618a01
This commit is contained in:
MacroFake 2022-10-24 11:46:09 +02:00
commit 6164618964
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

@ -138,7 +138,8 @@ static std::vector<CSubNet> rpc_allow_subnets;
//! Work queue for handling longer requests off the event loop thread //! Work queue for handling longer requests off the event loop thread
static std::unique_ptr<WorkQueue<HTTPClosure>> g_work_queue{nullptr}; static std::unique_ptr<WorkQueue<HTTPClosure>> g_work_queue{nullptr};
//! Handlers for (sub)paths //! Handlers for (sub)paths
static std::vector<HTTPPathHandler> pathHandlers; static Mutex g_httppathhandlers_mutex;
static std::vector<HTTPPathHandler> pathHandlers GUARDED_BY(g_httppathhandlers_mutex);
//! Bound listening sockets //! Bound listening sockets
static std::vector<evhttp_bound_socket *> boundSockets; static std::vector<evhttp_bound_socket *> boundSockets;
@ -239,6 +240,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
// Find registered handler for prefix // Find registered handler for prefix
std::string strURI = hreq->GetURI(); std::string strURI = hreq->GetURI();
std::string path; std::string path;
LOCK(g_httppathhandlers_mutex);
std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin(); std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end(); std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
for (; i != iend; ++i) { for (; i != iend; ++i) {
@ -633,11 +635,13 @@ HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod() const
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler) void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
{ {
LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch); LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
LOCK(g_httppathhandlers_mutex);
pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler)); pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler));
} }
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch) void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
{ {
LOCK(g_httppathhandlers_mutex);
std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin(); std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin();
std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end(); std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end();
for (; i != iend; ++i) for (; i != iend; ++i)