From a2a21c9d95887062d9e27323b3d3d49331eaaeb5 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Tue, 19 Nov 2024 19:53:23 +0000 Subject: [PATCH] mutexing of timer list --- include/dpp/cluster.h | 5 +++++ src/dpp/cluster.cpp | 14 +++++++++----- src/dpp/cluster/timer.cpp | 1 - 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index ab43fad341..c30c378735 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -185,6 +185,11 @@ class DPP_EXPORT cluster { */ std::unique_ptr engine_thread{nullptr}; + /** + * @brief Protection mutex for timers + */ + std::mutex timer_guard; + public: /** * @brief Current bot token for all shards on this cluster and all commands sent via HTTP diff --git a/src/dpp/cluster.cpp b/src/dpp/cluster.cpp index 7daa0735fa..0ff9bf7b2a 100644 --- a/src/dpp/cluster.cpp +++ b/src/dpp/cluster.cpp @@ -313,12 +313,16 @@ void cluster::start(bool return_after) { void cluster::shutdown() { /* Signal termination */ terminating = true; - /* Free memory for active timers */ - for (auto & t : timer_list) { - delete t.second; + { + std::lock_guard l(timer_guard); + log(ll_info, "Terminating " + std::to_string(timer_list.size()) + "timers"); + /* Free memory for active timers */ + for (auto &t: timer_list) { + delete t.second; + } + timer_list.clear(); + next_timer.clear(); } - timer_list.clear(); - next_timer.clear(); /* Terminate shards */ for (const auto& sh : shards) { log(ll_info, "Terminating shard id " + std::to_string(sh.second->shard_id)); diff --git a/src/dpp/cluster/timer.cpp b/src/dpp/cluster/timer.cpp index c255626d3b..961935c3a7 100644 --- a/src/dpp/cluster/timer.cpp +++ b/src/dpp/cluster/timer.cpp @@ -25,7 +25,6 @@ namespace dpp { timer lasthandle = 1; -std::mutex timer_guard; timer cluster::start_timer(timer_callback_t on_tick, uint64_t frequency, timer_callback_t on_stop) { std::lock_guard l(timer_guard);