gh-111119: Fix flaky test test_lock_two_threads (gh-111124)

This commit is contained in:
Sam Gross 2023-10-20 18:51:34 +02:00 committed by GitHub
parent cb4f7462d1
commit 264f4af506
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 4 deletions

View File

@ -75,10 +75,18 @@ test_lock_two_threads(PyObject *self, PyObject *obj)
assert(test_data.m.v == 1); assert(test_data.m.v == 1);
PyThread_start_new_thread(lock_thread, &test_data); PyThread_start_new_thread(lock_thread, &test_data);
while (!_Py_atomic_load_int(&test_data.started)) {
pysleep(10); // wait up to two seconds for the lock_thread to attempt to lock "m"
} int iters = 0;
pysleep(10); // allow some time for the other thread to try to lock uint8_t v;
do {
pysleep(10); // allow some time for the other thread to try to lock
v = _Py_atomic_load_uint8_relaxed(&test_data.m.v);
assert(v == 1 || v == 3);
iters++;
} while (v != 3 && iters < 200);
// both the "locked" and the "has parked" bits should be set
assert(test_data.m.v == 3); assert(test_data.m.v == 3);
PyMutex_Unlock(&test_data.m); PyMutex_Unlock(&test_data.m);