Skip to content

Commit

Permalink
iox-eclipse-iceoryx#2330 Improve thread management and error logging
Browse files Browse the repository at this point in the history
Refactored thread naming and error handling for the watchdog thread. Replaced raw `sd_notify` usage with `IOX_POSIX_CALL` to better handle errors. Enhanced log messages for clearer error reporting.
  • Loading branch information
khromenokroman committed Aug 23, 2024
1 parent 76dd33f commit 485bfa4
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions iceoryx_posh/source/roudi/roudi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ void RouDi::shutdown() noexcept
* the 'listen_thread_watchdog' has finished, hence ensuring a
* proper termination of the entire application.
*/
if (m_listen_thread_watchdog.joinable()) {
if (m_listen_thread_watchdog.joinable())
{
m_listen_thread_watchdog.join();
}
#endif
Expand Down Expand Up @@ -275,34 +276,32 @@ void RouDi::processRuntimeMessages(runtime::IpcInterfaceCreator&& roudiIpcInterf
{
IOX_LOG(WARN, "Run APP in unit(systemd)");
m_listen_thread_watchdog = std::thread([this] {
if (auto wdres = sd_notify(0, "READY=1") < 0)
bool status_change_name = iox::setThreadName("watchdog");
if (!status_change_name)
{
IOX_LOG(ERROR, "Can not set name for thread watchdog");
}
auto result_ready = IOX_POSIX_CALL(sd_notify)(0, "READY=1").successReturnValue(1).evaluate();
if (result_ready.has_error())
{
std::array<char, SIZE_ERROR_MESSAGE> buf{};
strerror_r(-static_cast<int>(wdres), buf.data(), buf.size());
IOX_LOG(ERROR, "WatchDogError: " << std::string(buf.data()));
return;
IOX_LOG(ERROR,
"Failed to send READY=1 signal. Error: " + result_ready.get_error().getHumanReadableErrnum());
}
IOX_LOG(DEBUG, "WatchDog READY=1");

IOX_LOG(INFO, "Start watchdog");
while (m_runHandleRuntimeMessageThread.load())
{
if (auto wdres = sd_notify(0, "WATCHDOG=1") < 0)
auto result_watchdog = IOX_POSIX_CALL(sd_notify)(0, "WATCHDOG=1").successReturnValue(1).evaluate();
if (result_watchdog.has_error())
{
std::array<char, SIZE_ERROR_MESSAGE> buf{};
strerror_r(-static_cast<int>(wdres), buf.data(), buf.size());
IOX_LOG(ERROR, "WatchDogError: " << std::string(buf.data()));
return;
IOX_LOG(ERROR,
"Failed to send WATCHDOG=1 signal. Error: "
+ result_watchdog.get_error().getHumanReadableErrnum());
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
if (pthread_setname_np(m_listen_thread_watchdog.native_handle(), "watchdog") != 0)
{
std::array<char, SIZE_ERROR_MESSAGE> buf{};
strerror_r(errno, buf.data(), buf.size());
IOX_LOG(ERROR, "Can not set name for thread watchdog: " << std::string(buf.data()));
}
}
#endif

Expand Down

0 comments on commit 485bfa4

Please sign in to comment.