diff --git a/launch_ros/launch_ros/actions/composable_node_container.py b/launch_ros/launch_ros/actions/composable_node_container.py index b17dad824..15bfb5b09 100644 --- a/launch_ros/launch_ros/actions/composable_node_container.py +++ b/launch_ros/launch_ros/actions/composable_node_container.py @@ -18,6 +18,8 @@ from typing import Optional from launch.action import Action +from launch.actions import RegisterEventHandler +from launch.event_handlers.on_process_start import OnProcessStart from launch.launch_context import LaunchContext from launch.some_substitutions_type import SomeSubstitutionsType @@ -61,10 +63,20 @@ def execute(self, context: LaunchContext) -> Optional[List[Action]]: load_actions = None # type: Optional[List[Action]] if self.__composable_node_descriptions is not None: from .load_composable_nodes import LoadComposableNodes - load_actions = [LoadComposableNodes( - composable_node_descriptions=self.__composable_node_descriptions, - target_container=self - )] + # Perform load action once the container has started. + load_actions = [ + RegisterEventHandler( + event_handler=OnProcessStart( + target_action=self, + on_start=[ + LoadComposableNodes( + composable_node_descriptions=self.__composable_node_descriptions, + target_container=self + ) + ] + ) + ) + ] container_actions = super().execute(context) # type: Optional[List[Action]] if container_actions is not None and load_actions is not None: return container_actions + load_actions diff --git a/launch_ros/launch_ros/actions/load_composable_nodes.py b/launch_ros/launch_ros/actions/load_composable_nodes.py index 9cfafb553..1cf87636f 100644 --- a/launch_ros/launch_ros/actions/load_composable_nodes.py +++ b/launch_ros/launch_ros/actions/load_composable_nodes.py @@ -20,9 +20,6 @@ import composition_interfaces.srv from launch.action import Action -from launch.actions import RegisterEventHandler -from launch.event_handlers.on_process_start import OnProcessStart -from launch.events.process import ProcessStarted from launch.launch_context import LaunchContext import launch.logging from launch.utilities import ensure_argument_type @@ -51,6 +48,9 @@ def __init__( The container node is expected to provide a `~/_container/load_node` service for loading purposes. Loading will be performed sequentially. + When executed, this action will block until the container's load service is available. + Make sure any LoadComposableNode action is executed only after its container processes + has started. :param composable_node_descriptions: descriptions of composable nodes to be loaded :param target_container: the container to load the nodes into @@ -156,15 +156,6 @@ def _load_in_sequence( ) ) - def _on_container_start( - self, - event: ProcessStarted, - context: LaunchContext - ) -> Optional[List[Action]]: - """Load nodes on container process start.""" - self._load_in_sequence(self.__composable_node_descriptions, context) - return None - def execute( self, context: LaunchContext @@ -176,10 +167,5 @@ def execute( self.__target_container.node_name ) ) - # Perform load action once the container has started. - return [RegisterEventHandler( - event_handler=OnProcessStart( - target_action=self.__target_container, - on_start=self._on_container_start, - ) - )] + # Assume user has configured `LoadComposableNodes` to happen after container action + self._load_in_sequence(self.__composable_node_descriptions, context)