Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 7 additions & 4 deletions rclpy/rclpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ def get_global_executor() -> 'Executor':
# imported locally to avoid loading extensions on module import
from rclpy.executors import SingleThreadedExecutor
__executor = SingleThreadedExecutor()
context = get_default_context()

def reset_executor():
global __executor
__executor.shutdown()
__executor = None
context.on_shutdown(reset_executor)
return __executor


Expand All @@ -116,10 +123,6 @@ def shutdown(*, context: Context = None, uninstall_handlers: Optional[bool] = No
If `True`, signal handlers will be uninstalled.
If not, signal handlers won't be uninstalled.
"""
global __executor
if __executor is not None:
__executor.shutdown()
__executor = None
_shutdown(context=context)
if (
uninstall_handlers or (
Expand Down
14 changes: 12 additions & 2 deletions rclpy/rclpy/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,19 @@ def shutdown(*, context=None):

def try_shutdown(*, context=None):
"""Shutdown rclpy if not already shutdown."""
global g_context_lock
global g_default_context
if context is None:
context = get_default_context()
return context.try_shutdown()
# Replace the default context with a new one if shutdown was successful
# or if the context was already shutdown.
with g_context_lock:
if g_default_context is None:
g_default_context = Context()
g_default_context.try_shutdown()
if not g_default_context.ok():
g_default_context = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish the default context registered an on_shutdown callback to manage the global variable too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently all the work is done with the default context lock taken.
If we register an on_shutdown() callback I don't think that would work.

Maybe it's possible to do that with some extra trick.

else:
return context.try_shutdown()


def get_rmw_implementation_identifier():
Expand Down