Skip to content

Commit

Permalink
tests: fix and work around some race conditions in tests
Browse files Browse the repository at this point in the history
These race conditions exist even with the GIL but are unlikely to be
triggered unless the GIL is disabled.
  • Loading branch information
colesbury committed Apr 23, 2023
1 parent 398204d commit 2ae5ee5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Lib/test/test_concurrent_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_faulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<string>", line 23 in run
){{1,3}} File "<string>", line (22|23) in run
File ".*threading.py", line [0-9]+ in _bootstrap_inner
File ".*threading.py", line [0-9]+ in _bootstrap
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -490,6 +491,7 @@ def g456():
try:
raise ValueError("oops")
except ValueError:
raised_err.set()
if leave_g.wait(timeout=support.LONG_TIMEOUT):
break

Expand All @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 2ae5ee5

Please sign in to comment.