aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHennadii Stepanov <32963518+hebasto@users.noreply.github.com>2020-03-27 20:29:20 +0200
committerHennadii Stepanov <32963518+hebasto@users.noreply.github.com>2020-05-13 16:14:04 +0300
commitda73f1513a637a9f347b64de66564d6cdb2541f8 (patch)
tree80576a5b81b17858d82b11f2d0163c12303b13bd
parenta33901cb6d82e2f75aab67f80a8852b7e6e9817a (diff)
downloadbitcoin-da73f1513a637a9f347b64de66564d6cdb2541f8.tar.xz
qt: Fix shutdown when waitfor* cmds are called from RPC console
-rw-r--r--src/interfaces/node.cpp10
-rw-r--r--src/rpc/server.cpp25
2 files changed, 27 insertions, 8 deletions
diff --git a/src/interfaces/node.cpp b/src/interfaces/node.cpp
index 9e603a12cd..91ed1dc72f 100644
--- a/src/interfaces/node.cpp
+++ b/src/interfaces/node.cpp
@@ -87,7 +87,15 @@ public:
Interrupt(m_context);
Shutdown(m_context);
}
- void startShutdown() override { StartShutdown(); }
+ void startShutdown() override
+ {
+ StartShutdown();
+ // Stop RPC for clean shutdown if any of waitfor* commands is executed.
+ if (gArgs.GetBoolArg("-server", false)) {
+ InterruptRPC();
+ StopRPC();
+ }
+ }
bool shutdownRequested() override { return ShutdownRequested(); }
void mapPort(bool use_upnp) override
{
diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp
index 219979f095..9b7412cb76 100644
--- a/src/rpc/server.cpp
+++ b/src/rpc/server.cpp
@@ -15,11 +15,15 @@
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
+#include <cassert>
#include <memory> // for unique_ptr
+#include <mutex>
#include <unordered_map>
static RecursiveMutex cs_rpcWarmup;
static std::atomic<bool> g_rpc_running{false};
+static std::once_flag g_rpc_interrupt_flag;
+static std::once_flag g_rpc_stop_flag;
static bool fRPCInWarmup GUARDED_BY(cs_rpcWarmup) = true;
static std::string rpcWarmupStatus GUARDED_BY(cs_rpcWarmup) = "RPC server started";
/* Timer-creating functions */
@@ -291,17 +295,24 @@ void StartRPC()
void InterruptRPC()
{
- LogPrint(BCLog::RPC, "Interrupting RPC\n");
- // Interrupt e.g. running longpolls
- g_rpc_running = false;
+ // This function could be called twice if the GUI has been started with -server=1.
+ std::call_once(g_rpc_interrupt_flag, []() {
+ LogPrint(BCLog::RPC, "Interrupting RPC\n");
+ // Interrupt e.g. running longpolls
+ g_rpc_running = false;
+ });
}
void StopRPC()
{
- LogPrint(BCLog::RPC, "Stopping RPC\n");
- WITH_LOCK(g_deadline_timers_mutex, deadlineTimers.clear());
- DeleteAuthCookie();
- g_rpcSignals.Stopped();
+ // This function could be called twice if the GUI has been started with -server=1.
+ assert(!g_rpc_running);
+ std::call_once(g_rpc_stop_flag, []() {
+ LogPrint(BCLog::RPC, "Stopping RPC\n");
+ WITH_LOCK(g_deadline_timers_mutex, deadlineTimers.clear());
+ DeleteAuthCookie();
+ g_rpcSignals.Stopped();
+ });
}
bool IsRPCRunning()