Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(update): refuse in-app handoff inside systemd services (#1555)

Bingxi Zhao (Frank)e02c82bdf0f1
diff --git a/deeptutor/api/routers/system.py b/deeptutor/api/routers/system.py
index c31d300f8..1df28d1d4 100644
--- a/deeptutor/api/routers/system.py
+++ b/deeptutor/api/routers/system.py
@@ -21,6 +21,7 @@ from deeptutor.api.routers.auth import require_admin
from deeptutor.multi_user.context import get_current_user
from deeptutor.runtime import memory_probe
from deeptutor.services.app_update import (
+ SYSTEMD_UPDATE_REASON,
Installation,
UpdateInProgressError,
UpdateJob,
@@ -31,6 +32,7 @@ from deeptutor.services.app_update import (
detect_installation,
get_version_check_service,
launcher_available,
+ running_under_systemd_service,
update_store_root,
)
from deeptutor.services.config import (
@@ -269,6 +271,8 @@ async def request_managed_update(_request: ManagedUpdateRequest) -> dict[str, An
settings = get_runtime_settings_service().load_system()
if not settings["version_check_enabled"]:
raise HTTPException(status_code=409, detail="Version checks are disabled")
+ if running_under_systemd_service():
+ raise HTTPException(status_code=409, detail=SYSTEMD_UPDATE_REASON)
if not launcher_available():
raise HTTPException(
status_code=409,
diff --git a/deeptutor/runtime/launcher.py b/deeptutor/runtime/launcher.py
index 57f931f84..4d5568a62 100644
--- a/deeptutor/runtime/launcher.py
+++ b/deeptutor/runtime/launcher.py
@@ -1214,8 +1214,10 @@ def _handoff_pending_update(
"""Hand a pending Web update to a detached worker before shutdown."""
from deeptutor.services.app_update import (
+ SYSTEMD_UPDATE_REASON,
UpdateJobStore,
launch_update_worker,
+ running_under_systemd_service,
update_store_root,
)
@@ -1226,6 +1228,14 @@ def _handoff_pending_update(
return False
if job.status != "pending":
return False
+ # A pending job from an older backend can reach this launcher despite the
+ # API guard. Keep the service alive: setsid does not leave its cgroup.
+ if running_under_systemd_service():
+ try:
+ store.mark_failed(job.id, SYSTEMD_UPDATE_REASON)
+ except Exception as exc:
+ _log(f"Could not record rejected systemd update: {exc}")
+ return False
try:
store.prepare_handoff(
job.id,
diff --git a/deeptutor/services/app_update.py b/deeptutor/services/app_update.py
index 037746030..0b2850a54 100644
--- a/deeptutor/services/app_update.py
+++ b/deeptutor/services/app_update.py
@@ -28,6 +28,11 @@ GITHUB_LATEST_RELEASE_URL = "https://api.github.com/repos/HKUDS/DeepTutor/releas
GITHUB_LATEST_RELEASE_WEB_URL = "https://github.com/HKUDS/DeepTutor/releases/latest"
VERSION_CHECK_TTL_SECONDS = 24 * 60 * 60
LAUNCHER_PID_ENV = "DEEPTUTOR_LAUNCHER_PID"
+SYSTEMD_UPDATE_REASON = (
+ "In-app updates are unavailable under a systemd service because systemd may stop "
+ "the update worker with the service. Stop the service, upgrade DeepTutor with "
+ "the same Python environment, then start the service with systemctl."
+)
InstallMode = Literal["pypi", "source", "docker", "unknown"]
JobStatus = Literal["pending", "handoff", "running", "restarting", "succeeded", "failed"]
@@ -175,6 +180,33 @@ def _running_from_source_checkout() -> bool:
return (checkout_root / ".git").exists() and (checkout_root / "pyproject.toml").is_file()
+def _systemd_service_unit(cgroup_text: str) -> str | None:
+ """Find a service unit in either cgroup v1 or v2 membership text."""
+ for line in cgroup_text.splitlines():
+ fields = line.split(":", 2)
+ if len(fields) != 3:
+ continue
+ for component in fields[2].split("/"):
+ if component.endswith(".service"):
+ return component
+ return None
+
+
+def running_under_systemd_service() -> bool:
+ """A detached process group does not escape a systemd service cgroup."""
+ if sys.platform != "linux":
+ return False
+ # systemd supplies this to service processes, including when the service
+ # uses a private cgroup namespace and /proc/self/cgroup reads as '/'.
+ if os.getenv("INVOCATION_ID"):
+ return True
+ try:
+ membership = Path("/proc/self/cgroup").read_text(encoding="utf-8")
+ except OSError:
+ return False
+ return _systemd_service_unit(membership) is not None
+
+
def detect_installation() -> Installation:
"""Classify only layouts whose update ownership is unambiguous."""
@@ -216,6 +248,14 @@ def detect_installation() -> Installation:
in_virtualenv = Path(sys.prefix).resolve() != Path(sys.base_prefix).resolve()
if not direct_url and in_virtualenv:
+ if running_under_systemd_service():
+ return Installation(
+ mode="pypi",
+ current_version=__version__,
+ automatic_update=False,
+ command=f"{sys.executable} -m pip install -U deeptutor",
+ reason=SYSTEMD_UPDATE_REASON,
+ )
return Installation(
mode="pypi",
current_version=__version__,
@@ -618,6 +658,7 @@ __all__ = [
"Installation",
"LAUNCHER_PID_ENV",
"ReleaseInfo",
+ "SYSTEMD_UPDATE_REASON",
"UpdateInProgressError",
"UpdateJob",
"UpdateJobStore",
@@ -629,6 +670,7 @@ __all__ = [
"get_version_check_service",
"launch_update_worker",
"launcher_available",
+ "running_under_systemd_service",
"reset_version_check_service_for_tests",
"update_store_root",
]
diff --git a/tests/api/test_system_router.py b/tests/api/test_system_router.py
index 1dc40501c..fd12aea78 100644
--- a/tests/api/test_system_router.py
+++ b/tests/api/test_system_router.py
@@ -129,6 +129,22 @@ async def test_managed_update_refuses_live_conversation(
assert getattr(raised.value, "status_code", None) == 409
+async def test_managed_update_refuses_systemd_before_reserving_job(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(system_router, "get_runtime_settings_service", _UpdateSettings)
+ monkeypatch.setattr(system_router, "running_under_systemd_service", lambda: True)
+
+ with pytest.raises(Exception) as raised:
+ await system_router.request_managed_update(
+ system_router.ManagedUpdateRequest(confirmation="update-and-restart")
+ )
+
+ assert getattr(raised.value, "status_code", None) == 409
+ assert "systemctl" in str(getattr(raised.value, "detail", ""))
+
+
@pytest.mark.asyncio
async def test_managed_update_creates_durable_job(
monkeypatch: pytest.MonkeyPatch,
diff --git a/tests/runtime/test_launcher.py b/tests/runtime/test_launcher.py
index 5ef6666f5..9af4ef84c 100644
--- a/tests/runtime/test_launcher.py
+++ b/tests/runtime/test_launcher.py
@@ -160,6 +160,29 @@ def test_launcher_hands_pending_update_to_worker(tmp_path: Path) -> None:
assert job.restart_home == str(tmp_path.resolve())
+def test_launcher_keeps_systemd_service_running_for_pending_update(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ from deeptutor.services import app_update
+
+ store = UpdateJobStore(update_store_root(tmp_path))
+ store.create(current_version="1.6.1", target_version="1.7.0")
+ monkeypatch.setattr(app_update, "running_under_systemd_service", lambda: True)
+ launched: list[Path] = []
+
+ assert (
+ launcher._handoff_pending_update(
+ tmp_path,
+ restart_argv=["start", "--home", str(tmp_path.resolve())],
+ worker_launcher=launched.append,
+ )
+ is False
+ )
+ assert launched == []
+ assert store.load().status == "failed"
+ assert not store.active_path.exists()
+
+
def test_launcher_completes_update_only_after_restart(tmp_path: Path) -> None:
store = UpdateJobStore(update_store_root(tmp_path))
pending = store.create(current_version="1.6.1", target_version="1.7.0")
diff --git a/tests/services/test_app_update.py b/tests/services/test_app_update.py
index 3a2a009eb..494610b53 100644
--- a/tests/services/test_app_update.py
+++ b/tests/services/test_app_update.py
@@ -167,6 +167,34 @@ def test_detect_installation_keeps_source_and_docker_host_managed(
assert installation.automatic_update is True
+def test_systemd_service_cgroup_disables_in_app_update(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ assert (
+ app_update._systemd_service_unit("0::/system.slice/deeptutor.service\n")
+ == "deeptutor.service"
+ )
+ assert (
+ app_update._systemd_service_unit(
+ "1:name=systemd:/user.slice/[email protected]/app.slice/deeptutor.service\n"
+ )
+ )
+ assert app_update._systemd_service_unit("0::/user.slice/session-2.scope\n") is None
+
+ monkeypatch.setattr(app_update, "_running_in_container", lambda: False)
+ monkeypatch.setattr(app_update, "_running_from_source_checkout", lambda: False)
+ monkeypatch.setattr(app_update, "_distribution_direct_url", lambda: {})
+ monkeypatch.setattr(app_update, "running_under_systemd_service", lambda: True)
+ monkeypatch.setattr(app_update.sys, "prefix", str(tmp_path / "venv"))
+ monkeypatch.setattr(app_update.sys, "base_prefix", str(tmp_path / "base"))
+
+ installation = app_update.detect_installation()
+ assert installation.mode == "pypi"
+ assert installation.automatic_update is False
+ assert "systemctl" in installation.reason
+
+
def test_detect_installation_prefers_source_checkout_when_metadata_is_shadowed(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,