diff --git a/backend/app/eval/runner.py b/backend/app/eval/runner.py index af8718f7..a552a528 100644 --- a/backend/app/eval/runner.py +++ b/backend/app/eval/runner.py @@ -976,19 +976,24 @@ async def _queued(driver: Any) -> bool: async def within(coro: Any, budget: float, paused: Any, *, tick: float = 0.5) -> Any: """同 asyncio.wait_for(coro, budget),只是 paused() 为真的那几段不算时间(复核:满载时排队等名额不该算进每题时限、触发止损)。 - 超时取消 coro、抛 TimeoutError。""" + 超时取消 coro、抛 TimeoutError。每隔一小段(tick,且不超过时限的 1/20)看一次;一段的头尾有一头在暂停就整段不算—— + 宁可少算一点,不把排队的时间算进去。""" loop = asyncio.get_running_loop() task = asyncio.ensure_future(coro) + step_s = max(0.005, min(tick, budget / 20)) spent, last = 0.0, loop.time() + was_paused = await paused() try: while True: - done, _ = await asyncio.wait({task}, timeout=max(0.02, min(tick, budget - spent))) + done, _ = await asyncio.wait({task}, timeout=max(0.005, min(step_s, budget - spent))) if task in done: return task.result() now = loop.time() step, last = now - last, now - if not await paused(): + is_paused = await paused() + if not (was_paused or is_paused): spent += step + was_paused = is_paused if spent >= budget: task.cancel() with contextlib.suppress(asyncio.CancelledError, Exception): diff --git a/backend/tests/test_eval_runner.py b/backend/tests/test_eval_runner.py index 94a9c106..6aaaa484 100644 --- a/backend/tests/test_eval_runner.py +++ b/backend/tests/test_eval_runner.py @@ -290,7 +290,7 @@ async def test_a_case_over_its_time_limit_stops_its_run(monkeypatch): async def test_time_queued_for_a_run_slot_does_not_count_against_the_case_limit(monkeypatch): - """复核:满载时作答运行排队等名额的那一段不算每题时限(排了 0.8 秒、时限 0.3 秒,拿到名额后很快答完:不记超时)。""" + """复核:满载时作答运行排队等名额的那一段不算每题时限(排了 2 秒、时限 1 秒,拿到名额后很快答完:不记超时)。""" from app.cluster import slots stored = slots.values({"max_total": 2, "interactive_reserve": 1}) @@ -321,7 +321,7 @@ async def test_time_queued_for_a_run_slot_does_not_count_against_the_case_limit( async def prepare(self): units = await original(self) - self.budget["per_case_timeout_s"] = 0.3 + self.budget["per_case_timeout_s"] = 1.0 return units monkeypatch.setattr(runner.EvalExecution, "_prepare", prepare) @@ -333,7 +333,7 @@ async def test_time_queued_for_a_run_slot_does_not_count_against_the_case_limit( [run] = await start(suite_id) while not await slots.waiting("eval-answer-in-queue"): await asyncio.sleep(0.02) - await asyncio.sleep(0.8) + await asyncio.sleep(2.0) gate.set() await es.wait_eval(run.id) finally: