Skip to content

Commit 264f4af

Browse files
authored
gh-111119: Fix flaky test test_lock_two_threads (gh-111124)
1 parent cb4f746 commit 264f4af

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Modules/_testinternalcapi/test_lock.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,18 @@ test_lock_two_threads(PyObject *self, PyObject *obj)
7575
assert(test_data.m.v == 1);
7676

7777
PyThread_start_new_thread(lock_thread, &test_data);
78-
while (!_Py_atomic_load_int(&test_data.started)) {
79-
pysleep(10);
80-
}
81-
pysleep(10); // allow some time for the other thread to try to lock
78+
79+
// wait up to two seconds for the lock_thread to attempt to lock "m"
80+
int iters = 0;
81+
uint8_t v;
82+
do {
83+
pysleep(10); // allow some time for the other thread to try to lock
84+
v = _Py_atomic_load_uint8_relaxed(&test_data.m.v);
85+
assert(v == 1 || v == 3);
86+
iters++;
87+
} while (v != 3 && iters < 200);
88+
89+
// both the "locked" and the "has parked" bits should be set
8290
assert(test_data.m.v == 3);
8391

8492
PyMutex_Unlock(&test_data.m);

0 commit comments

Comments
 (0)