mirror of
https://github.com/python/cpython
synced 2024-09-15 23:36:24 +00:00
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.
This commit is contained in:
parent
25684e7131
commit
98ab21cce6
|
@ -567,7 +567,6 @@ def _shutdown_put_join(self, immediate):
|
||||||
results = []
|
results = []
|
||||||
go = threading.Event()
|
go = threading.Event()
|
||||||
q.put("Y")
|
q.put("Y")
|
||||||
nb = q.qsize()
|
|
||||||
# queue not fulled
|
# queue not fulled
|
||||||
|
|
||||||
thrds = (
|
thrds = (
|
||||||
|
@ -578,13 +577,19 @@ def _shutdown_put_join(self, immediate):
|
||||||
for func, params in thrds:
|
for func, params in thrds:
|
||||||
threads.append(threading.Thread(target=func, args=params))
|
threads.append(threading.Thread(target=func, args=params))
|
||||||
threads[-1].start()
|
threads[-1].start()
|
||||||
self.assertEqual(q.unfinished_tasks, nb)
|
self.assertEqual(q.unfinished_tasks, 1)
|
||||||
for i in range(nb):
|
|
||||||
t = threading.Thread(target=q.task_done)
|
|
||||||
t.start()
|
|
||||||
threads.append(t)
|
|
||||||
q.shutdown(immediate)
|
q.shutdown(immediate)
|
||||||
go.set()
|
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:
|
for t in threads:
|
||||||
t.join()
|
t.join()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue