Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1023dbb

Browse files
authored
bpo-31516: current_thread() should not return a dummy thread at shutdown (#3673)
bpo-31516: current_thread() should not return a dummy thread at shutdown
1 parent e6f62f6 commit 1023dbb

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

Lib/test/test_threading.py

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

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

Lib/threading.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,8 +1158,8 @@ def run(self):
11581158
self.function(*self.args, **self.kwargs)
11591159
self.finished.set()
11601160

1161+
11611162
# Special thread class to represent the main thread
1162-
# This is garbage collected through an exit handler
11631163

11641164
class _MainThread(Thread):
11651165

@@ -1272,7 +1272,6 @@ def _shutdown():
12721272
while t:
12731273
t.join()
12741274
t = _pickSomeNonDaemonThread()
1275-
_main_thread._delete()
12761275

12771276
def _pickSomeNonDaemonThread():
12781277
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)