DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(launcher): stop killing a backend that is still starting
Bingxi Zhao (Frank)883c90f2ccef
diff --git a/deeptutor/runtime/banner.py b/deeptutor/runtime/banner.pyindex 8687271ea..bf9178651 100644--- a/deeptutor/runtime/banner.py+++ b/deeptutor/runtime/banner.py@@ -122,7 +122,11 @@ LABELS: dict[str, dict[str, str]] = {"stop.requested": "Requested a graceful stop from launcher PID {pid}.","stop.complete": "DeepTutor stopped.","stop.timeout": "Launcher PID {pid} did not stop in time. Check {log}.",- "start.not_ready": "{name} did not become ready within {timeout}s",+ "start.not_ready": (+ "{name} did not become ready within {timeout}s. "+ "Slow hardware or a workspace with data to migrate can need longer; "+ "set {env} to a larger number of seconds."+ ),"start.port_in_use": ("DeepTutor cannot start because port(s) already in use: {ports}. ""Stop the existing process or change data/user/settings/system.json."@@ -236,7 +240,11 @@ LABELS: dict[str, dict[str, str]] = {"stop.requested": "已请求 launcher PID {pid} 正常停止。","stop.complete": "DeepTutor 已停止。","stop.timeout": "launcher PID {pid} 未能及时停止,请检查 {log}。",- "start.not_ready": "{name} 在 {timeout} 秒内未就绪",+ "start.not_ready": (+ "{name} 在 {timeout} 秒内未就绪。"+ "硬件较慢或工作区需要迁移数据时启动会更久,"+ "可将 {env} 设为更大的秒数。"+ ),"start.port_in_use": ("无法启动 DeepTutor,端口已被占用: {ports}。""请先停止占用进程,或修改 data/user/settings/system.json 中的端口设置。"diff --git a/deeptutor/runtime/launcher.py b/deeptutor/runtime/launcher.pyindex 4d0074c83..25d8fbb0d 100644--- a/deeptutor/runtime/launcher.py+++ b/deeptutor/runtime/launcher.py@@ -32,8 +32,31 @@ from deeptutor.runtime.memory_probe import SUPERVISOR_PID_ENVfrom deeptutor.runtime.process import is_process_alivefrom deeptutor.services.app_update import LAUNCHER_PID_ENV-BACKEND_READY_TIMEOUT = 60-FRONTEND_READY_TIMEOUT = 120+BACKEND_READY_TIMEOUT_ENV = "DEEPTUTOR_BACKEND_READY_TIMEOUT"+FRONTEND_READY_TIMEOUT_ENV = "DEEPTUTOR_FRONTEND_READY_TIMEOUT"+++def _ready_timeout(env_name: str, default: int) -> int:+ """Seconds to wait for a child to answer, overridable per deployment.++ The wait has to end somewhere, but where is a property of the machine, not+ of the product: on ARM boards and on workspaces with data to migrate the+ backend has been seen reaching ``Application startup complete`` at 78s,+ and a fixed 60 killed it mid-initialisation and let systemd restart it+ into the same wall (#1435).+ """+ raw = str(os.environ.get(env_name, "")).strip()+ if not raw:+ return default+ try:+ seconds = int(raw)+ except ValueError:+ return default+ return seconds if seconds > 0 else default+++BACKEND_READY_TIMEOUT = _ready_timeout(BACKEND_READY_TIMEOUT_ENV, 60)+FRONTEND_READY_TIMEOUT = _ready_timeout(FRONTEND_READY_TIMEOUT_ENV, 120)FRONTEND_REUSE_PROBE_TIMEOUT = 2KILL_SIGNAL = getattr(signal, "SIGKILL", signal.SIGTERM)WEB_CACHE_DIR = Path("data") / "user" / "runtime" / "web"@@ -511,6 +534,7 @@ def _wait_for_http(url: str,process: ManagedProcess | None,timeout: int,+ env_name: str,should_stop: Callable[[], bool],) -> None:_log(_t("start.waiting_for", name=name, url=url))@@ -526,7 +550,7 @@ def _wait_for_http(returnexcept (urlerror.URLError, TimeoutError, OSError):time.sleep(0.5)- raise RuntimeError(_t("start.not_ready", name=name, timeout=timeout))+ raise RuntimeError(_t("start.not_ready", name=name, timeout=timeout, env=env_name))def _http_ready(url: str, *, timeout: float) -> bool:@@ -1434,6 +1458,7 @@ def start(url=f"http://127.0.0.1:{backend_port}/",process=backend,timeout=BACKEND_READY_TIMEOUT,+ env_name=BACKEND_READY_TIMEOUT_ENV,should_stop=should_stop,)if should_stop():@@ -1447,6 +1472,7 @@ def start(url=frontend_url,process=None,timeout=FRONTEND_READY_TIMEOUT,+ env_name=FRONTEND_READY_TIMEOUT_ENV,should_stop=should_stop,)else:@@ -1458,6 +1484,7 @@ def start(url=f"http://127.0.0.1:{frontend_port}/",process=web,timeout=FRONTEND_READY_TIMEOUT,+ env_name=FRONTEND_READY_TIMEOUT_ENV,should_stop=should_stop,)if should_stop():diff --git a/tests/runtime/test_launcher.py b/tests/runtime/test_launcher.pyindex 334c97dc5..bb31efb03 100644--- a/tests/runtime/test_launcher.py+++ b/tests/runtime/test_launcher.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport builtinsfrom pathlib import Path+from types import SimpleNamespaceimport pytest@@ -700,3 +701,46 @@ def test_stop_requests_only_the_registered_detached_launcher(assert launcher.stop(tmp_path, timeout=0.5) is Trueassert not paths.state.exists()assert not paths.stop.exists()+++def test_ready_timeout_is_overridable_for_slow_hardware(monkeypatch) -> None:+ """An ARM board with a workspace to migrate can need past 60s (#1435).++ The launcher killed a backend that was still initialising and let the+ supervisor restart it into the same wall, so the wait has to be a property+ of the machine rather than a constant.+ """+ monkeypatch.setenv(launcher.BACKEND_READY_TIMEOUT_ENV, "180")+ assert launcher._ready_timeout(launcher.BACKEND_READY_TIMEOUT_ENV, 60) == 180++ for unusable in ("", " ", "soon", "0", "-5"):+ monkeypatch.setenv(launcher.BACKEND_READY_TIMEOUT_ENV, unusable)+ assert launcher._ready_timeout(launcher.BACKEND_READY_TIMEOUT_ENV, 60) == 60++ monkeypatch.delenv(launcher.BACKEND_READY_TIMEOUT_ENV, raising=False)+ assert launcher._ready_timeout(launcher.BACKEND_READY_TIMEOUT_ENV, 60) == 60+++def test_ready_timeout_failure_names_the_override(monkeypatch) -> None:+ """A bare "did not become ready in 60s" left the reporter nothing to do."""+ process = SimpleNamespace(process=SimpleNamespace(poll=lambda: None))+ clock = iter([0.0, 0.0, 999.0])+ monkeypatch.setattr(launcher.time, "monotonic", lambda: next(clock))+ monkeypatch.setattr(launcher.time, "sleep", lambda _seconds: None)+ monkeypatch.setattr(+ launcher.urlrequest,+ "urlopen",+ lambda *_args, **_kwargs: (_ for _ in ()).throw(OSError("refused")),+ )++ with pytest.raises(RuntimeError) as excinfo:+ launcher._wait_for_http(+ name="Backend",+ url="http://127.0.0.1:65535/",+ process=process,+ timeout=60,+ env_name=launcher.BACKEND_READY_TIMEOUT_ENV,+ should_stop=lambda: False,+ )++ assert launcher.BACKEND_READY_TIMEOUT_ENV in str(excinfo.value)