Quellcode durchsuchen

Fix rclcpp SIGINT & SIGTERM handling (#636)

Fixes SIGINT and SIGTERM handling to adequately shut down o3de processes that host an rclcpp instance, especially to fix process termination via `CTRL+C` keyboard command

Cf. https://github.com/ros2/rclcpp/issues/317#issuecomment-380318892

Signed-off-by: Lars Gleim <[email protected]>
Co-authored-by: Adam Dąbrowski <[email protected]>
Lars Gleim vor 1 Jahr
Ursprung
Commit
86d092ae8f
1 geänderte Dateien mit 10 neuen und 0 gelöschten Zeilen
  1. 10 0
      Gems/ROS2/Code/Source/SystemComponents/ROS2SystemComponent.cpp

+ 10 - 0
Gems/ROS2/Code/Source/SystemComponents/ROS2SystemComponent.cpp

@@ -5,6 +5,8 @@
  * SPDX-License-Identifier: Apache-2.0 OR MIT
  *
  */
+#include <signal.h>
+
 #include "ROS2SystemComponent.h"
 #include <Lidar/LidarCore.h>
 #include <ROS2/Clock/PhysicallyStableClock.h>
@@ -99,6 +101,14 @@ namespace ROS2
     void ROS2SystemComponent::Init()
     {
         rclcpp::init(0, 0);
+        
+        // handle signals, e.g. via `Ctrl+C` hotkey or `kill` command 
+        auto handler = [](int sig){
+            rclcpp::shutdown(); // shutdown rclcpp
+            std::raise(sig); // shutdown o3de
+            };
+        signal(SIGINT, handler);
+        signal(SIGTERM, handler);
     }
 
     void ROS2SystemComponent::InitClock()