From 2ae5ee5ed4858358276142762945008e8d2d292c Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Sat, 15 Apr 2023 15:32:39 -0700 Subject: [PATCH] tests: fix and work around some race conditions in tests These race conditions exist even with the GIL but are unlikely to be triggered unless the GIL is disabled. --- Lib/test/test_concurrent_futures.py | 6 +++--- Lib/test/test_faulthandler.py | 2 +- Lib/test/test_sys.py | 7 ++++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py index 4707157335..198186cddf 100644 --- a/Lib/test/test_concurrent_futures.py +++ b/Lib/test/test_concurrent_futures.py @@ -918,9 +918,9 @@ def acquire_lock(lock): def test_idle_thread_reuse(self): executor = self.executor_type() - executor.submit(mul, 21, 2).result() - executor.submit(mul, 6, 7).result() - executor.submit(mul, 3, 14).result() + for args in [(21, 2), (6, 7), (3, 14)]: + executor.submit(mul, *args).result() + time.sleep(0.01) # wait a tiny bit for the thread to be idle self.assertEqual(len(executor._threads), 1) executor.shutdown(wait=True) diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index f816573d30..2407261aca 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -580,7 +580,7 @@ def run(self): regex = r""" ^Thread 0x[0-9a-f]+ \(most recent call first\): (?: File ".*threading.py", line [0-9]+ in [_a-z]+ - ){{1,3}} File "", line 23 in run + ){{1,3}} File "", line (22|23) in run File ".*threading.py", line [0-9]+ in _bootstrap_inner File ".*threading.py", line [0-9]+ in _bootstrap diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index ab2ad7f645..118fcdc73a 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -477,6 +477,7 @@ def test_current_exceptions(self): # thread does sys._current_frames(), and verifies that the frames # returned make sense. entered_g = threading.Event() + raised_err = threading.Event() leave_g = threading.Event() thread_info = [] # the thread's id @@ -490,6 +491,7 @@ def g456(): try: raise ValueError("oops") except ValueError: + raised_err.set() if leave_g.wait(timeout=support.LONG_TIMEOUT): break @@ -503,6 +505,8 @@ def g456(): self.assertEqual(len(thread_info), 1) thread_id = thread_info[0] + raised_err.wait() + d = sys._current_exceptions() for tid in d: self.assertIsInstance(tid, int) @@ -529,7 +533,8 @@ def g456(): # And the next record must be for g456(). filename, lineno, funcname, sourceline = stack[i+1] self.assertEqual(funcname, "g456") - self.assertTrue(sourceline.startswith("if leave_g.wait(")) + self.assertTrue(sourceline.startswith("if leave_g.wait(") or + sourceline.startswith("raised_err.set(")) # Reap the spawned thread. leave_g.set()