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()