From 42561fdf065032f5c39aea04d8c3fb4db93f73f3 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Mon, 4 Nov 2024 21:17:56 +1100 Subject: [PATCH] Fix "Exception ignored" in weakref callback during interpreter shutdown. Under some circumstances "Exception ignored in: ._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. --- CHANGES.rst | 2 ++ src/blinker/base.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index a876319..f84e181 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/src/blinker/base.py b/src/blinker/base.py index 2b0baba..d051b94 100644 --- a/src/blinker/base.py +++ b/src/blinker/base.py @@ -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 @@ -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