From ba5b13667b4243892c23bb13fe8ab743fb94010d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Fri, 23 Oct 2026 16:00:18 +0800 Subject: [PATCH] fix: hold batch admission through startup recovery --- .../trial_task_center/service.py | 29 +++++++++-------- .../trial_task_center/task_queue.py | 7 +++- tests/v1_1/test_batch_supervisor.py | 32 +++++++++++++++++++ 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/dx_productivity/trial_task_center/service.py b/src/dx_productivity/trial_task_center/service.py index 2e2b4c5..ce87502 100644 --- a/src/dx_productivity/trial_task_center/service.py +++ b/src/dx_productivity/trial_task_center/service.py @@ -3,7 +3,7 @@ from __future__ import annotations from collections.abc import Callable -from contextlib import contextmanager +from contextlib import contextmanager, ExitStack from datetime import datetime import os from pathlib import Path @@ -117,18 +117,21 @@ class TaskCenterService: self._path_opener = path_opener self._clock = clock self._supervised = supervised - if runner is None and recover_on_startup: - from .batch_supervisor import assert_supervisor_idle, pending_supervisors - if pending_supervisors(logging_context.root): - recover_on_startup = False - try: - assert_supervisor_idle(logging_context.root) - except TaskCenterError as error: - if error.code != 'supervisor_busy': - raise - recover_on_startup = False - self._runner = runner or TrialRunner(store, logging_context, probe_source_resolver=probe_source_resolver, - **({'recover_on_startup': False} if not recover_on_startup else {})) + with ExitStack() as startup: + if runner is None and recover_on_startup: + from .batch_supervisor import mutation_lease, pending_supervisors + try: + startup.enter_context(mutation_lease(logging_context.root)) + except TaskCenterError as error: + if error.code != 'supervisor_busy': + raise + recover_on_startup = False + if recover_on_startup and pending_supervisors(logging_context.root): + recover_on_startup = False + # Keep admission for the whole recovery, not just an idle probe: + # another window must not start a batch while startup rewrites state. + self._runner = runner or TrialRunner(store, logging_context, probe_source_resolver=probe_source_resolver, + **({'recover_on_startup': False} if not recover_on_startup else {})) self._quick_run_preparer = quick_run_preparer or QuickRunPreparer(logging_context.root) self._batch_feedback = None self._project_facades: dict[str, TaskCenterService] = {} diff --git a/src/dx_productivity/trial_task_center/task_queue.py b/src/dx_productivity/trial_task_center/task_queue.py index 9b3d606..5dc1971 100644 --- a/src/dx_productivity/trial_task_center/task_queue.py +++ b/src/dx_productivity/trial_task_center/task_queue.py @@ -60,7 +60,12 @@ def _lock_file(root: Path): def assert_task_queue_idle(root: Path) -> None: """Reject external mutations while a queue owns CAD; its runner may call services.""" from .batch_supervisor import assert_supervisor_idle - assert_supervisor_idle(root) + try: + assert_supervisor_idle(root) + except TaskCenterError as error: + if error.code != 'supervisor_busy': + raise + raise TaskCenterError('quick_run_busy', '另一批操作正在执行,请先安全暂停。') from error if _QUEUE_OWNER == get_ident(): return if _QUEUE_LOCK.locked(): diff --git a/tests/v1_1/test_batch_supervisor.py b/tests/v1_1/test_batch_supervisor.py index a8f3315..6a35118 100644 --- a/tests/v1_1/test_batch_supervisor.py +++ b/tests/v1_1/test_batch_supervisor.py @@ -90,6 +90,38 @@ def test_cancelled_request_does_not_launch_supervisor(tmp_path): assert not (tmp_path/'batch-resources').exists() +def test_startup_recovery_holds_admission_until_runner_initialized(tmp_path, monkeypatch): + from dx_productivity.trial_task_center import service as module + from dx_productivity.trial_task_center.batch_supervisor import assert_supervisor_idle + from dx_productivity.trial_task_center.errors import TaskCenterError + from dx_productivity.trial_task_center.logging_setup import configure_logging + from dx_productivity.trial_task_center.store import TaskStore + checked = [] + def runner(*args, **kwargs): + with pytest.raises(TaskCenterError, match='supervisor_busy'): + assert_supervisor_idle(tmp_path) + checked.append(True) + return object() + monkeypatch.setattr(module, 'TrialRunner', runner) + store = TaskStore(tmp_path/'task-center.db') + try: + module.TaskCenterService(store, configure_logging(tmp_path)) + assert checked == [True] + assert_supervisor_idle(tmp_path) + finally: + store.close() + + +def test_crashed_legacy_task_protects_actual_root_without_replay(tmp_path): + from dx_productivity.trial_task_center.batch_supervisor import _mark_interrupted + journal = {'version': 1, 'batch_id': 'a'*32, 'attempt_id': 'b'*32, + 'operation': 'execute_task_queue', 'task_ids': ['legacy'], 'state': 'running', + 'uncertainty_roots': {'legacy': str(tmp_path)}} + _mark_interrupted(tmp_path, journal, 'missing final result') + assert (tmp_path/'quick-run/uncertain.flag').exists() + assert not (tmp_path/'tasks/legacy/quick-run/uncertain.flag').exists() + + def test_parent_job_cleanup_failure_preserves_result_and_can_retry_resources_only(tmp_path, monkeypatch): from dx_productivity.trial_task_center.batch_supervisor import run_supervised from dx_productivity.trial_task_center.isolated_desktop import _Win32Api