[Python-checkins] tbpo-36402: Fix threading.Thread._stop() (GH-14047)

Victor Stinner webhook-mailer at python.org
Thu Jun 13 06:06:30 EDT 2019


https://github.com/python/cpython/commit/6f75c873752a16a7ad8f35855b1e29f59d048e84
commit: 6f75c873752a16a7ad8f35855b1e29f59d048e84
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2019-06-13T12:06:24+02:00
summary:

tbpo-36402: Fix threading.Thread._stop() (GH-14047)

Remove the _tstate_lock from _shutdown_locks, don't remove None.

files:
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 ad90010b8a38..0a0a62bdf9bf 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -738,6 +738,30 @@ def callback():
         finally:
             sys.settrace(old_trace)
 
+    @cpython_only
+    def test_shutdown_locks(self):
+        for daemon in (False, True):
+            with self.subTest(daemon=daemon):
+                event = threading.Event()
+                thread = threading.Thread(target=event.wait, daemon=daemon)
+
+                # Thread.start() must add lock to _shutdown_locks,
+                # but only for non-daemon thread
+                thread.start()
+                tstate_lock = thread._tstate_lock
+                if not daemon:
+                    self.assertIn(tstate_lock, threading._shutdown_locks)
+                else:
+                    self.assertNotIn(tstate_lock, threading._shutdown_locks)
+
+                # unblock the thread and join it
+                event.set()
+                thread.join()
+
+                # Thread._stop() must remove tstate_lock from _shutdown_locks.
+                # Daemon threads must never add it to _shutdown_locks.
+                self.assertNotIn(tstate_lock, threading._shutdown_locks)
+
 
 class ThreadJoinOnShutdown(BaseTestCase):
 
diff --git a/Lib/threading.py b/Lib/threading.py
index 67926403770e..7c6d404bcd10 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -965,7 +965,7 @@ def _stop(self):
         self._tstate_lock = None
         if not self.daemon:
             with _shutdown_locks_lock:
-                _shutdown_locks.discard(self._tstate_lock)
+                _shutdown_locks.discard(lock)
 
     def _delete(self):
         "Remove current thread from the dict of currently running threads."



More information about the Python-checkins mailing list