Skip to content

Commit

Permalink
AP_HAL_Linux: delay signal handlers setup
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Oleksiy Protas authored and peterbarker committed Apr 5, 2024
1 parent f1d37fc commit 364e6f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 7 additions & 1 deletion libraries/AP_HAL_Linux/HAL_Linux_Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 3 additions & 1 deletion libraries/AP_HAL_Linux/HAL_Linux_Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <AP_HAL/AP_HAL.h>

#include <signal.h>

class HAL_Linux : public AP_HAL::HAL {
public:
HAL_Linux();
Expand All @@ -12,7 +14,7 @@ class HAL_Linux : public AP_HAL::HAL {
static void exit_signal_handler(int);

protected:
bool _should_exit = false;
volatile sig_atomic_t _should_exit = false;
};

#if HAL_NUM_CAN_IFACES
Expand Down

0 comments on commit 364e6f0

Please sign in to comment.