Skip to content

Commit

Permalink
Fix "Exception ignored" in weakref callback during interpreter shutdown.
Browse files Browse the repository at this point in the history
Under some circumstances "Exception ignored in: <function
WeakMethod.__new__.<locals>._cb at 0x770cbdb5b9c0>" appears during interpreter shutdown.

Root cause is probably a weakref callback running here from an interpeter shutdown
finalizer pass, and then calling _disconnect either causes the same weakref to be
cleaned up again or calls into some other already-gone resource and triggers the error.
  • Loading branch information
projectgus authored and davidism committed Nov 8, 2024
1 parent dcce3e9 commit 42561fd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Unreleased
- Drop support for Python 3.8. :pr:`175`
- Remove previously deprecated ``__version__``, ``receiver_connected``,
``Signal.temporarily_connected_to`` and ``WeakNamespace``. :pr:`172`
- Skip weakref signal cleanup if the interpreter is shutting down.
:issue:`173`


Version 1.8.2
Expand Down
6 changes: 5 additions & 1 deletion src/blinker/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import collections.abc as c
import sys
import typing as t
import weakref
from collections import defaultdict
Expand Down Expand Up @@ -403,7 +404,10 @@ def _make_cleanup_receiver(
"""

def cleanup(ref: weakref.ref[c.Callable[..., t.Any]]) -> None:
self._disconnect(receiver_id, ANY_ID)
# If the interpreter is shutting down, disconnecting can result in a
# weird ignored exception. Don't call it in that case.
if not sys.is_finalizing():
self._disconnect(receiver_id, ANY_ID)

return cleanup

Expand Down

0 comments on commit 42561fd

Please sign in to comment.