From 4742cbc0f483624ffc9bcf9c39b2bd4715ced9b2 Mon Sep 17 00:00:00 2001 From: Andreas Schwab Date: Thu, 10 Aug 2023 22:19:46 +0200 Subject: [PATCH] gh-107851: reduce race condition in test_flock and test_lockf Avoid a spurious test failure. When the parent process sleeps longer between tries than the subprocess holds the lock, the synchronisation can fail. --- Lib/test/_test_eintr.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/_test_eintr.py b/Lib/test/_test_eintr.py index 15586f15dfab30..16f29473df2a73 100644 --- a/Lib/test/_test_eintr.py +++ b/Lib/test/_test_eintr.py @@ -506,7 +506,9 @@ def _lock(self, lock_func, lock_name): with open(os_helper.TESTFN, 'wb') as f: # synchronize the subprocess start_time = time.monotonic() - for _ in support.sleeping_retry(support.LONG_TIMEOUT, error=False): + # make sure to sleep significantly less than sleep_time + # between tries, to minimize the race with the subprocess + for _ in support.sleeping_retry(support.LONG_TIMEOUT, error=False, max_delay=self.sleep_time / 2): try: lock_func(f, fcntl.LOCK_EX | fcntl.LOCK_NB) lock_func(f, fcntl.LOCK_UN)