Skip to content

Commit faf45d1

Browse files
author
MarcoFalke
committed
http: Avoid crash when g_thread_http was never started
g_thread_http can not be joined when it is not joinable. Avoid crashing the node by adding the required check and add a test.
1 parent fa12a37 commit faf45d1

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/httpserver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,15 @@ bool UpdateHTTPServerLogging(bool enable) {
421421
#endif
422422
}
423423

424-
static std::thread threadHTTP;
424+
static std::thread g_thread_http;
425425
static std::vector<std::thread> g_thread_http_workers;
426426

427427
void StartHTTPServer()
428428
{
429429
LogPrint(BCLog::HTTP, "Starting HTTP server\n");
430430
int rpcThreads = std::max((long)gArgs.GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
431431
LogPrintf("HTTP: starting %d worker threads\n", rpcThreads);
432-
threadHTTP = std::thread(ThreadHTTP, eventBase);
432+
g_thread_http = std::thread(ThreadHTTP, eventBase);
433433

434434
for (int i = 0; i < rpcThreads; i++) {
435435
g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue, i);
@@ -467,7 +467,7 @@ void StopHTTPServer()
467467
boundSockets.clear();
468468
if (eventBase) {
469469
LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
470-
threadHTTP.join();
470+
if (g_thread_http.joinable()) g_thread_http.join();
471471
}
472472
if (eventHTTP) {
473473
evhttp_free(eventHTTP);

test/functional/rpc_users.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,13 @@ def run_test(self):
9999

100100
self.test_auth(self.nodes[1], self.rpcuser, self.rpcpassword)
101101

102+
self.log.info('Check that failure to write cookie file will abort the node gracefully')
103+
self.stop_node(0)
104+
cookie_file = os.path.join(get_datadir_path(self.options.tmpdir, 0), self.chain, '.cookie.tmp')
105+
os.mkdir(cookie_file)
106+
init_error = 'Error: Unable to start HTTP server. See debug log for details.'
107+
self.nodes[0].assert_start_raises_init_error(expected_msg=init_error)
108+
109+
102110
if __name__ == '__main__':
103111
HTTPBasicsTest().main()

0 commit comments

Comments
 (0)