https://github.com/python/cpython/commit/f4870086bac79c708ba506452b5e580bcd212d26
commit: f4870086bac79c708ba506452b5e580bcd212d26
branch: 3.14
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: hugovk <1324225+hug...@users.noreply.github.com>
date: 2025-08-04T16:01:39+03:00
summary:

[3.14] gh-130522: Fix unraisable TypeError in threading at interpreter shutdown 
(GH-131537) (#137105)

Co-authored-by: Tyler Kennedy <t...@tkte.ch>
Co-authored-by: Peter Bierma <zintensity...@gmail.com>

files:
A Misc/NEWS.d/next/Library/2025-07-25-09-21-56.gh-issue-130522.Crwq68.rst
M Lib/test/test_threading.py
M Lib/threading.py

diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index c07b8133f0a2d1..e468fcd7601c15 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -1993,6 +1993,23 @@ def modify_file():
             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):
diff --git a/Lib/threading.py b/Lib/threading.py
index 2a65f9a7aa3028..79b753d983aaaf 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -1421,7 +1421,7 @@ def __init__(self, dummy_thread):
         # 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
index 00000000000000..6c2246631dda9e
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-07-25-09-21-56.gh-issue-130522.Crwq68.rst
@@ -0,0 +1,2 @@
+Fix unraisable :exc:`TypeError` raised during :term:`interpreter shutdown`
+in the :mod:`threading` module.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to