Skip to content

Commit 4c51358

Browse files
bpo-30727: Fix a race condition in test_treading.
1 parent ea00798 commit 4c51358

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Lib/test/lock_tests.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,21 +461,28 @@ def _check_notify(self, cond):
461461
# construct. In particular, it is possible that this can no longer
462462
# be conveniently guaranteed should their implementation ever change.
463463
N = 5
464+
ready = []
464465
results1 = []
465466
results2 = []
466467
phase_num = 0
467468
def f():
468469
cond.acquire()
470+
ready.append(phase_num)
469471
result = cond.wait()
470472
cond.release()
471473
results1.append((result, phase_num))
472474
cond.acquire()
475+
ready.append(phase_num)
473476
result = cond.wait()
474477
cond.release()
475478
results2.append((result, phase_num))
476479
b = Bunch(f, N)
477480
b.wait_for_started()
478-
_wait()
481+
# first wait, to ensure all workers settle into cond.wait() before
482+
# we continue. See issues #8799 and #30727.
483+
while len(ready) < 5:
484+
_wait()
485+
ready = []
479486
self.assertEqual(results1, [])
480487
# Notify 3 threads at first
481488
cond.acquire()
@@ -487,9 +494,9 @@ def f():
487494
_wait()
488495
self.assertEqual(results1, [(True, 1)] * 3)
489496
self.assertEqual(results2, [])
490-
# first wait, to ensure all workers settle into cond.wait() before
491-
# we continue. See issue #8799
492-
_wait()
497+
# make sure all awaken workers settle into cond.wait()
498+
while len(ready) < 3:
499+
_wait()
493500
# Notify 5 threads: they might be in their first or second wait
494501
cond.acquire()
495502
cond.notify(5)
@@ -500,7 +507,9 @@ def f():
500507
_wait()
501508
self.assertEqual(results1, [(True, 1)] * 3 + [(True, 2)] * 2)
502509
self.assertEqual(results2, [(True, 2)] * 3)
503-
_wait() # make sure all workers settle into cond.wait()
510+
# make sure all workers settle into cond.wait()
511+
while len(ready) < 5:
512+
_wait()
504513
# Notify all threads: they are all in their second wait
505514
cond.acquire()
506515
cond.notify_all()

0 commit comments

Comments
 (0)