Skip to content

Commit 37f43e4

Browse files
Merge dashpay#5737: backport: Merge bitcoin#18452, 19111, 19104
96ac317 Merge bitcoin#19104: gui, refactor: Register Qt meta types in application constructor (Jonas Schnelli) 32f717c Merge bitcoin#19111: Limit scope of all global std::once_flag (MarcoFalke) 7a6667f Merge bitcoin#18452: qt: Fix shutdown when waitfor* cmds are called from RPC console (Jonas Schnelli) Pull request description: ## Issue being fixed or feature implemented ## What was done? ## How Has This Been Tested? CI passing ## Breaking Changes None ## Checklist: _Go over all the following points, and put an `x` in all the boxes that apply._ - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ Top commit has no ACKs. Tree-SHA512: a1a9d18674b7c55549edd684211bb9de983a67876ed292d90ef3dccb126122af9b5d8c887608bbce74b7fa92c020a542d2ea1acdc693d85eba889aa8c56ac8df
2 parents 73c90c8 + 96ac317 commit 37f43e4

File tree

5 files changed

+48
-26
lines changed

5 files changed

+48
-26
lines changed

src/node/interfaces.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,15 @@ class NodeImpl : public Node
254254
StartRestart();
255255
PrepareShutdown(*m_context);
256256
}
257-
void startShutdown() override { StartShutdown(); }
257+
void startShutdown() override
258+
{
259+
StartShutdown();
260+
// Stop RPC for clean shutdown if any of waitfor* commands is executed.
261+
if (gArgs.GetBoolArg("-server", false)) {
262+
InterruptRPC();
263+
StopRPC();
264+
}
265+
}
258266
bool shutdownRequested() override { return ShutdownRequested(); }
259267
void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
260268
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }

src/qt/bitcoin.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ Q_DECLARE_METATYPE(CAmount)
7474
Q_DECLARE_METATYPE(SynchronizationState)
7575
Q_DECLARE_METATYPE(uint256)
7676

77+
static void RegisterMetaTypes()
78+
{
79+
// Register meta types used for QMetaObject::invokeMethod and Qt::QueuedConnection
80+
qRegisterMetaType<bool*>();
81+
qRegisterMetaType<SynchronizationState>();
82+
#ifdef ENABLE_WALLET
83+
qRegisterMetaType<WalletModel*>();
84+
#endif
85+
// Register typedefs (see http://qt-project.org/doc/qt-5/qmetatype.html#qRegisterMetaType)
86+
// IMPORTANT: if CAmount is no longer a typedef use the normal variant above (see https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType-1)
87+
qRegisterMetaType<CAmount>("CAmount");
88+
qRegisterMetaType<size_t>("size_t");
89+
90+
qRegisterMetaType<std::function<void()>>("std::function<void()>");
91+
qRegisterMetaType<QMessageBox::Icon>("QMessageBox::Icon");
92+
qRegisterMetaType<interfaces::BlockAndHeaderTipInfo>("interfaces::BlockAndHeaderTipInfo");
93+
}
94+
7795
static QString GetLangTerritory()
7896
{
7997
QSettings settings;
@@ -262,6 +280,7 @@ BitcoinApplication::BitcoinApplication():
262280
pollShutdownTimer(nullptr),
263281
returnValue(0)
264282
{
283+
RegisterMetaTypes();
265284
// Qt runs setlocale(LC_ALL, "") on initialization.
266285
setQuitOnLastWindowClosed(false);
267286
}
@@ -543,21 +562,6 @@ int GuiMain(int argc, char* argv[])
543562

544563
BitcoinApplication app;
545564

546-
// Register meta types used for QMetaObject::invokeMethod and Qt::QueuedConnection
547-
qRegisterMetaType<bool*>();
548-
qRegisterMetaType<SynchronizationState>();
549-
#ifdef ENABLE_WALLET
550-
qRegisterMetaType<WalletModel*>();
551-
#endif
552-
// Register typedefs (see http://qt-project.org/doc/qt-5/qmetatype.html#qRegisterMetaType)
553-
// IMPORTANT: if CAmount is no longer a typedef use the normal variant above (see https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType-1)
554-
qRegisterMetaType<CAmount>("CAmount");
555-
qRegisterMetaType<size_t>("size_t");
556-
557-
qRegisterMetaType<std::function<void()>>("std::function<void()>");
558-
qRegisterMetaType<QMessageBox::Icon>("QMessageBox::Icon");
559-
qRegisterMetaType<interfaces::BlockAndHeaderTipInfo>("interfaces::BlockAndHeaderTipInfo");
560-
561565
/// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these
562566
// Command-line options take precedence:
563567
SetupServerArgs(node_context);

src/rpc/server.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
#include <algorithm>
2020
#include <atomic>
21+
#include <cassert>
2122
#include <memory> // for unique_ptr
23+
#include <mutex>
2224
#include <unordered_map>
2325

2426
static Mutex g_rpc_warmup_mutex;
@@ -325,17 +327,26 @@ void StartRPC()
325327

326328
void InterruptRPC()
327329
{
328-
LogPrint(BCLog::RPC, "Interrupting RPC\n");
329-
// Interrupt e.g. running longpolls
330-
g_rpc_running = false;
330+
static std::once_flag g_rpc_interrupt_flag;
331+
// This function could be called twice if the GUI has been started with -server=1.
332+
std::call_once(g_rpc_interrupt_flag, []() {
333+
LogPrint(BCLog::RPC, "Interrupting RPC\n");
334+
// Interrupt e.g. running longpolls
335+
g_rpc_running = false;
336+
});
331337
}
332338

333339
void StopRPC()
334340
{
335-
LogPrint(BCLog::RPC, "Stopping RPC\n");
336-
WITH_LOCK(g_deadline_timers_mutex, deadlineTimers.clear());
337-
DeleteAuthCookie();
338-
g_rpcSignals.Stopped();
341+
static std::once_flag g_rpc_stop_flag;
342+
// This function could be called twice if the GUI has been started with -server=1.
343+
assert(!g_rpc_running);
344+
std::call_once(g_rpc_stop_flag, []() {
345+
LogPrint(BCLog::RPC, "Stopping RPC\n");
346+
WITH_LOCK(g_deadline_timers_mutex, deadlineTimers.clear());
347+
DeleteAuthCookie();
348+
g_rpcSignals.Stopped();
349+
});
339350
}
340351

341352
bool IsRPCRunning()

src/support/lockedpool.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#endif
3030

3131
LockedPoolManager* LockedPoolManager::_instance = nullptr;
32-
std::once_flag LockedPoolManager::init_flag;
3332

3433
/*******************************************************************************/
3534
// Utilities

src/support/lockedpool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ class LockedPoolManager : public LockedPool
221221
/** Return the current instance, or create it once */
222222
static LockedPoolManager& Instance()
223223
{
224-
std::call_once(LockedPoolManager::init_flag, LockedPoolManager::CreateInstance);
224+
static std::once_flag init_flag;
225+
std::call_once(init_flag, LockedPoolManager::CreateInstance);
225226
return *LockedPoolManager::_instance;
226227
}
227228

@@ -234,7 +235,6 @@ class LockedPoolManager : public LockedPool
234235
static bool LockingFailed();
235236

236237
static LockedPoolManager* _instance;
237-
static std::once_flag init_flag;
238238
};
239239

240240
#endif // BITCOIN_SUPPORT_LOCKEDPOOL_H

0 commit comments

Comments
 (0)