From 364e6f06f3cb117115d6a2fdf33895a489c2b3a8 Mon Sep 17 00:00:00 2001 From: Oleksiy Protas Date: Mon, 18 Dec 2023 22:52:09 +0200 Subject: [PATCH] AP_HAL_Linux: delay signal handlers setup Allow default signals before full initialization in Linux, this makes sure we don't get an unkillable process if it hangs on initialization Exit flag marked volatile to counteract possible compiler optimization due to the handler code running in a different context --- libraries/AP_HAL_Linux/HAL_Linux_Class.cpp | 8 +++++++- libraries/AP_HAL_Linux/HAL_Linux_Class.h | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp b/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp index 4b2cd90e28..a0be63c44a 100644 --- a/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp +++ b/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp @@ -467,7 +467,11 @@ void HAL_Linux::run(int argc, char* const argv[], Callbacks* callbacks) const } } - setup_signal_handlers(); + // NOTE: signal handlers are only set before the main loop, so + // that if anything before the loops hangs, the default signals + // can still stop the process proprely, although without proper + // teardown. + // This isn't perfect, but still prevents an unkillable process. scheduler->init(); gpio->init(); @@ -497,6 +501,8 @@ void HAL_Linux::run(int argc, char* const argv[], Callbacks* callbacks) const AP_Module::call_hook_setup_complete(); #endif + setup_signal_handlers(); + while (!_should_exit) { callbacks->loop(); } diff --git a/libraries/AP_HAL_Linux/HAL_Linux_Class.h b/libraries/AP_HAL_Linux/HAL_Linux_Class.h index 87089427aa..a0dc7f2045 100644 --- a/libraries/AP_HAL_Linux/HAL_Linux_Class.h +++ b/libraries/AP_HAL_Linux/HAL_Linux_Class.h @@ -2,6 +2,8 @@ #include +#include + class HAL_Linux : public AP_HAL::HAL { public: HAL_Linux(); @@ -12,7 +14,7 @@ public: static void exit_signal_handler(int); protected: - bool _should_exit = false; + volatile sig_atomic_t _should_exit = false; }; #if HAL_NUM_CAN_IFACES