Skip to content

Commit

Permalink
Asynchronously wait for load node service response (#174)
Browse files Browse the repository at this point in the history
* 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 <jacob@openrobotics.org>

* Add back debug log

It was accidentally removed.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
  • Loading branch information
jacobperron committed Nov 19, 2020
1 parent 90ed6bd commit 20fce6a
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion launch_ros/launch_ros/actions/load_composable_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Module for the LoadComposableNodes action."""

import threading

from typing import List
from typing import Optional

Expand Down Expand Up @@ -86,13 +88,39 @@ def _load_node(
)
)
return

# Asynchronously wait on service call so that we can periodically check for shutdown
event = threading.Event()

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 = self.__rclpy_load_node_client.call(request)

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:
Expand Down

0 comments on commit 20fce6a

Please sign in to comment.