rpc: Add mutex to guard deadlineTimers

This commit is contained in:
João Barbosa 2020-04-29 10:40:39 +01:00
parent 0ef0d33f75
commit a2e6db5c4f

View file

@ -25,7 +25,8 @@ static std::string rpcWarmupStatus GUARDED_BY(cs_rpcWarmup) = "RPC server starte
/* Timer-creating functions */ /* Timer-creating functions */
static RPCTimerInterface* timerInterface = nullptr; static RPCTimerInterface* timerInterface = nullptr;
/* Map of name to timer. */ /* Map of name to timer. */
static std::map<std::string, std::unique_ptr<RPCTimerBase> > deadlineTimers; static Mutex g_deadline_timers_mutex;
static std::map<std::string, std::unique_ptr<RPCTimerBase> > deadlineTimers GUARDED_BY(g_deadline_timers_mutex);
static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler); static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler);
struct RPCCommandExecutionInfo struct RPCCommandExecutionInfo
@ -298,7 +299,7 @@ void InterruptRPC()
void StopRPC() void StopRPC()
{ {
LogPrint(BCLog::RPC, "Stopping RPC\n"); LogPrint(BCLog::RPC, "Stopping RPC\n");
deadlineTimers.clear(); WITH_LOCK(g_deadline_timers_mutex, deadlineTimers.clear());
DeleteAuthCookie(); DeleteAuthCookie();
g_rpcSignals.Stopped(); g_rpcSignals.Stopped();
} }
@ -486,6 +487,7 @@ void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nS
{ {
if (!timerInterface) if (!timerInterface)
throw JSONRPCError(RPC_INTERNAL_ERROR, "No timer handler registered for RPC"); throw JSONRPCError(RPC_INTERNAL_ERROR, "No timer handler registered for RPC");
LOCK(g_deadline_timers_mutex);
deadlineTimers.erase(name); deadlineTimers.erase(name);
LogPrint(BCLog::RPC, "queue run of timer %s in %i seconds (using %s)\n", name, nSeconds, timerInterface->Name()); LogPrint(BCLog::RPC, "queue run of timer %s in %i seconds (using %s)\n", name, nSeconds, timerInterface->Name());
deadlineTimers.emplace(name, std::unique_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000))); deadlineTimers.emplace(name, std::unique_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000)));