]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-130522: Fix unraisable TypeError in threading at interpreter shutdown (#131537)
authorTyler Kennedy <tk@tkte.ch>
Fri, 25 Jul 2025 14:51:30 +0000 (10:51 -0400)
committerGitHub <noreply@github.com>
Fri, 25 Jul 2025 14:51:30 +0000 (14:51 +0000)
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Lib/test/test_threading.py
Lib/threading.py
Misc/NEWS.d/next/Library/2025-07-25-09-21-56.gh-issue-130522.Crwq68.rst [new file with mode: 0644]

index 00a3037c3e1e01dd19a15ea290bd8238ea5b9802..002a1feeb85c949046fb85ac050b881f4de77ed1 100644 (file)
@@ -2042,6 +2042,23 @@ class ThreadingExceptionTests(BaseTestCase):
             t.start()
             t.join()
 
+    def test_dummy_thread_on_interpreter_shutdown(self):
+        # GH-130522: When `threading` held a reference to itself and then a
+        # _DummyThread() object was created, destruction of the dummy thread
+        # would emit an unraisable exception at shutdown, due to a lock being
+        # destroyed.
+        code = """if True:
+        import sys
+        import threading
+
+        threading.x = sys.modules[__name__]
+        x = threading._DummyThread()
+        """
+        rc, out, err = assert_python_ok("-c", code)
+        self.assertEqual(rc, 0)
+        self.assertEqual(out, b"")
+        self.assertEqual(err, b"")
+
 
 class ThreadRunFail(threading.Thread):
     def run(self):
index b6c451d1fbaabd460ab7f4233d5a537aa0fa9558..b3e24fe7ddaa0ce3618292646912ed7ee9af34ee 100644 (file)
@@ -1414,7 +1414,7 @@ class _DeleteDummyThreadOnDel:
         # the related _DummyThread will be kept forever!
         _thread_local_info._track_dummy_thread_ref = self
 
-    def __del__(self):
+    def __del__(self, _active_limbo_lock=_active_limbo_lock, _active=_active):
         with _active_limbo_lock:
             if _active.get(self._tident) is self._dummy_thread:
                 _active.pop(self._tident, None)
diff --git a/Misc/NEWS.d/next/Library/2025-07-25-09-21-56.gh-issue-130522.Crwq68.rst b/Misc/NEWS.d/next/Library/2025-07-25-09-21-56.gh-issue-130522.Crwq68.rst
new file mode 100644 (file)
index 0000000..6c22466
--- /dev/null
@@ -0,0 +1,2 @@
+Fix unraisable :exc:`TypeError` raised during :term:`interpreter shutdown`
+in the :mod:`threading` module.