fix: preserve SIGTERM shutdown on older ROS 2

Forward SIGTERM to rclcpp's SIGINT path on releases that predate native SIGTERM handling, while leaving shutdown signal ownership with rclcpp on newer releases.
This commit is contained in:
slz
2026-08-11 14:32:26 +08:00
parent a1146a6e54
commit 1733d513f5
+23 -3
View File
@@ -22,6 +22,13 @@
#include <ament_index_cpp/get_package_share_directory.hpp> #include <ament_index_cpp/get_package_share_directory.hpp>
#include <ament_index_cpp/get_package_prefix.hpp> #include <ament_index_cpp/get_package_prefix.hpp>
#include <rclcpp_components/register_node_macro.hpp> #include <rclcpp_components/register_node_macro.hpp>
#if __has_include(<rclcpp/version.h>)
#include <rclcpp/version.h>
#define ORBBEC_RCLCPP_HANDLES_SIGTERM \
((RCLCPP_VERSION_MAJOR > 13) || (RCLCPP_VERSION_MAJOR == 13 && RCLCPP_VERSION_MINOR >= 1))
#else
#define ORBBEC_RCLCPP_HANDLES_SIGTERM 0
#endif
#include <rcutils/logging.h> #include <rcutils/logging.h>
#include <csignal> #include <csignal>
#include <sys/mman.h> #include <sys/mman.h>
@@ -113,6 +120,14 @@ void crashSignalHandler(int sig) {
_exit(sig); // Use _exit instead of exit to avoid cleanup that may crash _exit(sig); // Use _exit instead of exit to avoid cleanup that may crash
} }
#if !ORBBEC_RCLCPP_HANDLES_SIGTERM
void forwardSigtermToRclcpp(int) {
// Older rclcpp versions such as Foxy's only handle SIGINT. Forward SIGTERM to that signal-safe
// shutdown path instead of calling rclcpp::shutdown() directly from this signal handler.
kill(getpid(), SIGINT);
}
#endif
namespace orbbec_camera { namespace orbbec_camera {
backward::SignalHandling OBCameraNodeDriver::sh; backward::SignalHandling OBCameraNodeDriver::sh;
@@ -238,9 +253,14 @@ OBCameraNodeDriver::~OBCameraNodeDriver() {
} }
void OBCameraNodeDriver::init() { void OBCameraNodeDriver::init() {
// Keep SIGINT/SIGTERM managed by rclcpp. Overriding them from a composable node bypasses // Keep shutdown signals managed by rclcpp. Overriding them from a composable node bypasses its
// rclcpp's deferred signal handling and can leave SDK streaming threads running after the ROS // deferred signal handling and can leave SDK streaming threads running after the ROS context has
// context has already been shut down. // already been shut down.
#if !ORBBEC_RCLCPP_HANDLES_SIGTERM
// Older rclcpp versions such as Foxy's predate native SIGTERM handling, so translate it to the
// SIGINT path that rclcpp does manage. Newer distributions handle both signals themselves.
signal(SIGTERM, forwardSigtermToRclcpp);
#endif
signal(SIGSEGV, crashSignalHandler); // segment fault signal(SIGSEGV, crashSignalHandler); // segment fault
signal(SIGABRT, crashSignalHandler); // abort signal(SIGABRT, crashSignalHandler); // abort
signal(SIGFPE, crashSignalHandler); // float point exception signal(SIGFPE, crashSignalHandler); // float point exception