Skip to content

Commit 98ab21c

Browse files
authored
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.
1 parent 25684e7 commit 98ab21c

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Lib/test/test_queue.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,6 @@ def _shutdown_put_join(self, immediate):
567567
results = []
568568
go = threading.Event()
569569
q.put("Y")
570-
nb = q.qsize()
571570
# queue not fulled
572571

573572
thrds = (
@@ -578,13 +577,19 @@ def _shutdown_put_join(self, immediate):
578577
for func, params in thrds:
579578
threads.append(threading.Thread(target=func, args=params))
580579
threads[-1].start()
581-
self.assertEqual(q.unfinished_tasks, nb)
582-
for i in range(nb):
583-
t = threading.Thread(target=q.task_done)
584-
t.start()
585-
threads.append(t)
580+
self.assertEqual(q.unfinished_tasks, 1)
581+
586582
q.shutdown(immediate)
587583
go.set()
584+
585+
if immediate:
586+
with self.assertRaises(self.queue.ShutDown):
587+
q.get_nowait()
588+
else:
589+
result = q.get()
590+
self.assertEqual(result, "Y")
591+
q.task_done()
592+
588593
for t in threads:
589594
t.join()
590595

0 commit comments

Comments
 (0)