Skip to content

Commit bd5ec7c

Browse files
author
MarcoFalke
committed
Merge #19006: rpc: Avoid crash when g_thread_http was never started
faf45d1 http: Avoid crash when g_thread_http was never started (MarcoFalke) fa12a37 test: Replace inline-comments with logs, pep8 formatting (MarcoFalke) fa83b39 init: Remove confusing and redundant InitError (MarcoFalke) Pull request description: Avoid a crash during shutdown when the init sequence failed for some reason ACKs for top commit: promag: Tested ACK faf45d1. ryanofsky: Code review ACK faf45d1. Thanks for updates, this is much easier to parse for me now. Since previous reviews: split out and reverted some cleanups & replaced chmod with mkdir in test hebasto: ACK faf45d1, tested on Linux Mint 19.3 with the following patch: Tree-SHA512: 59632bf01c999e65c724e2728ac103250ccd8b0b16fac19d3a2a82639ab73e4f2efb86c78e63c588a5954625d8d0cf9545e2a7e070e6e15d2a54beeb50e00b61
2 parents a587f85 + faf45d1 commit bd5ec7c

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

src/httprpc.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <httpserver.h>
1010
#include <rpc/protocol.h>
1111
#include <rpc/server.h>
12-
#include <ui_interface.h>
1312
#include <util/strencodings.h>
1413
#include <util/system.h>
1514
#include <util/translation.h>
@@ -251,9 +250,6 @@ static bool InitRPCAuthentication()
251250
{
252251
LogPrintf("Using random cookie authentication.\n");
253252
if (!GenerateAuthCookie(&strRPCUserColonPass)) {
254-
uiInterface.ThreadSafeMessageBox(
255-
_("Error: A fatal internal error occurred, see debug.log for details"), // Same message as AbortNode
256-
"", CClientUIInterface::MSG_ERROR);
257253
return false;
258254
}
259255
} else {

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: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import configparser
2121
import sys
2222

23+
2324
def call_with_auth(node, user, password):
2425
url = urllib.parse.urlparse(node.url)
2526
headers = {"Authorization": "Basic " + str_to_b64str('{}:{}'.format(user, password))}
@@ -64,9 +65,9 @@ def setup_chain(self):
6465
self.password = lines[3]
6566

6667
with open(os.path.join(get_datadir_path(self.options.tmpdir, 0), "bitcoin.conf"), 'a', encoding='utf8') as f:
67-
f.write(rpcauth+"\n")
68-
f.write(rpcauth2+"\n")
69-
f.write(rpcauth3+"\n")
68+
f.write(rpcauth + "\n")
69+
f.write(rpcauth2 + "\n")
70+
f.write(rpcauth3 + "\n")
7071
with open(os.path.join(get_datadir_path(self.options.tmpdir, 1), "bitcoin.conf"), 'a', encoding='utf8') as f:
7172
f.write("rpcuser={}\n".format(self.rpcuser))
7273
f.write("rpcpassword={}\n".format(self.rpcpassword))
@@ -76,32 +77,35 @@ def test_auth(self, node, user, password):
7677
assert_equal(200, call_with_auth(node, user, password).status)
7778

7879
self.log.info('Wrong...')
79-
assert_equal(401, call_with_auth(node, user, password+'wrong').status)
80+
assert_equal(401, call_with_auth(node, user, password + 'wrong').status)
8081

8182
self.log.info('Wrong...')
82-
assert_equal(401, call_with_auth(node, user+'wrong', password).status)
83+
assert_equal(401, call_with_auth(node, user + 'wrong', password).status)
8384

8485
self.log.info('Wrong...')
85-
assert_equal(401, call_with_auth(node, user+'wrong', password+'wrong').status)
86+
assert_equal(401, call_with_auth(node, user + 'wrong', password + 'wrong').status)
8687

8788
def run_test(self):
88-
89-
##################################################
90-
# Check correctness of the rpcauth config option #
91-
##################################################
89+
self.log.info('Check correctness of the rpcauth config option')
9290
url = urllib.parse.urlparse(self.nodes[0].url)
9391

9492
self.test_auth(self.nodes[0], url.username, url.password)
9593
self.test_auth(self.nodes[0], 'rt', self.rtpassword)
9694
self.test_auth(self.nodes[0], 'rt2', self.rt2password)
9795
self.test_auth(self.nodes[0], self.user, self.password)
9896

99-
###############################################################
100-
# Check correctness of the rpcuser/rpcpassword config options #
101-
###############################################################
97+
self.log.info('Check correctness of the rpcuser/rpcpassword config options')
10298
url = urllib.parse.urlparse(self.nodes[1].url)
10399

104100
self.test_auth(self.nodes[1], self.rpcuser, self.rpcpassword)
105101

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+
106110
if __name__ == '__main__':
107-
HTTPBasicsTest ().main ()
111+
HTTPBasicsTest().main()

0 commit comments

Comments
 (0)