From 6bcd199c410d9885b5bcb9895649fb9ce0ece231 Mon Sep 17 00:00:00 2001 From: tushar-mahalya Date: Mon, 23 Nov 2026 14:07:39 +0530 Subject: [PATCH] fix(evaluator): give evaluations a free worker after a timeout A timed-out evaluation keeps running in its executor thread. With the default parallel_evaluations=1 that thread is the only one, so every later evaluation queued behind it and timed out without starting, even for valid programs. Replace the executor when a call times out. --- openevolve/evaluator.py | 76 ++++++++++++++++----------------- tests/test_evaluator_timeout.py | 32 ++++++++++++++ 2 files changed, 69 insertions(+), 39 deletions(-) diff --git a/openevolve/evaluator.py b/openevolve/evaluator.py index 1a5f42129..f9ebf97fb 100644 --- a/openevolve/evaluator.py +++ b/openevolve/evaluator.py @@ -65,10 +65,7 @@ def __init__( # Dedicated executor (not the loop's default) so a timed-out evaluation's # orphaned thread can't block asyncio.run()'s shutdown_default_executor(). - self._executor = ThreadPoolExecutor( - max_workers=max(1, config.parallel_evaluations), - thread_name_prefix="openevolve-eval", - ) + self._executor = self._new_executor() # Set up evaluation function if file exists self._load_evaluation_function() @@ -78,6 +75,35 @@ def __init__( logger.info(f"Initialized evaluator with {evaluation_file}") + def _new_executor(self) -> ThreadPoolExecutor: + return ThreadPoolExecutor( + max_workers=max(1, self.config.parallel_evaluations), + thread_name_prefix="openevolve-eval", + ) + + async def _run_with_timeout(self, func: Callable, *args: Any) -> Any: + """ + Run a blocking evaluation function in the executor, bounded by config.timeout + + A timed-out call keeps running in its thread because Python cannot stop it. + That thread would hold an executor slot indefinitely (forever for an infinite + loop), so later evaluations would queue behind it and time out without ever + starting. The executor is replaced on timeout so they get a free slot. + + Raises: + asyncio.TimeoutError: If the function exceeds the timeout + """ + loop = asyncio.get_running_loop() + try: + return await asyncio.wait_for( + loop.run_in_executor(self._executor, func, *args), timeout=self.config.timeout + ) + except asyncio.TimeoutError: + stuck_executor = self._executor + self._executor = self._new_executor() + stuck_executor.shutdown(wait=False) + raise + def _load_evaluation_function(self) -> None: """Load the evaluation function from the evaluation file""" if not os.path.exists(self.evaluation_file): @@ -448,17 +474,10 @@ async def _direct_evaluate( Exception: If evaluation function raises an exception """ - # Create a coroutine that runs the evaluation function in an executor - async def run_evaluation(): - loop = asyncio.get_event_loop() - return await loop.run_in_executor( - self._executor, - functools.partial(self.evaluate_function, **self._evaluator_kwargs), - program_path, - ) - - # Run the evaluation with timeout - let exceptions bubble up for retry handling - result = await asyncio.wait_for(run_evaluation(), timeout=self.config.timeout) + # Let exceptions bubble up for retry handling + result = await self._run_with_timeout( + functools.partial(self.evaluate_function, **self._evaluator_kwargs), program_path + ) # Return result as-is to be processed by _process_evaluation_result # This supports both dict and EvaluationResult returns, just like _cascade_evaluate @@ -497,14 +516,7 @@ async def _cascade_evaluate( # Run first stage with timeout try: - - async def run_stage1(): - loop = asyncio.get_event_loop() - return await loop.run_in_executor( - self._executor, module.evaluate_stage1, program_path - ) - - stage1_result = await asyncio.wait_for(run_stage1(), timeout=self.config.timeout) + stage1_result = await self._run_with_timeout(module.evaluate_stage1, program_path) stage1_eval_result = self._process_evaluation_result(stage1_result) except asyncio.TimeoutError: logger.warning(f"Stage 1 evaluation timed out after {self.config.timeout}s") @@ -540,14 +552,7 @@ async def run_stage1(): # Run second stage with timeout try: - - async def run_stage2(): - loop = asyncio.get_event_loop() - return await loop.run_in_executor( - self._executor, module.evaluate_stage2, program_path - ) - - stage2_result = await asyncio.wait_for(run_stage2(), timeout=self.config.timeout) + stage2_result = await self._run_with_timeout(module.evaluate_stage2, program_path) stage2_eval_result = self._process_evaluation_result(stage2_result) except asyncio.TimeoutError: logger.warning(f"Stage 2 evaluation timed out after {self.config.timeout}s") @@ -604,14 +609,7 @@ async def run_stage2(): # Run third stage with timeout try: - - async def run_stage3(): - loop = asyncio.get_event_loop() - return await loop.run_in_executor( - self._executor, module.evaluate_stage3, program_path - ) - - stage3_result = await asyncio.wait_for(run_stage3(), timeout=self.config.timeout) + stage3_result = await self._run_with_timeout(module.evaluate_stage3, program_path) stage3_eval_result = self._process_evaluation_result(stage3_result) except asyncio.TimeoutError: logger.warning(f"Stage 3 evaluation timed out after {self.config.timeout}s") diff --git a/tests/test_evaluator_timeout.py b/tests/test_evaluator_timeout.py index b544e4867..02a590301 100644 --- a/tests/test_evaluator_timeout.py +++ b/tests/test_evaluator_timeout.py @@ -407,6 +407,38 @@ async def run_test(): self.assertLess(outer_elapsed, 5) + def test_evaluation_after_timeout_gets_a_free_worker(self): + """A timed-out evaluation's orphaned thread must not make the next, + valid evaluation queue behind it and time out as well.""" + + async def run_test(): + evaluator = self._create_evaluator(timeout=1) + + result = await evaluator.evaluate_program("# SLEEP_LONG", "test_stuck") + self.assertTrue(result.get("timeout")) + + start_time = time.time() + result = await evaluator.evaluate_program("def test(): return 'fast'", "test_next") + self.assertLess(time.time() - start_time, 0.9) + self.assertNotIn("timeout", result) + self.assertEqual(result["score"], 0.5) + + asyncio.run(run_test()) + + def test_cascade_evaluation_after_timeout_gets_a_free_worker(self): + async def run_test(): + evaluator = self._create_evaluator(timeout=1, cascade_evaluation=True) + + result = await evaluator.evaluate_program("# STAGE1_TIMEOUT", "test_stuck") + self.assertTrue(result.get("timeout")) + + result = await evaluator.evaluate_program("def test(): return 'fast'", "test_next") + self.assertNotIn("timeout", result) + self.assertEqual(result["stage1_score"], 0.7) + + asyncio.run(run_test()) + + class TestTimeoutIntegration(unittest.TestCase): """Integration tests for timeout functionality"""