Skip to content

Commit 7709b4d

Browse files
[2.7] bpo-30727: Fix a race condition in test_threading. (GH-2334). (#2353)
(cherry picked from commit 32cb968)
1 parent 5082674 commit 7709b4d

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
@@ -359,21 +359,28 @@ def _check_notify(self, cond):
359359
# construct. In particular, it is possible that this can no longer
360360
# be conveniently guaranteed should their implementation ever change.
361361
N = 5
362+
ready = []
362363
results1 = []
363364
results2 = []
364365
phase_num = 0
365366
def f():
366367
cond.acquire()
368+
ready.append(phase_num)
367369
cond.wait()
368370
cond.release()
369371
results1.append(phase_num)
370372
cond.acquire()
373+
ready.append(phase_num)
371374
cond.wait()
372375
cond.release()
373376
results2.append(phase_num)
374377
b = Bunch(f, N)
375378
b.wait_for_started()
376-
_wait()
379+
# first wait, to ensure all workers settle into cond.wait() before
380+
# we continue. See issues #8799 and #30727.
381+
while len(ready) < 5:
382+
_wait()
383+
ready = []
377384
self.assertEqual(results1, [])
378385
# Notify 3 threads at first
379386
cond.acquire()
@@ -385,9 +392,9 @@ def f():
385392
_wait()
386393
self.assertEqual(results1, [1] * 3)
387394
self.assertEqual(results2, [])
388-
# first wait, to ensure all workers settle into cond.wait() before
389-
# we continue. See issue #8799
390-
_wait()
395+
# make sure all awaken workers settle into cond.wait()
396+
while len(ready) < 3:
397+
_wait()
391398
# Notify 5 threads: they might be in their first or second wait
392399
cond.acquire()
393400
cond.notify(5)
@@ -398,7 +405,9 @@ def f():
398405
_wait()
399406
self.assertEqual(results1, [1] * 3 + [2] * 2)
400407
self.assertEqual(results2, [2] * 3)
401-
_wait() # make sure all workers settle into cond.wait()
408+
# make sure all workers settle into cond.wait()
409+
while len(ready) < 5:
410+
_wait()
402411
# Notify all threads: they are all in their second wait
403412
cond.acquire()
404413
cond.notify_all()

0 commit comments

Comments
 (0)