https://github.com/python/cpython/commit/98ab21cce6d4c7bd2b5a0a1521b50b1ce2566a44
commit: 98ab21cce6d4c7bd2b5a0a1521b50b1ce2566a44
branch: main
author: Sam Gross <[email protected]>
committer: colesbury <[email protected]>
date: 2024-03-13T14:56:28-04:00
summary:

gh-116631: Fix race condition in `test_shutdown_immediate_put_join` (#116670)

The test case had a race condition: if `q.task_done()` was executed
after `shutdown(immediate=True)`, then it would raise an exception
because the immediate shutdown already emptied the queue. This happened
rarely with the GIL (due to the switching interval), but frequently in
the free-threaded build.

files:
M Lib/test/test_queue.py

diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py
index 92d670ca6f8f5b..ad31ba1af03b6f 100644
--- a/Lib/test/test_queue.py
+++ b/Lib/test/test_queue.py
@@ -567,7 +567,6 @@ def _shutdown_put_join(self, immediate):
         results = []
         go = threading.Event()
         q.put("Y")
-        nb = q.qsize()
         # queue not fulled
 
         thrds = (
@@ -578,13 +577,19 @@ def _shutdown_put_join(self, immediate):
         for func, params in thrds:
             threads.append(threading.Thread(target=func, args=params))
             threads[-1].start()
-        self.assertEqual(q.unfinished_tasks, nb)
-        for i in range(nb):
-            t = threading.Thread(target=q.task_done)
-            t.start()
-            threads.append(t)
+        self.assertEqual(q.unfinished_tasks, 1)
+
         q.shutdown(immediate)
         go.set()
+
+        if immediate:
+            with self.assertRaises(self.queue.ShutDown):
+                q.get_nowait()
+        else:
+            result = q.get()
+            self.assertEqual(result, "Y")
+            q.task_done()
+
         for t in threads:
             t.join()
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to