Skip to content

fix: handle PythonFinalizationError in LoggerManager shutdown#2

Open
mephistofox wants to merge 1 commit intoa-ulianov:mainfrom
mephistofox:fix/logger-manager-finalization-error
Open

fix: handle PythonFinalizationError in LoggerManager shutdown#2
mephistofox wants to merge 1 commit intoa-ulianov:mainfrom
mephistofox:fix/logger-manager-finalization-error

Conversation

@mephistofox
Copy link

Summary

  • Fix PythonFinalizationError in LoggerManager.__del__ on Python 3.13+
  • Add test for finalization-safe shutdown behavior

Problem

On Python 3.13+, importing or using ozonapi produces this error at interpreter exit:

Exception ignored while calling deallocator <function LoggerManager.__del__>:
  File "ozonapi/infrastructure/logging/manager.py", line 227, in __del__
  File "ozonapi/infrastructure/logging/manager.py", line 190, in shutdown
  File "logging/handlers.py", line 1640, in stop
  File "threading.py", line 1133, in join
PythonFinalizationError: cannot join thread at interpreter shutdown

Root cause

LoggerManager.__del__() calls shutdown() which calls QueueListener.stop(). The stop() method internally calls self._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:

  1. LoggingSettings.USE_ASYNC defaults to True
  2. Module-level manager = LoggerManager('ozonapi') + manager.configure() in __init__.py creates a QueueListener thread at import time
  3. When the interpreter shuts down, __del__ tries to join that thread

Fix

Check sys.is_finalizing() before calling listener.stop():

  • Normal execution: full listener.stop() with thread join (no behavior change)
  • During finalization: close handlers directly without joining the thread

Test plan

  • All 20 existing tests pass
  • New test_shutdown_during_finalization test verifies the fix
  • Tested on Python 3.14.2 where the error was originally observed

Co-authored-by: Mephisto dev@fxcode.ru

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant