From 5fd8c50dde53b4da9d7e6c0a6dad924bf7c60de7 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 18 Oct 2016 11:49:13 +0200 Subject: [PATCH] fix(i3): Shutdown on socket disconnect --- include/modules/i3.hpp | 44 +++++++++++++++------------------------- include/modules/meta.hpp | 9 ++++---- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/include/modules/i3.hpp b/include/modules/i3.hpp index 4e14a50e..5d3efef2 100644 --- a/include/modules/i3.hpp +++ b/include/modules/i3.hpp @@ -2,8 +2,8 @@ #include -#include "config.hpp" #include "components/config.hpp" +#include "config.hpp" #include "drawtypes/iconset.hpp" #include "drawtypes/label.hpp" #include "modules/meta.hpp" @@ -44,18 +44,6 @@ namespace modules { public: using event_module::event_module; - ~i3_module() { - // Shutdown ipc connection {{{ - - try { - shutdown(m_ipc.get_event_socket_fd(), SHUT_RD); - shutdown(m_ipc.get_main_socket_fd(), SHUT_RD); - } catch (const std::exception& err) { - } - - // }}} - } - void setup() { // Load configuration values {{{ @@ -106,24 +94,22 @@ namespace modules { // }}} } - bool has_event() { - // Wait for ipc events {{{ + void stop() { + // Shutdown ipc connection when before stopping the module {{{ - try { - m_ipc.handle_event(); - return true; - } catch (const std::exception& err) { - if (enabled()) { - m_log.err("%s: Error while handling ipc event, stopping module...", name()); - m_log.err("%s: %s", name(), err.what()); - stop(); - } - return false; - } + shutdown(m_ipc.get_event_socket_fd(), SHUT_RD); + shutdown(m_ipc.get_main_socket_fd(), SHUT_RD); + event_module::stop(); // }}} } + bool has_event() { + if (!m_ipc.handle_event()) + throw module_error("Socket connection closed..."); + return true; + } + bool update() { // Refresh workspace data {{{ @@ -159,7 +145,8 @@ namespace modules { flag = i3_flag::WORKSPACE_FOCUSED; else if (workspace->urgent) flag = i3_flag::WORKSPACE_URGENT; - else if (!workspace->visible || (workspace->visible && workspace->output != focused_output)) + else if (!workspace->visible || + (workspace->visible && workspace->output != focused_output)) flag = i3_flag::WORKSPACE_UNFOCUSED; else flag = i3_flag::WORKSPACE_VISIBLE; @@ -184,7 +171,8 @@ namespace modules { label->replace_token("%name%", wsname); label->replace_token("%icon%", icon->m_text); label->replace_token("%index%", to_string(workspace->num)); - m_workspaces.emplace_back(make_unique(workspace->num, flag, std::move(label))); + m_workspaces.emplace_back( + make_unique(workspace->num, flag, std::move(label))); } return true; diff --git a/include/modules/meta.hpp b/include/modules/meta.hpp index c6357391..3ef00809 100644 --- a/include/modules/meta.hpp +++ b/include/modules/meta.hpp @@ -210,13 +210,14 @@ namespace modules { } void stop() { + wakeup(); + std::lock_guard lck(this->update_lock); { if (!enabled()) return; m_log.trace("%s: Stop", name()); enable(false); - wakeup(); if (!on_stop.empty()) on_stop.emit(name()); } @@ -391,7 +392,7 @@ namespace modules { CAST_MODULE(Impl)->broadcast(); CAST_MODULE(Impl)->sleep(m_interval); } - } catch (const application_error& err) { + } catch (const std::exception& err) { this->m_log.err("%s: %s", this->name(), err.what()); this->m_log.warn("Stopping '%s'...", this->name()); CAST_MODULE(Impl)->stop(); @@ -435,7 +436,7 @@ namespace modules { lck.unlock(); CAST_MODULE(Impl)->idle(); } - } catch (const application_error& err) { + } catch (const std::exception& err) { this->m_log.err("%s: %s", this->name(), err.what()); this->m_log.warn("Stopping '%s'...", this->name()); CAST_MODULE(Impl)->stop(); @@ -467,7 +468,7 @@ namespace modules { std::lock_guard lck(this->update_lock); CAST_MODULE(Impl)->poll_events(); } - } catch (const application_error& err) { + } catch (const std::exception& err) { this->m_log.err("%s: %s", this->name(), err.what()); this->m_log.warn("Stopping '%s'...", this->name()); CAST_MODULE(Impl)->stop();