Skip to content

Commit

Permalink
pythongh-116631: Fix race condition in `test_shutdown_immediate_put_j…
Browse files Browse the repository at this point in the history
…oin` (python#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.
  • Loading branch information
colesbury authored and adorilson committed Mar 25, 2024
1 parent 69861cd commit 3a20121
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions Lib/test/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,6 @@ def _shutdown_put_join(self, immediate):
results = []
go = threading.Event()
q.put("Y")
nb = q.qsize()
# queue not fulled

thrds = (
Expand All @@ -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()

Expand Down

0 comments on commit 3a20121

Please sign in to comment.