From 59c360fe63912d73c601df51756feef0afd77659 Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Tue, 18 Aug 2020 15:40:49 -0700 Subject: [PATCH 1/2] Asynchronously wait for load node service response Fixes #171 By asychronously waiting for the service response, we can monitor if launch is shutting down and abandon the request so we don't block the shutdown process. Signed-off-by: Jacob Perron --- .../actions/load_composable_nodes.py | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/launch_ros/launch_ros/actions/load_composable_nodes.py b/launch_ros/launch_ros/actions/load_composable_nodes.py index b08d2b8c..befe5408 100644 --- a/launch_ros/launch_ros/actions/load_composable_nodes.py +++ b/launch_ros/launch_ros/actions/load_composable_nodes.py @@ -14,6 +14,8 @@ """Module for the LoadComposableNodes action.""" +import threading + from typing import List from typing import Optional from typing import Text @@ -99,13 +101,33 @@ def _load_node( ) ) return - self.__logger.debug( - "Calling the '{}' service with request '{}'".format( - self.__rclpy_load_node_client.srv_name, request - ) - ) - response = self.__rclpy_load_node_client.call(request) + + # Asynchronously wait on service call so that we can periodically check for shutdown + event = threading.Event() + + def unblock(future): + nonlocal event + event.set() + + response_future = self.__rclpy_load_node_client.call_async(request) + response_future.add_done_callback(unblock) + + while not event.wait(1.0): + if context.is_shutdown: + self.__logger.warning( + "Abandoning wait for the '{}' service response, due to shutdown.".format( + self.__rclpy_load_node_client.srv_name), + ) + response_future.cancel() + return + + # Get response + if response_future.exception() is not None: + raise response_future.exception() + response = response_future.result() + self.__logger.debug("Received response '{}'".format(response)) + node_name = response.full_node_name if response.full_node_name else request.node_name if response.success: if node_name is not None: From 15ec341894ec22a74a413cb581c5f4ff69477cf4 Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Tue, 18 Aug 2020 15:43:15 -0700 Subject: [PATCH 2/2] Add back debug log It was accidentally removed. Signed-off-by: Jacob Perron --- launch_ros/launch_ros/actions/load_composable_nodes.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/launch_ros/launch_ros/actions/load_composable_nodes.py b/launch_ros/launch_ros/actions/load_composable_nodes.py index befe5408..2116119b 100644 --- a/launch_ros/launch_ros/actions/load_composable_nodes.py +++ b/launch_ros/launch_ros/actions/load_composable_nodes.py @@ -109,6 +109,12 @@ def unblock(future): nonlocal event event.set() + self.__logger.debug( + "Calling the '{}' service with request '{}'".format( + self.__rclpy_load_node_client.srv_name, request + ) + ) + response_future = self.__rclpy_load_node_client.call_async(request) response_future.add_done_callback(unblock)