How can I make ROS2 launch quit on node crash?

asked 2020-02-10 09:11:09 -0600

allezxandre gravatar image

updated 2020-02-10 18:48:15 -0600

jayess gravatar image

I'm launching multiple nodes using ROS2 launch. I want the whole system to come down if any of the node encounters an exception.

Right now, my launch file looks like this:

from os.path import join

import launch
import launch.actions
import launch.substitutions
import launch_ros.actions
from ament_index_python import get_package_share_directory
from launch.actions import LogInfo
from launch.event_handlers import OnProcessExit


def generate_launch_description():
    emit_shutdown_action = launch.actions.Shutdown(reason='launch is shutting down')
    ld = launch.LaunchDescription([
        launch_ros.actions.Node(
            package='node_1',
            node_executable='node1',
        ),
        launch_ros.actions.Node(
            package="node_2",
            node_executable='node_2',
            # METHOD 1: Node on_exit attribute
            on_exit=[LogInfo(msg=["Node 2 stopped. Stopping everything..."]),
                     emit_shutdown_action],
        ),
    ])
    # METHOD 2: ProcessExit event handler
    ld.add_action(launch.actions.RegisterEventHandler(event_handler=OnProcessExit(
        on_exit=[LogInfo(msg=["A Node stopped. Stopping everything..."]),
                 emit_shutdown_action]
    )))

    return ld

Here, I'm trying to effectively catch an unexpected crash of a node in two ways:

  1. through the 'node_2' process on_exit attribute
  2. through the ProcessExit event handler

However, it appears my nodes raising exceptions does not initiate a shut down, and instead, after raising an exception I only receive a log like such:

[ERROR] [node_2]: process has died [pid 11784, exit code 1, cmd '[..]/node_2' ].

indicating that the launch system is aware of my node dying, although no shutdown was triggered.

What should I do to make the launch system shutdown when one of the nodes dies?

edit retag flag offensive close merge delete