Skip to content

Commit f487008

Browse files
miss-islingtonTkTechZeroIntensity
authored
[3.14] gh-130522: Fix unraisable TypeError in threading at interpreter shutdown (GH-131537) (#137105)
Co-authored-by: Tyler Kennedy <tk@tkte.ch> Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent 134ca90 commit f487008

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/test/test_threading.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,23 @@ def modify_file():
19931993
t.start()
19941994
t.join()
19951995

1996+
def test_dummy_thread_on_interpreter_shutdown(self):
1997+
# GH-130522: When `threading` held a reference to itself and then a
1998+
# _DummyThread() object was created, destruction of the dummy thread
1999+
# would emit an unraisable exception at shutdown, due to a lock being
2000+
# destroyed.
2001+
code = """if True:
2002+
import sys
2003+
import threading
2004+
2005+
threading.x = sys.modules[__name__]
2006+
x = threading._DummyThread()
2007+
"""
2008+
rc, out, err = assert_python_ok("-c", code)
2009+
self.assertEqual(rc, 0)
2010+
self.assertEqual(out, b"")
2011+
self.assertEqual(err, b"")
2012+
19962013

19972014
class ThreadRunFail(threading.Thread):
19982015
def run(self):

Lib/threading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ def __init__(self, dummy_thread):
14211421
# the related _DummyThread will be kept forever!
14221422
_thread_local_info._track_dummy_thread_ref = self
14231423

1424-
def __del__(self):
1424+
def __del__(self, _active_limbo_lock=_active_limbo_lock, _active=_active):
14251425
with _active_limbo_lock:
14261426
if _active.get(self._tident) is self._dummy_thread:
14271427
_active.pop(self._tident, None)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix unraisable :exc:`TypeError` raised during :term:`interpreter shutdown`
2+
in the :mod:`threading` module.

0 commit comments

Comments
 (0)