[Python-checkins] [3.6] bpo-31516: current_thread() should not return a dummy thread at shutdown (GH-3673) (#3856)
Antoine Pitrou
webhook-mailer at python.org
Mon Oct 2 11:20:05 EDT 2017
https://github.com/python/cpython/commit/ac6245a31f9a757db0520722c592cb7fdcb55eb0
commit: ac6245a31f9a757db0520722c592cb7fdcb55eb0
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Antoine Pitrou <pitrou at free.fr>
date: 2017年10月02日T17:20:01+02:00
summary:
[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 1023dbbcb7f05e76053486ae7ef7f73b4cdc5398)
files:
A Misc/NEWS.d/next/Library/2017-09-20-18-43-01.bpo-31516.23Yuq3.rst
M Lib/test/test_threading.py
M Lib/threading.py
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index 9cadc0f1c02..b42314fdbb2 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -542,6 +542,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()
diff --git a/Lib/threading.py b/Lib/threading.py
index 95978d310a2..bb2556544b0 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -1182,8 +1182,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):
@@ -1293,7 +1293,6 @@ def _shutdown():
while t:
t.join()
t = _pickSomeNonDaemonThread()
- _main_thread._delete()
def _pickSomeNonDaemonThread():
for t in enumerate():
diff --git a/Misc/NEWS.d/next/Library/2017-09-20-18-43-01.bpo-31516.23Yuq3.rst b/Misc/NEWS.d/next/Library/2017-09-20-18-43-01.bpo-31516.23Yuq3.rst
new file mode 100644
index 00000000000..af48d159fef
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-09-20-18-43-01.bpo-31516.23Yuq3.rst
@@ -0,0 +1 @@
+``threading.current_thread()`` should not return a dummy thread at shutdown.
More information about the Python-checkins
mailing list