-
Notifications
You must be signed in to change notification settings - Fork 263
Ensure logging is initialized only once #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,10 @@ | |
| import weakref | ||
|
|
||
|
|
||
| g_logging_configure_lock = threading.Lock() | ||
| g_logging_ref_count = 0 | ||
|
|
||
|
|
||
| class Context: | ||
| """ | ||
| Encapsulates the lifecycle of init and shutdown. | ||
|
|
@@ -36,21 +40,29 @@ def __init__(self): | |
| self._lock = threading.Lock() | ||
| self._callbacks = [] | ||
| self._callbacks_lock = threading.Lock() | ||
| self._logging_initialized = False | ||
|
|
||
| @property | ||
| def handle(self): | ||
| return self._handle | ||
|
|
||
| def init(self, args: Optional[List[str]] = None): | ||
| def init(self, args: Optional[List[str]] = None, *, initialize_logging: bool = True): | ||
| """ | ||
| Initialize ROS communications for a given context. | ||
|
|
||
| :param args: List of command line arguments. | ||
| """ | ||
| # imported locally to avoid loading extensions on module import | ||
| from rclpy.impl.implementation_singleton import rclpy_implementation | ||
| global g_logging_ref_count | ||
| with self._handle as capsule, self._lock: | ||
| rclpy_implementation.rclpy_init(args if args is not None else sys.argv, capsule) | ||
| if initialize_logging and not self._logging_initialized: | ||
| with g_logging_configure_lock: | ||
| g_logging_ref_count += 1 | ||
| if g_logging_ref_count == 1: | ||
| rclpy_implementation.rclpy_logging_configure(capsule) | ||
| self._logging_initialized = True | ||
|
|
||
| def ok(self): | ||
| """Check if context hasn't been shut down.""" | ||
|
|
@@ -73,6 +85,7 @@ def shutdown(self): | |
| with self._handle as capsule, self._lock: | ||
| rclpy_implementation.rclpy_shutdown(capsule) | ||
| self._call_on_shutdown_callbacks() | ||
| self._logging_fini() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ivanpauno any idea why this is not called from
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, I guess that was only a mistake.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #800 should fix it |
||
|
|
||
| def try_shutdown(self): | ||
| """Shutdown this context, if not already shutdown.""" | ||
|
|
@@ -95,3 +108,17 @@ def on_shutdown(self, callback: Callable[[], None]): | |
| callback() | ||
| else: | ||
| self._callbacks.append(weakref.WeakMethod(callback, self._remove_callback)) | ||
|
|
||
| def _logging_fini(self): | ||
| 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 | ||
Uh oh!
There was an error while loading. Please reload this page.