From bc13c4bdcc8cffe74791b113de4c4618be48b9f2 Mon Sep 17 00:00:00 2001 From: khromenokroman Date: Fri, 23 Aug 2024 13:46:57 +0200 Subject: [PATCH] iox-#2330 Add shutdown method to Systemd_service_handler Implemented a shutdown method in Systemd_service_handler to allow graceful termination of the watchdog thread. This enhancement ensures proper resource cleanup and improves system robustness by avoiding potential hang-ups during the shutdown process. --- .../iceoryx_posh/internal/roudi/roudi.hpp | 24 ++++++++++++++----- iceoryx_posh/source/roudi/roudi.cpp | 3 +++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp b/iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp index 6ac9df26d0..452ed5a31e 100644 --- a/iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp +++ b/iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp @@ -56,6 +56,7 @@ class I_systemd I_systemd& operator=(I_systemd&& other) = default; virtual void process_notify() = 0; + virtual void shutdown() = 0; protected: I_systemd() = default; @@ -67,10 +68,9 @@ class Systemd_service_handler final : public I_systemd public: Systemd_service_handler() = default; Systemd_service_handler(Systemd_service_handler const& other) = delete; - Systemd_service_handler(Systemd_service_handler&& other) = default; + Systemd_service_handler(Systemd_service_handler&& other) = delete; Systemd_service_handler& operator=(Systemd_service_handler const& other) = delete; Systemd_service_handler& operator=(Systemd_service_handler&& other) = default; - static constexpr uint16_t SIZE_STRING = 4096; ~Systemd_service_handler() final { @@ -84,6 +84,12 @@ class Systemd_service_handler final : public I_systemd m_listen_thread_watchdog.join(); } } + + void shutdown() final + { + m_shutdown.store(true); + } + void process_notify() final { /* @@ -134,7 +140,7 @@ class Systemd_service_handler final : public I_systemd IOX_LOG(DEBUG, "WatchDog READY=1"); IOX_LOG(INFO, "Start watchdog"); - while (true) + while (!m_shutdown.load()) { auto result_watchdog = IOX_POSIX_CALL(sd_notify)(0, "WATCHDOG=1").successReturnValue(1).evaluate(); if (result_watchdog.has_error()) @@ -152,6 +158,10 @@ class Systemd_service_handler final : public I_systemd private: std::thread m_listen_thread_watchdog; // 8 + public: + static constexpr uint16_t SIZE_STRING = 4096; // 2 + private: + std::atomic_bool m_shutdown{false}; // 1 }; #else class Systemd_service_handler final : public I_systemd @@ -163,14 +173,16 @@ class Systemd_service_handler final : public I_systemd Systemd_service_handler& operator=(Systemd_service_handler const& other) = delete; Systemd_service_handler& operator=(Systemd_service_handler&& other) = default; - ~Systemd_service_handler() final + ~Systemd_service_handler() final = default; + void process_notify() final { + // empty implementation } - void process_notify() final + void shutdown() final { // empty implementation } -} +}; #endif } // namespace systemd diff --git a/iceoryx_posh/source/roudi/roudi.cpp b/iceoryx_posh/source/roudi/roudi.cpp index 45ec079336..07b5cf70ee 100644 --- a/iceoryx_posh/source/roudi/roudi.cpp +++ b/iceoryx_posh/source/roudi/roudi.cpp @@ -269,6 +269,9 @@ void RouDi::processRuntimeMessages(runtime::IpcInterfaceCreator&& roudiIpcInterf processMessage(message, cmd, runtimeName); } } + if (!m_runHandleRuntimeMessageThread.load()){ + roudi_systemd.shutdown(); + } } version::VersionInfo RouDi::parseRegisterMessage(const runtime::IpcMessage& message,