Skip to content

Commit dd89a41

Browse files
committed
gh-101267: make the regression test deterministic and self-checking
* Block the workers on a never-set Event (via the harness create_event()) instead of submitting time.sleep(3): the sleep could finish before the worker is terminated on a loaded machine, completing the future and raising no BrokenProcessPool. * Use try/except/else with self.fail() rather than seeding count = None, so a missing exception is reported clearly. assertRaises() can't be used here because it strips the traceback the test needs to inspect.
1 parent c367036 commit dd89a41

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

Lib/test/test_concurrent_futures/test_process_pool.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,30 @@ def test_killed_child(self):
6363
# Submitting other jobs fails as well.
6464
self.assertRaises(BrokenProcessPool, self.executor.submit, pow, 2, 8)
6565

66+
@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
6667
def test_broken_process_pool_traceback(self):
6768
# When a child process is abruptly terminated, the whole pool gets
6869
# "broken", and a BrokenProcessPool exception should be created
6970
# for each future instead of sharing one exception among all futures.
70-
futures = [self.executor.submit(time.sleep, 3) for _ in range(3)]
71-
# Get one of the processes, and terminate (kill) it.
71+
event = self.create_event()
72+
futures = [self.executor.submit(event.wait) for _ in range(3)]
7273
p = next(iter(self.executor._processes.values()))
7374
p.terminate()
7475
for fut in futures:
75-
count = None
76+
# Don't use assertRaises(): it clears the traceback off exc.
7677
try:
7778
fut.result()
78-
except BrokenProcessPool as e:
79-
count = sum(
80-
1
81-
for frame_summary in traceback.extract_tb(e.__traceback__)
82-
if frame_summary.filename == __file__
83-
)
79+
except BrokenProcessPool as exc:
80+
tb = exc.__traceback__
81+
else:
82+
self.fail("BrokenProcessPool not raised")
83+
count = sum(
84+
1
85+
for frame_summary in traceback.extract_tb(tb)
86+
if frame_summary.filename == __file__
87+
)
8488
# This code file should appear exactly once in the traceback.
89+
# A shared exception would accumulate a frame per result() call.
8590
self.assertEqual(count, 1)
8691

8792
@warnings_helper.ignore_fork_in_thread_deprecation_warnings()

0 commit comments

Comments
 (0)