|
18 | 18 |
|
19 | 19 | #include <algorithm>
|
20 | 20 | #include <atomic>
|
| 21 | +#include <cassert> |
21 | 22 | #include <memory> // for unique_ptr
|
| 23 | +#include <mutex> |
22 | 24 | #include <unordered_map>
|
23 | 25 |
|
24 | 26 | static Mutex g_rpc_warmup_mutex;
|
25 | 27 | 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; |
26 | 30 | static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true;
|
27 | 31 | static std::string rpcWarmupStatus GUARDED_BY(g_rpc_warmup_mutex) = "RPC server started";
|
28 | 32 | /* Timer-creating functions */
|
@@ -325,17 +329,24 @@ void StartRPC()
|
325 | 329 |
|
326 | 330 | void InterruptRPC()
|
327 | 331 | {
|
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 | + }); |
331 | 338 | }
|
332 | 339 |
|
333 | 340 | void StopRPC()
|
334 | 341 | {
|
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 | + }); |
339 | 350 | }
|
340 | 351 |
|
341 | 352 | bool IsRPCRunning()
|
|
0 commit comments