]> granicus.if.org Git - python/commitdiff
tbpo-36402: Fix threading.Thread._stop() (GH-14047)
authorVictor Stinner <vstinner@redhat.com>
Thu, 13 Jun 2019 10:06:24 +0000 (12:06 +0200)
committerGitHub <noreply@github.com>
Thu, 13 Jun 2019 10:06:24 +0000 (12:06 +0200)
Remove the _tstate_lock from _shutdown_locks, don't remove None.

Lib/test/test_threading.py
Lib/threading.py

index ad90010b8a3821e87a8f0af4bb4d8969e28bb011..0a0a62bdf9bfaf65405d65b233577db697da3a63 100644 (file)
@@ -738,6 +738,30 @@ class ThreadTests(BaseTestCase):
         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):
 
index 67926403770e62242da1c67fcfe335e48f4ab08a..7c6d404bcd10f6085ef285f28c929faf25846686 100644 (file)
@@ -965,7 +965,7 @@ class Thread:
         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."