Skip to content

Commit ac6245a

Browse files
miss-islingtonpitrou
authored andcommitted
[3.6] bpo-31516: current_thread() should not return a dummy thread at shutdown (GH-3673) (#3856)
bpo-31516: current_thread() should not return a dummy thread at shutdown (cherry picked from commit 1023dbb)
1 parent 66fb5ef commit ac6245a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

‎Lib/test/test_threading.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,35 @@ def f():
542542
self.assertEqual(err, b"")
543543
self.assertEqual(data, "Thread-1\nTrue\nTrue\n")
544544

545+
def test_main_thread_during_shutdown(self):
546+
# bpo-31516: current_thread() should still point to the main thread
547+
# at shutdown
548+
code = """if 1:
549+
import gc, threading
550+
551+
main_thread = threading.current_thread()
552+
assert main_thread is threading.main_thread() # sanity check
553+
554+
class RefCycle:
555+
def __init__(self):
556+
self.cycle = self
557+
558+
def __del__(self):
559+
print("GC:",
560+
threading.current_thread() is main_thread,
561+
threading.main_thread() is main_thread,
562+
threading.enumerate() == [main_thread])
563+
564+
RefCycle()
565+
gc.collect() # sanity check
566+
x = RefCycle()
567+
"""
568+
_, out, err = assert_python_ok("-c", code)
569+
data = out.decode()
570+
self.assertEqual(err, b"")
571+
self.assertEqual(data.splitlines(),
572+
["GC: True True True"] * 2)
573+
545574
def test_tstate_lock(self):
546575
# Test an implementation detail of Thread objects.
547576
started = _thread.allocate_lock()

‎Lib/threading.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,8 @@ def run(self):
11821182
self.function(*self.args, **self.kwargs)
11831183
self.finished.set()
11841184

1185+
11851186
# Special thread class to represent the main thread
1186-
# This is garbage collected through an exit handler
11871187

11881188
class _MainThread(Thread):
11891189

@@ -1293,7 +1293,6 @@ def _shutdown():
12931293
while t:
12941294
t.join()
12951295
t = _pickSomeNonDaemonThread()
1296-
_main_thread._delete()
12971296

12981297
def _pickSomeNonDaemonThread():
12991298
for t in enumerate():
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``threading.current_thread()`` should not return a dummy thread at shutdown.

0 commit comments

Comments
 (0)