From 565a6d7032ccb8c16183840814ca98076f012d05 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 11 Oct 2016 05:01:25 +0200 Subject: [PATCH] refactor(controller): Force shutdown if dead lock If the mutex haven't been successfully locked after 3 seconds, force shutdown by raising SIGKILL. This to ensure termination in case of a dead lock. --- include/components/controller.hpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/include/components/controller.hpp b/include/components/controller.hpp index 9bbff24a..75354657 100644 --- a/include/components/controller.hpp +++ b/include/components/controller.hpp @@ -56,7 +56,12 @@ class controller { * threads and spawned processes */ ~controller() noexcept { - std::lock_guard lck(m_lock); + if (!m_mutex.try_lock_for(3s)) { + m_log.warn("Failed to acquire lock for 3s... Forcing shutdown using SIGKILL"); + raise(SIGKILL); + } + + std::lock_guard guard(m_mutex, std::adopt_lock); m_log.trace("controller: Stop modules"); for (auto&& block : m_modules) { @@ -385,13 +390,15 @@ class controller { } void on_module_update(string module_name) { - std::lock_guard lck(m_lock); + if (!m_mutex.try_lock()) + return; + std::lock_guard guard(m_mutex, std::adopt_lock); if (!m_running) return; - if (!m_throttler->passthrough(throttle_util::strategy::try_once_or_leave_yolo{})) { - m_log.trace("Throttled"); + if (!m_throttler->passthrough(m_throttle_strategy)) { + m_log.trace("controller: Update event throttled"); return; } @@ -457,7 +464,9 @@ class controller { } void on_module_click(string input) { - std::lock_guard lck(m_lock); + if (!m_mutex.try_lock()) + return; + std::lock_guard guard(m_mutex, std::adopt_lock); for (auto&& block : m_modules) { for (auto&& module : block.second) { @@ -492,7 +501,7 @@ class controller { unique_ptr m_bar; unique_ptr m_traymanager; - threading_util::spin_lock m_lock; + std::timed_mutex m_mutex; stateflag m_stdout{false}; stateflag m_running{false}; @@ -506,6 +515,7 @@ class controller { map> m_modules; unique_ptr m_throttler; + throttle_util::strategy::try_once_or_leave_yolo m_throttle_strategy; }; LEMONBUDDY_NS_END