Skip to content

Commit 7a6667f

Browse files
jonasschnelliPastaPastaPasta
authored andcommitted
Merge bitcoin#18452: qt: Fix shutdown when waitfor* cmds are called from RPC console
da73f15 qt: Fix shutdown when waitfor* cmds are called from RPC console (Hennadii Stepanov) Pull request description: On master (7eed413), if the GUI has been started with`-server=1`, `bitcoin-qt` hangs on shutdown during calling any of the `waitfor*` commands in the GUI RPC console. This PR suggests minimal changes to fix this bug. Fix bitcoin#17495 ACKs for top commit: jonasschnelli: utACK da73f15 Tree-SHA512: 469f5332945a5f2c57d19336cda5df79b123ccc494aea6d58a85eb1293be52708b2b9c5bb6bc2c402a90b7b4e9e8d7ab8fe84cf201cf7ce612c9290c57e43681
1 parent 73c90c8 commit 7a6667f

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
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/rpc/server.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
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;
2527
static std::atomic<bool> g_rpc_running{false};
28+
static std::once_flag g_rpc_interrupt_flag;
29+
static std::once_flag g_rpc_stop_flag;
2630
static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true;
2731
static std::string rpcWarmupStatus GUARDED_BY(g_rpc_warmup_mutex) = "RPC server started";
2832
/* Timer-creating functions */
@@ -325,17 +329,24 @@ void StartRPC()
325329

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

333340
void StopRPC()
334341
{
335-
LogPrint(BCLog::RPC, "Stopping RPC\n");
336-
WITH_LOCK(g_deadline_timers_mutex, deadlineTimers.clear());
337-
DeleteAuthCookie();
338-
g_rpcSignals.Stopped();
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()

0 commit comments

Comments
 (0)