Skip to content

Commit da73f15

Browse files
committed
qt: Fix shutdown when waitfor* cmds are called from RPC console
1 parent a33901c commit da73f15

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/interfaces/node.cpp

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

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 */
@@ -291,17 +295,24 @@ void StartRPC()
291295

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

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

307318
bool IsRPCRunning()

0 commit comments

Comments
 (0)