fix: handle PythonFinalizationError in LoggerManager shutdown#2
Open
mephistofox wants to merge 1 commit intoa-ulianov:mainfrom
Open
fix: handle PythonFinalizationError in LoggerManager shutdown#2mephistofox wants to merge 1 commit intoa-ulianov:mainfrom
mephistofox wants to merge 1 commit intoa-ulianov:mainfrom
Conversation
Python 3.13+ raises PythonFinalizationError when calling thread.join() during interpreter shutdown. LoggerManager.__del__ calls shutdown() which calls QueueListener.stop(), triggering this error. Fix: check sys.is_finalizing() before calling listener.stop(). During finalization, close handlers directly without joining the thread. Reproduces on Python 3.13+ with USE_ASYNC=True (default). Co-authored-by: Mephisto <dev@fxcode.ru>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PythonFinalizationErrorinLoggerManager.__del__on Python 3.13+Problem
On Python 3.13+, importing or using
ozonapiproduces this error at interpreter exit:Root cause
LoggerManager.__del__()callsshutdown()which callsQueueListener.stop(). Thestop()method internally callsself._thread.join()— but during interpreter finalization, threads cannot be joined anymore.Python 3.13 introduced
PythonFinalizationError(PEP 768) which is now raised instead of silently failing.This happens because:
LoggingSettings.USE_ASYNCdefaults toTruemanager = LoggerManager('ozonapi')+manager.configure()in__init__.pycreates aQueueListenerthread at import time__del__tries to join that threadFix
Check
sys.is_finalizing()before callinglistener.stop():listener.stop()with thread join (no behavior change)Test plan
test_shutdown_during_finalizationtest verifies the fixCo-authored-by: Mephisto dev@fxcode.ru