Skip to content

Commit feeb7b8

Browse files
committed
refactor: Remove calls to StartShutdown from KernelNotifications
Use SignalInterrupt object instead. There is a slight change in behavior here because the previous StartShutdown code used to abort on failure and the new code logs errors instead.
1 parent 6824eec commit feeb7b8

File tree

9 files changed

+29
-17
lines changed

9 files changed

+29
-17
lines changed

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()->shutdown, m_chain->context()->exit_status, message);
3635
}
3736

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

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14321432

14331433
// ********************************************************* Step 7: load block chain
14341434

1435-
node.notifications = std::make_unique<KernelNotifications>(node.exit_status);
1435+
node.notifications = std::make_unique<KernelNotifications>(*Assert(node.shutdown), node.exit_status);
14361436
ReadNotificationArgs(args, *node.notifications);
14371437
fReindex = args.GetBoolArg("-reindex", false);
14381438
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);

src/node/abort.cpp

Lines changed: 5 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,14 @@
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* shutdown, 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 (shutdown && !(*shutdown)()) {
26+
LogPrintf("Error: failed to send shutdown signal\n");
27+
};
2628
}
2729
} // 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* shutdown, 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/kernel_notifications.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <logging.h>
1616
#include <node/abort.h>
1717
#include <node/interface_ui.h>
18-
#include <shutdown.h>
1918
#include <util/check.h>
2019
#include <util/strencodings.h>
2120
#include <util/string.h>
@@ -62,7 +61,9 @@ kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state
6261
{
6362
uiInterface.NotifyBlockTip(state, &index);
6463
if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
65-
StartShutdown();
64+
if (!m_shutdown()) {
65+
LogPrintf("Error: failed to send shutdown signal after reaching stop height\n");
66+
}
6667
return kernel::Interrupted{};
6768
}
6869
return {};
@@ -85,12 +86,13 @@ void KernelNotifications::warning(const bilingual_str& warning)
8586

8687
void KernelNotifications::flushError(const std::string& debug_message)
8788
{
88-
AbortNode(m_exit_status, debug_message);
89+
AbortNode(&m_shutdown, m_exit_status, debug_message);
8990
}
9091

9192
void KernelNotifications::fatalError(const std::string& debug_message, const bilingual_str& user_message)
9293
{
93-
node::AbortNode(m_exit_status, debug_message, user_message, m_shutdown_on_fatal_error);
94+
node::AbortNode(m_shutdown_on_fatal_error ? &m_shutdown : nullptr,
95+
m_exit_status, debug_message, user_message);
9496
}
9597

9698
void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications)

src/node/kernel_notifications.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ class CBlockIndex;
1616
enum class SynchronizationState;
1717
struct bilingual_str;
1818

19+
namespace util {
20+
class SignalInterrupt;
21+
} // namespace util
22+
1923
namespace node {
2024

2125
static constexpr int DEFAULT_STOPATHEIGHT{0};
2226

2327
class KernelNotifications : public kernel::Notifications
2428
{
2529
public:
26-
KernelNotifications(std::atomic<int>& exit_status) : m_exit_status{exit_status} {}
30+
KernelNotifications(util::SignalInterrupt& shutdown, std::atomic<int>& exit_status) : m_shutdown(shutdown), m_exit_status{exit_status} {}
2731

2832
[[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) override;
2933

@@ -42,6 +46,7 @@ class KernelNotifications : public kernel::Notifications
4246
//! Useful for tests, can be set to false to avoid shutdown on fatal error.
4347
bool m_shutdown_on_fatal_error{true};
4448
private:
49+
util::SignalInterrupt& m_shutdown;
4550
std::atomic<int>& m_exit_status;
4651
};
4752

src/test/blockmanager_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
2727
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
2828
{
2929
const auto params {CreateChainParams(ArgsManager{}, ChainType::MAIN)};
30-
KernelNotifications notifications{m_node.exit_status};
30+
KernelNotifications notifications{*Assert(m_node.shutdown), m_node.exit_status};
3131
const BlockManager::Options blockman_opts{
3232
.chainparams = *params,
3333
.blocks_dir = m_args.GetBlocksDirPath(),
@@ -134,13 +134,13 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)
134134

135135
BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
136136
{
137-
KernelNotifications notifications{m_node.exit_status};
137+
KernelNotifications notifications{*Assert(m_node.shutdown), m_node.exit_status};
138138
node::BlockManager::Options blockman_opts{
139139
.chainparams = Params(),
140140
.blocks_dir = m_args.GetBlocksDirPath(),
141141
.notifications = notifications,
142142
};
143-
BlockManager blockman{m_node.kernel->interrupt, blockman_opts};
143+
BlockManager blockman{*Assert(m_node.shutdown), blockman_opts};
144144

145145
// Test blocks with no transactions, not even a coinbase
146146
CBlock block1;

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto
180180

181181
m_cache_sizes = CalculateCacheSizes(m_args);
182182

183-
m_node.notifications = std::make_unique<KernelNotifications>(m_node.exit_status);
183+
m_node.notifications = std::make_unique<KernelNotifications>(*Assert(m_node.shutdown), m_node.exit_status);
184184

185185
const ChainstateManager::Options chainman_opts{
186186
.chainparams = chainparams,

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ struct SnapshotTestSetup : TestChain100Setup {
379379
LOCK(::cs_main);
380380
chainman.ResetChainstates();
381381
BOOST_CHECK_EQUAL(chainman.GetAll().size(), 0);
382-
m_node.notifications = std::make_unique<KernelNotifications>(m_node.exit_status);
382+
m_node.notifications = std::make_unique<KernelNotifications>(*Assert(m_node.shutdown), m_node.exit_status);
383383
const ChainstateManager::Options chainman_opts{
384384
.chainparams = ::Params(),
385385
.datadir = chainman.m_options.datadir,

0 commit comments

Comments
 (0)