From 706aaed4320a5344b63caa855bd256b9759add53 Mon Sep 17 00:00:00 2001 From: Vito Sansevero Date: Tue, 17 Nov 2026 12:48:57 -0800 Subject: [PATCH] fix(tests): the pool-reuse test makes its first job run on both threads ThreadPoolExecutor starts threads lazily and skips a new one while another is idle. The first job's tiny tasks could all run on worker 0 and leave worker 1 to start in the second job, so second <= first failed although the pool was reused (queue run 44169912829: first {worker_0}, second {worker_1}). A two-party barrier now makes the first job run on both. --- tests/test_utils_parallel.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/test_utils_parallel.py b/tests/test_utils_parallel.py index 4d058688..e31e23c0 100644 --- a/tests/test_utils_parallel.py +++ b/tests/test_utils_parallel.py @@ -51,7 +51,17 @@ class TestMapParallel(unittest.TestCase): def test_jobs_reuse_the_same_long_lived_threads(self): """Arenas are reused instead of a new set of threads per job.""" first, second = set(), set() - map_parallel(lambda _: first.add(threading.current_thread()), range(8), 2) + # The pool starts threads lazily: tiny tasks could all run on the first + # thread and leave the second to start in the next job (queue run + # 44169912829). The barrier makes the first job run on both. + both = threading.Barrier(2) + + def meet(_): + first.add(threading.current_thread()) + both.wait(WAIT) + + map_parallel(meet, range(8), 2) + self.assertEqual(len(first), 2, first) map_parallel(lambda _: second.add(threading.current_thread()), range(8), 2) self.assertTrue(second <= first, (first, second)) self.assertLessEqual(len(_job_threads()), 2)