Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion rclpy/rclpy/executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def shutdown(self, timeout_sec: Optional[float] = None) -> bool:
:param timeout_sec: Seconds to wait. Block forever if ``None`` or negative.
Don't wait if 0.
:return: ``True`` if all outstanding callbacks finished executing, or ``False`` if the
timeot expires before all outstanding work is done.
timeout expires before all outstanding work is done.
"""
with self._shutdown_lock:
if not self._is_shutdown:
Expand Down Expand Up @@ -1035,3 +1035,23 @@ def spin_once_until_future_complete(
self._spin_once_until_future_complete(future, timeout_sec)
finally:
self._exit_spin()

def shutdown(
self,
timeout_sec: Optional[float] = None,
*,
wait_for_threads: bool = True
) -> bool:
"""
Stop executing callbacks and wait for their completion.

:param timeout_sec: Seconds to wait. Block forever if ``None`` or negative.
Don't wait if 0.
:param wait_for_threads: If true, this function will block until all executor threads
have joined.
:return: ``True`` if all outstanding callbacks finished executing, or ``False`` if the
timeout expires before all outstanding work is done.
"""
success: bool = super().shutdown(timeout_sec)
self._executor.shutdown(wait=wait_for_threads)
return success
18 changes: 18 additions & 0 deletions rclpy/test/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ def test_multi_threaded_executor_executes(self):
finally:
executor.shutdown()

def test_multi_threaded_executor_closes_threads(self):
self.assertIsNotNone(self.node.handle)

def get_threads():
return {t.name for t in threading.enumerate()}

main_thread_name = get_threads()
# Explicitly specify 2_threads for single thread system failure
executor = MultiThreadedExecutor(context=self.context, num_threads=2)

try:
# Give the executor a callback so at least one thread gets spun up
self.assertTrue(self.func_execution(executor))
finally:
self.assertTrue(main_thread_name != get_threads())
executor.shutdown(wait_for_threads=True)
self.assertTrue(main_thread_name == get_threads())

def test_add_node_to_executor(self):
self.assertIsNotNone(self.node.handle)
for cls in [SingleThreadedExecutor, EventsExecutor]:
Expand Down