]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-116631: Fix race condition in `test_shutdown_immediate_put_join` (#116670)
authorSam Gross <colesbury@gmail.com>
Wed, 13 Mar 2024 18:56:28 +0000 (14:56 -0400)
committerGitHub <noreply@github.com>
Wed, 13 Mar 2024 18:56:28 +0000 (14:56 -0400)
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.

Lib/test/test_queue.py

index 92d670ca6f8f5bd2b4e9640d2be6fd85f672e691..ad31ba1af03b6f64f1d14507a4f4d140073677fc 100644 (file)
@@ -567,7 +567,6 @@ class BaseQueueTestMixin(BlockingTestMixin):
         results = []
         go = threading.Event()
         q.put("Y")
-        nb = q.qsize()
         # queue not fulled
 
         thrds = (
@@ -578,13 +577,19 @@ class BaseQueueTestMixin(BlockingTestMixin):
         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()