From abd96eb089d08eb2eefa4264f87978d6efbd772a Mon Sep 17 00:00:00 2001
From: Patrick Ziegler
Date: Fri, 15 Oct 2021 10:33:10 +0200
Subject: [PATCH] Enable module in start funcion (#2538)
Before it was enabled by default. That means if the constructor fails,
the destructor will complain that the module was not stopped before
deconstructing.
We can't just call stop if module creation fails because the module is
only partially initialized.
---
include/modules/meta/base.hpp | 3 ++-
include/modules/meta/base.inl | 5 +++++
include/modules/meta/event_module.hpp | 1 +
include/modules/meta/inotify_module.hpp | 1 +
include/modules/meta/static_module.hpp | 1 +
include/modules/meta/timer_module.hpp | 1 +
src/modules/ipc.cpp | 1 +
src/modules/script.cpp | 1 +
8 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/include/modules/meta/base.hpp b/include/modules/meta/base.hpp
index f842e907..60a82e5e 100644
--- a/include/modules/meta/base.hpp
+++ b/include/modules/meta/base.hpp
@@ -158,6 +158,7 @@ namespace modules {
bool visible() const override;
+ void start() override;
void join() final override;
void stop() override;
void halt(string error_message) override;
@@ -218,7 +219,7 @@ namespace modules {
bool m_handle_events{true};
private:
- atomic m_enabled{true};
+ atomic m_enabled{false};
atomic m_visible{true};
atomic m_changed{true};
string m_cache;
diff --git a/include/modules/meta/base.inl b/include/modules/meta/base.inl
index ab53cb59..0d3bd82c 100644
--- a/include/modules/meta/base.inl
+++ b/include/modules/meta/base.inl
@@ -65,6 +65,11 @@ namespace modules {
return static_cast(m_enabled);
}
+ template
+ void module::start() {
+ m_enabled = true;
+ }
+
template
void module::join() {
for (auto&& thread_ : m_threads) {
diff --git a/include/modules/meta/event_module.hpp b/include/modules/meta/event_module.hpp
index 025fdccd..05d37050 100644
--- a/include/modules/meta/event_module.hpp
+++ b/include/modules/meta/event_module.hpp
@@ -11,6 +11,7 @@ namespace modules {
using module::module;
void start() override {
+ this->module::start();
this->m_mainthread = thread(&event_module::runner, this);
}
diff --git a/include/modules/meta/inotify_module.hpp b/include/modules/meta/inotify_module.hpp
index 2c69e948..06877c14 100644
--- a/include/modules/meta/inotify_module.hpp
+++ b/include/modules/meta/inotify_module.hpp
@@ -12,6 +12,7 @@ namespace modules {
using module::module;
void start() override {
+ this->module::start();
this->m_mainthread = thread(&inotify_module::runner, this);
}
diff --git a/include/modules/meta/static_module.hpp b/include/modules/meta/static_module.hpp
index 8bbef10f..ce4758a4 100644
--- a/include/modules/meta/static_module.hpp
+++ b/include/modules/meta/static_module.hpp
@@ -11,6 +11,7 @@ namespace modules {
using module::module;
void start() override {
+ this->module::start();
CAST_MOD(Impl)->update();
CAST_MOD(Impl)->broadcast();
}
diff --git a/include/modules/meta/timer_module.hpp b/include/modules/meta/timer_module.hpp
index e2ca716a..a9ede914 100644
--- a/include/modules/meta/timer_module.hpp
+++ b/include/modules/meta/timer_module.hpp
@@ -13,6 +13,7 @@ namespace modules {
using module::module;
void start() override {
+ this->module::start();
this->m_mainthread = thread(&timer_module::runner, this);
}
diff --git a/src/modules/ipc.cpp b/src/modules/ipc.cpp
index 76bcaa6c..9aba10be 100644
--- a/src/modules/ipc.cpp
+++ b/src/modules/ipc.cpp
@@ -68,6 +68,7 @@ namespace modules {
* Start module and run first defined hook if configured to
*/
void ipc_module::start() {
+ this->module::start();
m_mainthread = thread([&] {
m_log.trace("%s: Thread id = %i", this->name(), concurrency_util::thread_id(this_thread::get_id()));
update();
diff --git a/src/modules/script.cpp b/src/modules/script.cpp
index 983ac8c9..71fe714a 100644
--- a/src/modules/script.cpp
+++ b/src/modules/script.cpp
@@ -33,6 +33,7 @@ namespace modules {
* Start the module worker
*/
void script_module::start() {
+ this->module::start();
m_mainthread = thread([&] {
try {
while (running()) {