Skip to content

Fix: avoid importing and calling close in __del__ during Python shutdown#7312

Draft
agoscinski wants to merge 1 commit intoaiidateam:mainfrom
agoscinski:fix-del-broker
Draft

Fix: avoid importing and calling close in __del__ during Python shutdown#7312
agoscinski wants to merge 1 commit intoaiidateam:mainfrom
agoscinski:fix-del-broker

Conversation

@agoscinski
Copy link
Copy Markdown
Collaborator

Fixing issue #7309

Two related fixes to prevent 'Exception ignored in del' errors during Python shutdown:

  1. Move 'import warnings' to module level in RabbitmqBroker and StorageBackend to avoid ImportError when sys.meta_path is None during shutdown.

  2. Guard del with sys.is_finalizing() to skip the close() call during Python shutdown when asyncio/kiwipy event loop infrastructure is already torn down, preventing AttributeError in pytray's await_ method.

During shutdown:

  • sys.meta_path becomes None, making imports unavailable
  • Module globals are set to None before garbage collection
  • asyncio.run_coroutine_threadsafe() fails with AttributeError

By deferring the import and skipping close during finalization, we avoid both issues since the process is exiting anyway.

TODO:

  • There is some filterwarning somewhere in the database module that needs to be removed, otherwise the warning is always filtered out.

Upcoming changes:

  • ResourceWarnings are ignored by default one needs to use PYTHONDEVMODE. This not a bug but a design consideration, it would be good if these warnings are logged somewhere but not showing them to the user makes sense. this requires some upcoming changes in the logger system

Two related fixes to prevent 'Exception ignored in __del__' errors during
Python shutdown:

1. Move 'import warnings' to module level in RabbitmqBroker and StorageBackend
   to avoid ImportError when sys.meta_path is None during shutdown.

2. Guard __del__ with sys.is_finalizing() to skip the close() call during
   Python shutdown when asyncio/kiwipy event loop infrastructure is already
   torn down, preventing AttributeError in pytray's await_ method.

During shutdown:
- sys.meta_path becomes None, making imports unavailable
- Module globals are set to None before garbage collection
- asyncio.run_coroutine_threadsafe() fails with AttributeError

By deferring the import and skipping close during finalization, we avoid
both issues since the process is exiting anyway.
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 5, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.86%. Comparing base (15454cd) to head (0ab2c30).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/aiida/brokers/rabbitmq/broker.py 50.00% 2 Missing ⚠️
src/aiida/orm/implementation/storage_backend.py 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7312      +/-   ##
==========================================
+ Coverage   79.85%   79.86%   +0.01%     
==========================================
  Files         566      566              
  Lines       43947    43966      +19     
==========================================
+ Hits        35088    35107      +19     
  Misses       8859     8859              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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