From 6177388659f3851bfb0914c0fe32174b9fa429cb Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Mon, 22 Apr 2024 11:31:45 -0700 Subject: [PATCH] add signal handler SIGIN/SIGTERM to ros2run. Signed-off-by: Tomoya Fujita --- ros2run/ros2run/api/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ros2run/ros2run/api/__init__.py b/ros2run/ros2run/api/__init__.py index fd592bccd..b18371f01 100644 --- a/ros2run/ros2run/api/__init__.py +++ b/ros2run/ros2run/api/__init__.py @@ -62,6 +62,16 @@ def run_executable(*, path, argv, prefix=None): cmd = prefix + cmd process = subprocess.Popen(cmd) + + # add signal handler for the parent process, so that we can finalize the child process + def signal_handler(sig, frame): + print(ROS2RUN_MSG_PREFIX, 'Received signal: ', signal.strsignal(sig)) + if process.poll() is None: + # If child process is running, forward the signal to it + process.send_signal(sig) + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTERM, signal_handler) + while process.returncode is None: try: process.communicate()