-
Notifications
You must be signed in to change notification settings - Fork 695
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
Fix for Runtime error when reinitializing logger #1184
base: master
Are you sure you want to change the base?
Conversation
We hit this error intermittently as we have to reinitialize our logger at least once during our application startup. File "/opt/pysetup/.venv/lib/python3.11/site-packages/loguru/_logger.py", line 2031, in _log for handler in core.handlers.values(): RuntimeError: dictionary changed size during iteration Fix is to copy the values to avoid the "changed size" error.
Bump - hoping this can get a look sometime soon! |
this doesn't actually fix the problem, it only make error less possible to happen。 should choose lock, if we think loguru should support logging and reinit at same time. there is already core.lock, just acquire it |
Thanks for investigating this bug and opening a PR, @jeremyk. Apologies for the delay. Actually, the The This is done here for Line 1006 in 80f05fd
And here for Line 1043 in 80f05fd
Once the This allows the iteration through handlers for Now, I said Lines 1053 to 1059 in 80f05fd
The problem is that after the first iteration, the The fix should be implemented in the iff --git a/loguru/_logger.py b/loguru/_logger.py
index cc514b0..ffe4f80 100644
--- a/loguru/_logger.py
+++ b/loguru/_logger.py
@@ -1040,7 +1040,7 @@ class Logger:
)
with self._core.lock:
- handlers = self._core.handlers.copy()
+ handlers = self._core.handlers
if handler_id is not None and handler_id not in handlers:
raise ValueError("There is no existing handler with id %d" % handler_id) from None
@@ -1051,6 +1051,7 @@ class Logger:
handler_ids = [handler_id]
for handler_id in handler_ids:
+ handlers = handlers.copy()
handler = handlers.pop(handler_id)
# This needs to be done first in case "stop()" raises an exception |
I'm totally fine if you close this PR and take care of it. Thanks @Delgan ! |
We hit this error intermittently as we have to reinitialize our logger at least once during our application startup.
File "/opt/pysetup/.venv/lib/python3.11/site-packages/loguru/_logger.py", line 2031, in _log
for handler in core.handlers.values():
RuntimeError: dictionary changed size during iteration
Fix is to copy the values to avoid the "changed size" error.
Fixes: #1183