Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,35 @@ def f():
self.assertEqual(err, b"")
self.assertEqual(data, "Thread-1\nTrue\nTrue\n")

def test_main_thread_during_shutdown(self):
# bpo-31516: current_thread() should still point to the main thread
# at shutdown
code = """if 1:
import gc, threading

main_thread = threading.current_thread()
assert main_thread is threading.main_thread() # sanity check

class RefCycle:
def __init__(self):
self.cycle = self

def __del__(self):
print("GC:",
threading.current_thread() is main_thread,
threading.main_thread() is main_thread,
threading.enumerate() == [main_thread])

RefCycle()
gc.collect() # sanity check
x = RefCycle()
"""
_, out, err = assert_python_ok("-c", code)
data = out.decode()
self.assertEqual(err, b"")
self.assertEqual(data.splitlines(),
["GC: True True True"] * 2)

def test_tstate_lock(self):
# Test an implementation detail of Thread objects.
started = _thread.allocate_lock()
Expand Down
3 changes: 1 addition & 2 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1158,8 +1158,8 @@ def run(self):
self.function(*self.args, **self.kwargs)
self.finished.set()


# Special thread class to represent the main thread
# This is garbage collected through an exit handler

class _MainThread(Thread):

Expand Down Expand Up @@ -1272,7 +1272,6 @@ def _shutdown():
while t:
t.join()
t = _pickSomeNonDaemonThread()
_main_thread._delete()

def _pickSomeNonDaemonThread():
for t in enumerate():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``threading.current_thread()`` should not return a dummy thread at shutdown.