Skip to content

Commit ce395f7

Browse files
authored
Asynchronously wait for load node service response (#174) (#240)
* 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 <[email protected]> * Add back debug log It was accidentally removed. Signed-off-by: Jacob Perron <[email protected]>
1 parent 8f7bfc5 commit ce395f7

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

launch_ros/launch_ros/actions/load_composable_nodes.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""Module for the LoadComposableNodes action."""
1616

17+
import threading
18+
1719
from typing import List
1820
from typing import Optional
1921
from typing import Text
@@ -99,13 +101,39 @@ def _load_node(
99101
)
100102
)
101103
return
104+
105+
# Asynchronously wait on service call so that we can periodically check for shutdown
106+
event = threading.Event()
107+
108+
def unblock(future):
109+
nonlocal event
110+
event.set()
111+
102112
self.__logger.debug(
103113
"Calling the '{}' service with request '{}'".format(
104114
self.__rclpy_load_node_client.srv_name, request
105115
)
106116
)
107-
response = self.__rclpy_load_node_client.call(request)
117+
118+
response_future = self.__rclpy_load_node_client.call_async(request)
119+
response_future.add_done_callback(unblock)
120+
121+
while not event.wait(1.0):
122+
if context.is_shutdown:
123+
self.__logger.warning(
124+
"Abandoning wait for the '{}' service response, due to shutdown.".format(
125+
self.__rclpy_load_node_client.srv_name),
126+
)
127+
response_future.cancel()
128+
return
129+
130+
# Get response
131+
if response_future.exception() is not None:
132+
raise response_future.exception()
133+
response = response_future.result()
134+
108135
self.__logger.debug("Received response '{}'".format(response))
136+
109137
node_name = response.full_node_name if response.full_node_name else request.node_name
110138
if response.success:
111139
if node_name is not None:

0 commit comments

Comments
 (0)