Skip to content

Commit

Permalink
Call Context._logging_fini() in Context.try_shutdown() (#800)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
  • Loading branch information
ivanpauno authored Aug 18, 2021
1 parent 1a7e15b commit ad22613
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions rclpy/rclpy/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class Context:
def __init__(self):
self._lock = threading.Lock()
self._callbacks = []
self._callbacks_lock = threading.Lock()
self._logging_initialized = False
self.__context = None

Expand Down Expand Up @@ -89,21 +88,20 @@ def ok(self):
return self.__context.ok()

def _call_on_shutdown_callbacks(self):
with self._callbacks_lock:
for weak_method in self._callbacks:
callback = weak_method()
if callback is not None:
callback()
self._callbacks = []
for weak_method in self._callbacks:
callback = weak_method()
if callback is not None:
callback()
self._callbacks = []

def shutdown(self):
"""Shutdown this context."""
if self.__context is None:
raise RuntimeError('Context must be initialized before it can be shutdown')
with self.__context, self._lock:
self.__context.shutdown()
self._call_on_shutdown_callbacks()
self._logging_fini()
self._call_on_shutdown_callbacks()
self._logging_fini()

def try_shutdown(self):
"""Shutdown this context, if not already shutdown."""
Expand All @@ -113,6 +111,7 @@ def try_shutdown(self):
if self.__context.ok():
self.__context.shutdown()
self._call_on_shutdown_callbacks()
self._logging_fini()

def _remove_callback(self, weak_method):
self._callbacks.remove(weak_method)
Expand All @@ -121,25 +120,25 @@ def on_shutdown(self, callback: Callable[[], None]):
"""Add a callback to be called on shutdown."""
if not callable(callback):
raise TypeError('callback should be a callable, got {}', type(callback))
with self._callbacks_lock:
if not self.ok():
with self.__context, self._lock:
if not self.__context.ok():
callback()
else:
self._callbacks.append(weakref.WeakMethod(callback, self._remove_callback))

def _logging_fini(self):
# This function must be called with self._lock held.
from rclpy.impl.implementation_singleton import rclpy_implementation
global g_logging_ref_count
with self._lock:
if self._logging_initialized:
with g_logging_configure_lock:
g_logging_ref_count -= 1
if g_logging_ref_count == 0:
rclpy_implementation.rclpy_logging_fini()
if g_logging_ref_count < 0:
raise RuntimeError(
'Unexpected error: logger ref count should never be lower that zero')
self._logging_initialized = False
if self._logging_initialized:
with g_logging_configure_lock:
g_logging_ref_count -= 1
if g_logging_ref_count == 0:
rclpy_implementation.rclpy_logging_fini()
if g_logging_ref_count < 0:
raise RuntimeError(
'Unexpected error: logger ref count should never be lower that zero')
self._logging_initialized = False

def get_domain_id(self):
"""Get domain id of context."""
Expand Down

0 comments on commit ad22613

Please sign in to comment.