From 3a174cd400c6c239539d4c0c10b557c3e0615212 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Fri, 28 Aug 2015 16:55:16 +0200 Subject: Fix race condition between starting HTTP server thread and setting EventBase() Split StartHTTPServer into InitHTTPServer and StartHTTPServer to give clients a window to register their handlers without race conditions. Thanks @ajweiss for figuring this out. --- src/httpserver.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src/httpserver.cpp') diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 813764f22..7e599b1d7 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -320,7 +320,7 @@ static void HTTPWorkQueueRun(WorkQueue* queue) queue->Run(); } -bool StartHTTPServer(boost::thread_group& threadGroup) +bool InitHTTPServer() { struct evhttp* http = 0; struct event_base* base = 0; @@ -366,19 +366,25 @@ bool StartHTTPServer(boost::thread_group& threadGroup) return false; } - LogPrint("http", "Starting HTTP server\n"); + LogPrint("http", "Initialized HTTP server\n"); int workQueueDepth = std::max((long)GetArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L); - int rpcThreads = std::max((long)GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L); - LogPrintf("HTTP: creating work queue of depth %d and %d worker threads\n", workQueueDepth, rpcThreads); + LogPrintf("HTTP: creating work queue of depth %d\n", workQueueDepth); + workQueue = new WorkQueue(workQueueDepth); + eventBase = base; + eventHTTP = http; + return true; +} - threadGroup.create_thread(boost::bind(&ThreadHTTP, base, http)); +bool StartHTTPServer(boost::thread_group& threadGroup) +{ + LogPrint("http", "Starting HTTP server\n"); + int rpcThreads = std::max((long)GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L); + LogPrintf("HTTP: starting %d worker threads\n", rpcThreads); + threadGroup.create_thread(boost::bind(&ThreadHTTP, eventBase, eventHTTP)); for (int i = 0; i < rpcThreads; i++) threadGroup.create_thread(boost::bind(&HTTPWorkQueueRun, workQueue)); - - eventBase = base; - eventHTTP = http; return true; } -- cgit v1.2.3