Skip to content

Commit 368530d

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

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/qt/bitcoin.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <interfaces/handler.h>
3131
#include <interfaces/node.h>
3232
#include <noui.h>
33+
#include <rpc/server.h>
3334
#include <ui_interface.h>
3435
#include <uint256.h>
3536
#include <util/system.h>
@@ -311,6 +312,11 @@ void BitcoinApplication::requestShutdown()
311312
// Request node shutdown, which can interrupt long operations, like
312313
// rescanning a wallet.
313314
m_node.startShutdown();
315+
// Stop RPC for clean shutdown if waitfor* commands are executed.
316+
if (gArgs.GetBoolArg("-server", false)) {
317+
InterruptRPC();
318+
StopRPC();
319+
}
314320
// Unsetting the client model can cause the current thread to wait for node
315321
// to complete an operation, like wait for a RPC execution to complete.
316322
window->setClientModel(nullptr);

src/rpc/server.cpp

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

18+
#include <cassert>
1819
#include <memory> // for unique_ptr
1920
#include <unordered_map>
2021

2122
static RecursiveMutex cs_rpcWarmup;
2223
static std::atomic<bool> g_rpc_running{false};
24+
static std::atomic<bool> g_rpc_stopped{true};
2325
static bool fRPCInWarmup GUARDED_BY(cs_rpcWarmup) = true;
2426
static std::string rpcWarmupStatus GUARDED_BY(cs_rpcWarmup) = "RPC server started";
2527
/* Timer-creating functions */
@@ -285,21 +287,28 @@ void StartRPC()
285287
{
286288
LogPrint(BCLog::RPC, "Starting RPC\n");
287289
g_rpc_running = true;
290+
g_rpc_stopped = false;
288291
g_rpcSignals.Started();
289292
}
290293

291294
void InterruptRPC()
292295
{
296+
// This function could be called twice if the GUI has been started with -server=1.
297+
if (!g_rpc_running) return;
293298
LogPrint(BCLog::RPC, "Interrupting RPC\n");
294299
// Interrupt e.g. running longpolls
295300
g_rpc_running = false;
296301
}
297302

298303
void StopRPC()
299304
{
305+
// This function could be called twice if the GUI has been started with -server=1.
306+
assert(!g_rpc_running);
307+
if (g_rpc_stopped) return;
300308
LogPrint(BCLog::RPC, "Stopping RPC\n");
301309
deadlineTimers.clear();
302310
DeleteAuthCookie();
311+
g_rpc_stopped = true;
303312
g_rpcSignals.Stopped();
304313
}
305314

0 commit comments

Comments
 (0)