Skip to content

Commit e2aec8e

Browse files
[3.5] bpo-30727: Fix a race condition in test_threading. (GH-2334) (#2352)
(cherry picked from commit 32cb968)
1 parent 8aa15ba commit e2aec8e

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
@@ -449,21 +449,28 @@ def _check_notify(self, cond):
449449
# construct. In particular, it is possible that this can no longer
450450
# be conveniently guaranteed should their implementation ever change.
451451
N = 5
452+
ready = []
452453
results1 = []
453454
results2 = []
454455
phase_num = 0
455456
def f():
456457
cond.acquire()
458+
ready.append(phase_num)
457459
result = cond.wait()
458460
cond.release()
459461
results1.append((result, phase_num))
460462
cond.acquire()
463+
ready.append(phase_num)
461464
result = cond.wait()
462465
cond.release()
463466
results2.append((result, phase_num))
464467
b = Bunch(f, N)
465468
b.wait_for_started()
466-
_wait()
469+
# first wait, to ensure all workers settle into cond.wait() before
470+
# we continue. See issues #8799 and #30727.
471+
while len(ready) < 5:
472+
_wait()
473+
ready.clear()
467474
self.assertEqual(results1, [])
468475
# Notify 3 threads at first
469476
cond.acquire()
@@ -475,9 +482,9 @@ def f():
475482
_wait()
476483
self.assertEqual(results1, [(True, 1)] * 3)
477484
self.assertEqual(results2, [])
478-
# first wait, to ensure all workers settle into cond.wait() before
479-
# we continue. See issue #8799
480-
_wait()
485+
# make sure all awaken workers settle into cond.wait()
486+
while len(ready) < 3:
487+
_wait()
481488
# Notify 5 threads: they might be in their first or second wait
482489
cond.acquire()
483490
cond.notify(5)
@@ -488,7 +495,9 @@ def f():
488495
_wait()
489496
self.assertEqual(results1, [(True, 1)] * 3 + [(True, 2)] * 2)
490497
self.assertEqual(results2, [(True, 2)] * 3)
491-
_wait() # make sure all workers settle into cond.wait()
498+
# make sure all workers settle into cond.wait()
499+
while len(ready) < 5:
500+
_wait()
492501
# Notify all threads: they are all in their second wait
493502
cond.acquire()
494503
cond.notify_all()

0 commit comments

Comments
 (0)