Skip to content

Commit 2d7b6bc

Browse files
committed
qt: Fix shutdown when waitfor* cmds are called from RPC console
1 parent 7eed413 commit 2d7b6bc

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/interfaces/node.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ class NodeImpl : public Node
8686
Interrupt(m_context);
8787
Shutdown(m_context);
8888
}
89-
void startShutdown() override { StartShutdown(); }
89+
void startShutdown() override {
90+
StartShutdown();
91+
// Stop RPC for clean shutdown if any of waitfor* commands is executed.
92+
if (gArgs.GetBoolArg("-server", false)) {
93+
InterruptRPC();
94+
StopRPC();
95+
}
96+
}
9097
bool shutdownRequested() override { return ShutdownRequested(); }
9198
void mapPort(bool use_upnp) override
9299
{

src/rpc/server.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
#include <boost/algorithm/string/classification.hpp>
1616
#include <boost/algorithm/string/split.hpp>
1717

18+
#include <cassert>
1819
#include <memory> // for unique_ptr
20+
#include <mutex>
1921
#include <unordered_map>
2022

2123
static RecursiveMutex cs_rpcWarmup;
2224
static std::atomic<bool> g_rpc_running{false};
25+
static std::once_flag g_rpc_interrupt_flag;
26+
static std::once_flag g_rpc_stop_flag;
2327
static bool fRPCInWarmup GUARDED_BY(cs_rpcWarmup) = true;
2428
static std::string rpcWarmupStatus GUARDED_BY(cs_rpcWarmup) = "RPC server started";
2529
/* Timer-creating functions */
@@ -290,17 +294,24 @@ void StartRPC()
290294

291295
void InterruptRPC()
292296
{
293-
LogPrint(BCLog::RPC, "Interrupting RPC\n");
294-
// Interrupt e.g. running longpolls
295-
g_rpc_running = false;
297+
// This function could be called twice if the GUI has been started with -server=1.
298+
std::call_once(g_rpc_interrupt_flag, []() {
299+
LogPrint(BCLog::RPC, "Interrupting RPC\n");
300+
// Interrupt e.g. running longpolls
301+
g_rpc_running = false;
302+
});
296303
}
297304

298305
void StopRPC()
299306
{
300-
LogPrint(BCLog::RPC, "Stopping RPC\n");
301-
deadlineTimers.clear();
302-
DeleteAuthCookie();
303-
g_rpcSignals.Stopped();
307+
// This function could be called twice if the GUI has been started with -server=1.
308+
assert(!g_rpc_running);
309+
std::call_once(g_rpc_stop_flag, []() {
310+
LogPrint(BCLog::RPC, "Stopping RPC\n");
311+
deadlineTimers.clear();
312+
DeleteAuthCookie();
313+
g_rpcSignals.Stopped();
314+
});
304315
}
305316

306317
bool IsRPCRunning()

0 commit comments

Comments
 (0)