Skip to content

Commit

Permalink
Fix a bug when using multiple rclpy.init context managers. (#1314)
Browse files Browse the repository at this point in the history
That is, if you call rclpy.init as a context manager within
the same process, and using the default context, it currently
fails.  That is because you cannot reinitialize (call init) on
a Context object once it has been shutdown.  Instead, force
try_shutdown to replace the global context object with a new
one on rclpy.init shutdown by passing a None.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Jul 22, 2024
1 parent 43198cb commit f7dd807
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion rclpy/rclpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ def __exit__(
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
try_shutdown(context=self.context, uninstall_handlers=self.installed_signal_handlers)
# The Context class can only be initialized once. Thus when using the default context,
# we have to destroy it every time we are done using it and create a new one.
# utilities.try_shutdown will only replace the default context if we pass 'None', so make
# sure to do that for the default context.
shutdown_context = None if self.context is get_default_context() else self.context
try_shutdown(context=shutdown_context, uninstall_handlers=self.installed_signal_handlers)


def init(
Expand Down

0 comments on commit f7dd807

Please sign in to comment.