Skip to content

Commit 0dce004

Browse files
committed
Get rid of shutdown.cpp/shutdown.h, use SignalInterrupt directly
This change just removes some code and gets rid of an unnecessary layer of indirection after #27861
1 parent 99b3af7 commit 0dce004

33 files changed

+91
-147
lines changed

src/Makefile.am

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ BITCOIN_CORE_H = \
264264
script/sign.h \
265265
script/signingprovider.h \
266266
script/standard.h \
267-
shutdown.h \
268267
signet.h \
269268
streams.h \
270269
support/allocators/pool.h \
@@ -447,7 +446,6 @@ libbitcoin_node_a_SOURCES = \
447446
rpc/signmessage.cpp \
448447
rpc/txoutproof.cpp \
449448
script/sigcache.cpp \
450-
shutdown.cpp \
451449
signet.cpp \
452450
timedata.cpp \
453451
torcontrol.cpp \
@@ -956,7 +954,6 @@ libbitcoinkernel_la_SOURCES = \
956954
script/script_error.cpp \
957955
script/sigcache.cpp \
958956
script/standard.cpp \
959-
shutdown.cpp \
960957
signet.cpp \
961958
support/cleanse.cpp \
962959
support/lockedpool.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ int main(int argc, char* argv[])
130130
cache_sizes.coins_db = 2 << 22;
131131
cache_sizes.coins = (450 << 20) - (2 << 20) - (2 << 22);
132132
node::ChainstateLoadOptions options;
133-
options.check_interrupt = [] { return false; };
134133
auto [status, error] = node::LoadChainstate(chainman, cache_sizes, options);
135134
if (status != node::ChainstateLoadStatus::SUCCESS) {
136135
std::cerr << "Failed to load Chain state from your datadir." << std::endl;

src/bitcoind.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <node/context.h>
2121
#include <node/interface_ui.h>
2222
#include <noui.h>
23-
#include <shutdown.h>
2423
#include <util/check.h>
2524
#include <util/exception.h>
2625
#include <util/strencodings.h>
@@ -273,7 +272,7 @@ MAIN_FUNCTION
273272

274273
// Start application
275274
if (AppInit(node)) {
276-
WaitForShutdown();
275+
node.kernel->interrupt.wait();
277276
} else {
278277
node.exit_status = EXIT_FAILURE;
279278
}

src/httpserver.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#include <netbase.h>
1616
#include <node/interface_ui.h>
1717
#include <rpc/protocol.h> // For HTTP status codes
18-
#include <shutdown.h>
1918
#include <sync.h>
2019
#include <util/strencodings.h>
20+
#include <util/signalinterrupt.h>
2121
#include <util/threadnames.h>
2222
#include <util/translation.h>
2323

@@ -233,7 +233,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
233233
}
234234
}
235235
}
236-
std::unique_ptr<HTTPRequest> hreq(new HTTPRequest(req));
236+
auto hreq{std::make_unique<HTTPRequest>(req, *static_cast<const util::SignalInterrupt*>(arg))};
237237

238238
// Early address-based allow check
239239
if (!ClientAllowed(hreq->GetPeer())) {
@@ -374,7 +374,7 @@ static void libevent_log_cb(int severity, const char *msg)
374374
LogPrintLevel(BCLog::LIBEVENT, level, "%s\n", msg);
375375
}
376376

377-
bool InitHTTPServer()
377+
bool InitHTTPServer(const util::SignalInterrupt& interrupt)
378378
{
379379
if (!InitHTTPAllowList())
380380
return false;
@@ -403,7 +403,7 @@ bool InitHTTPServer()
403403
evhttp_set_timeout(http, gArgs.GetIntArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT));
404404
evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE);
405405
evhttp_set_max_body_size(http, MAX_SIZE);
406-
evhttp_set_gencb(http, http_request_cb, nullptr);
406+
evhttp_set_gencb(http, http_request_cb, (void*)&interrupt);
407407

408408
if (!HTTPBindAddresses(http)) {
409409
LogPrintf("Unable to bind any endpoint for RPC server\n");
@@ -531,7 +531,7 @@ void HTTPEvent::trigger(struct timeval* tv)
531531
else
532532
evtimer_add(ev, tv); // trigger after timeval passed
533533
}
534-
HTTPRequest::HTTPRequest(struct evhttp_request* _req, bool _replySent) : req(_req), replySent(_replySent)
534+
HTTPRequest::HTTPRequest(struct evhttp_request* _req, const util::SignalInterrupt& interrupt, bool _replySent) : req(_req), m_interrupt(interrupt), replySent(_replySent)
535535
{
536536
}
537537

@@ -591,7 +591,7 @@ void HTTPRequest::WriteHeader(const std::string& hdr, const std::string& value)
591591
void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
592592
{
593593
assert(!replySent && req);
594-
if (ShutdownRequested()) {
594+
if (m_interrupt) {
595595
WriteHeader("Connection", "close");
596596
}
597597
// Send event to main http thread to send reply message

src/httpserver.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <optional>
1010
#include <string>
1111

12+
namespace util {
13+
class SignalInterrupt;
14+
} // namespace util
15+
1216
static const int DEFAULT_HTTP_THREADS=4;
1317
static const int DEFAULT_HTTP_WORKQUEUE=16;
1418
static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
@@ -21,7 +25,7 @@ class HTTPRequest;
2125
/** Initialize HTTP server.
2226
* Call this before RegisterHTTPHandler or EventBase().
2327
*/
24-
bool InitHTTPServer();
28+
bool InitHTTPServer(const util::SignalInterrupt& interrupt);
2529
/** Start HTTP server.
2630
* This is separate from InitHTTPServer to give users race-condition-free time
2731
* to register their handlers between InitHTTPServer and StartHTTPServer.
@@ -57,10 +61,11 @@ class HTTPRequest
5761
{
5862
private:
5963
struct evhttp_request* req;
64+
const util::SignalInterrupt& m_interrupt;
6065
bool replySent;
6166

6267
public:
63-
explicit HTTPRequest(struct evhttp_request* req, bool replySent = false);
68+
explicit HTTPRequest(struct evhttp_request* req, const util::SignalInterrupt& interrupt, bool replySent = false);
6469
~HTTPRequest();
6570

6671
enum RequestMethod {

src/index/base.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <node/context.h>
1414
#include <node/database_args.h>
1515
#include <node/interface_ui.h>
16-
#include <shutdown.h>
1716
#include <tinyformat.h>
1817
#include <util/thread.h>
1918
#include <util/translation.h>
@@ -32,7 +31,7 @@ template <typename... Args>
3231
void BaseIndex::FatalErrorf(const char* fmt, const Args&... args)
3332
{
3433
auto message = tfm::format(fmt, args...);
35-
node::AbortNode(m_chain->context()->exit_status, message);
34+
node::AbortNode(&m_chain->context()->kernel->interrupt, m_chain->context()->exit_status, message);
3635
}
3736

3837
CBlockLocator GetLocator(interfaces::Chain& chain, const uint256& block_hash)

src/init.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
#include <scheduler.h>
6666
#include <script/sigcache.h>
6767
#include <script/standard.h>
68-
#include <shutdown.h>
6968
#include <sync.h>
7069
#include <timedata.h>
7170
#include <torcontrol.h>
@@ -184,17 +183,16 @@ static fs::path GetPidFile(const ArgsManager& args)
184183
// The network-processing threads are all part of a thread group
185184
// created by AppInit() or the Qt main() function.
186185
//
187-
// A clean exit happens when StartShutdown() or the SIGTERM
188-
// signal handler sets ShutdownRequested(), which makes main thread's
189-
// WaitForShutdown() interrupts the thread group.
190-
// And then, WaitForShutdown() makes all other on-going threads
191-
// in the thread group join the main thread.
186+
// A clean exit happens when the SignalInterrupt object or SIGTERM signal
187+
// handler is triggered. Which makes main thread's SignalInterrupt::wait() call
188+
// return, and join all other ongoing threads in the thread group to the main
189+
// thread.
192190
// Shutdown() is then called to clean up database connections, and stop other
193191
// threads that should only be stopped after the main network-processing
194192
// threads have exited.
195193
//
196194
// Shutdown for Qt is very similar, only it uses a QTimer to detect
197-
// ShutdownRequested() getting set, and then does the normal Qt
195+
// SignalInterrupt getting set, and then does the normal Qt
198196
// shutdown thing.
199197
//
200198

@@ -365,7 +363,7 @@ void Shutdown(NodeContext& node)
365363
#ifndef WIN32
366364
static void HandleSIGTERM(int)
367365
{
368-
StartShutdown();
366+
Assert(kernel::g_context)->interrupt();
369367
}
370368

371369
static void HandleSIGHUP(int)
@@ -375,7 +373,7 @@ static void HandleSIGHUP(int)
375373
#else
376374
static BOOL WINAPI consoleCtrlHandler(DWORD dwCtrlType)
377375
{
378-
StartShutdown();
376+
Assert(kernel::g_context)->interrupt();
379377
Sleep(INFINITE);
380378
return true;
381379
}
@@ -663,7 +661,7 @@ static bool AppInitServers(NodeContext& node)
663661
const ArgsManager& args = *Assert(node.args);
664662
RPCServer::OnStarted(&OnRPCStarted);
665663
RPCServer::OnStopped(&OnRPCStopped);
666-
if (!InitHTTPServer())
664+
if (!InitHTTPServer(node.kernel->interrupt))
667665
return false;
668666
StartRPC();
669667
node.rpc_interruption_point = RpcInterruptionPoint;
@@ -1407,7 +1405,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14071405

14081406
// ********************************************************* Step 7: load block chain
14091407

1410-
node.notifications = std::make_unique<KernelNotifications>(node.exit_status);
1408+
node.notifications = std::make_unique<KernelNotifications>(node.kernel->interrupt, node.exit_status);
14111409
fReindex = args.GetBoolArg("-reindex", false);
14121410
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);
14131411
ChainstateManager::Options chainman_opts{
@@ -1458,7 +1456,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14581456
}
14591457
LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", cache_sizes.coins * (1.0 / 1024 / 1024), mempool_opts.max_size_bytes * (1.0 / 1024 / 1024));
14601458

1461-
for (bool fLoaded = false; !fLoaded && !ShutdownRequested();) {
1459+
for (bool fLoaded = false; !fLoaded && !node.kernel->interrupt;) {
14621460
node.mempool = std::make_unique<CTxMemPool>(mempool_opts);
14631461

14641462
node.chainman = std::make_unique<ChainstateManager>(node.kernel->interrupt, chainman_opts, blockman_opts);
@@ -1472,7 +1470,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14721470
options.check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
14731471
options.check_level = args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
14741472
options.require_full_verification = args.IsArgSet("-checkblocks") || args.IsArgSet("-checklevel");
1475-
options.check_interrupt = ShutdownRequested;
14761473
options.coins_error_cb = [] {
14771474
uiInterface.ThreadSafeMessageBox(
14781475
_("Error reading from database, shutting down."),
@@ -1507,7 +1504,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15071504
return InitError(error);
15081505
}
15091506

1510-
if (!fLoaded && !ShutdownRequested()) {
1507+
if (!fLoaded && !node.kernel->interrupt) {
15111508
// first suggest a reindex
15121509
if (!options.reindex) {
15131510
bool fRet = uiInterface.ThreadSafeQuestion(
@@ -1516,7 +1513,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15161513
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
15171514
if (fRet) {
15181515
fReindex = true;
1519-
AbortShutdown();
1516+
node.kernel->interrupt.reset();
15201517
} else {
15211518
LogPrintf("Aborted block database rebuild. Exiting.\n");
15221519
return false;
@@ -1530,7 +1527,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15301527
// As LoadBlockIndex can take several minutes, it's possible the user
15311528
// requested to kill the GUI during the last operation. If so, exit.
15321529
// As the program has not fully started yet, Shutdown() is possibly overkill.
1533-
if (ShutdownRequested()) {
1530+
if (node.kernel->interrupt) {
15341531
LogPrintf("Shutdown requested. Exiting.\n");
15351532
return false;
15361533
}
@@ -1655,7 +1652,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16551652
ImportBlocks(chainman, vImportFiles);
16561653
if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
16571654
LogPrintf("Stopping after block import\n");
1658-
StartShutdown();
1655+
node.kernel->interrupt();
16591656
return;
16601657
}
16611658

@@ -1672,16 +1669,16 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16721669
// Wait for genesis block to be processed
16731670
{
16741671
WAIT_LOCK(g_genesis_wait_mutex, lock);
1675-
// We previously could hang here if StartShutdown() is called prior to
1672+
// We previously could hang here if node.kernel->interrupt() is called prior to
16761673
// ImportBlocks getting started, so instead we just wait on a timer to
1677-
// check ShutdownRequested() regularly.
1678-
while (!fHaveGenesis && !ShutdownRequested()) {
1674+
// check node.kernel->interrupt regularly.
1675+
while (!fHaveGenesis && !node.kernel->interrupt) {
16791676
g_genesis_wait_cv.wait_for(lock, std::chrono::milliseconds(500));
16801677
}
16811678
block_notify_genesis_wait_connection.disconnect();
16821679
}
16831680

1684-
if (ShutdownRequested()) {
1681+
if (node.kernel->interrupt) {
16851682
return false;
16861683
}
16871684

src/node/abort.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <logging.h>
88
#include <node/interface_ui.h>
9-
#include <shutdown.h>
9+
#include <util/signalinterrupt.h>
1010
#include <util/translation.h>
1111
#include <warnings.h>
1212

@@ -16,12 +16,12 @@
1616

1717
namespace node {
1818

19-
void AbortNode(std::atomic<int>& exit_status, const std::string& debug_message, const bilingual_str& user_message, bool shutdown)
19+
void AbortNode(util::SignalInterrupt* interrupt, std::atomic<int>& exit_status, const std::string& debug_message, const bilingual_str& user_message)
2020
{
2121
SetMiscWarning(Untranslated(debug_message));
2222
LogPrintf("*** %s\n", debug_message);
2323
InitError(user_message.empty() ? _("A fatal internal error occurred, see debug.log for details") : user_message);
2424
exit_status.store(EXIT_FAILURE);
25-
if (shutdown) StartShutdown();
25+
if (interrupt) (*interrupt)();
2626
}
2727
} // namespace node

src/node/abort.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
#include <atomic>
1111
#include <string>
1212

13+
namespace util {
14+
class SignalInterrupt;
15+
} // namespace util
16+
1317
namespace node {
14-
void AbortNode(std::atomic<int>& exit_status, const std::string& debug_message, const bilingual_str& user_message = {}, bool shutdown = true);
18+
void AbortNode(util::SignalInterrupt* interrupt, std::atomic<int>& exit_status, const std::string& debug_message, const bilingual_str& user_message = {});
1519
} // namespace node
1620

1721
#endif // BITCOIN_NODE_ABORT_H

src/node/blockstorage.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <flatfile.h>
1111
#include <hash.h>
1212
#include <kernel/chainparams.h>
13+
#include <kernel/context.h>
1314
#include <logging.h>
1415
#include <pow.h>
1516
#include <reverse_iterator.h>

0 commit comments

Comments
 (0)