Skip to content

Commit 8f41211

Browse files
miss-islingtonTkTechZeroIntensity
authored
[3.13] gh-130522: Fix unraisable TypeError in threading at interpreter shutdown (GH-131537) (GH-137106)
gh-130522: Fix unraisable TypeError in threading at interpreter shutdown (GH-131537) (cherry picked from commit cb93b6f) Co-authored-by: Tyler Kennedy <tk@tkte.ch> Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent eda6134 commit 8f41211

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
@@ -1849,6 +1849,23 @@ def modify_file():
18491849
t.start()
18501850
t.join()
18511851

1852+
def test_dummy_thread_on_interpreter_shutdown(self):
1853+
# GH-130522: When `threading` held a reference to itself and then a
1854+
# _DummyThread() object was created, destruction of the dummy thread
1855+
# would emit an unraisable exception at shutdown, due to a lock being
1856+
# destroyed.
1857+
code = """if True:
1858+
import sys
1859+
import threading
1860+
1861+
threading.x = sys.modules[__name__]
1862+
x = threading._DummyThread()
1863+
"""
1864+
rc, out, err = assert_python_ok("-c", code)
1865+
self.assertEqual(rc, 0)
1866+
self.assertEqual(out, b"")
1867+
self.assertEqual(err, b"")
1868+
18521869

18531870
class ThreadRunFail(threading.Thread):
18541871
def run(self):

Lib/threading.py

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

1384-
def __del__(self):
1384+
def __del__(self, _active_limbo_lock=_active_limbo_lock, _active=_active):
13851385
with _active_limbo_lock:
13861386
if _active.get(self._tident) is self._dummy_thread:
13871387
_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)