Skip to content

The agent that grows with you

fix(bot-screen): install timeout releases the profile slot even when a root child survives

teknium1004b43f15cc7
diff --git a/tests/tools/test_bot_desktop_install.py b/tests/tools/test_bot_desktop_install.py
index a3e5392a66a..076dd2a55ad 100644
--- a/tests/tools/test_bot_desktop_install.py
+++ b/tests/tools/test_bot_desktop_install.py
@@ -97,3 +97,37 @@ def test_timeout_kills_the_package_managers_whole_process_group(monkeypatch):
else:
subprocess.run(["kill", "-9", str(child)], check=False)
pytest.fail("grandchild survived the install timeout")
+
+
+def test_timeout_returns_and_frees_the_slot_even_when_a_descendant_survives(monkeypatch):
+ """From an unprivileged Hermes, killpg reaches the sudo leader but not a root-owned apt child; that
+ child keeps the pipe's write end open, so draining stdout never sees EOF and the profile slot stays
+ taken forever. The timeout must end the drain and release the slot regardless of what survived.
+ Stand-in for the unkillable root child: a grandchild in its own session holding our stdout."""
+ import subprocess
+ import time
+
+ monkeypatch.setattr(install, "_sudo_nopasswd", lambda: True)
+ monkeypatch.setattr(install, "_TERM_GRACE_SECONDS", 0.2, raising=False)
+ real_popen = subprocess.Popen
+
+ def popen(argv, **kw):
+ if argv[:1] != ["sudo"]:
+ return real_popen(argv, **kw)
+ return real_popen(["sh", "-c", "setsid sleep 30 & echo child $!; wait"], **kw)
+
+ monkeypatch.setattr(install.subprocess, "Popen", popen)
+ lines: list[str] = []
+ started = time.monotonic()
+ worker = threading.Thread(target=lambda: lines.append(
+ f"code {install.install_packages(ask_password=lambda: '', on_line=lines.append, timeout_seconds=0.5)}"))
+ worker.start()
+ worker.join(3.0)
+ survivor = next((int(line.split()[1]) for line in lines if line.startswith("child ")), None)
+ if survivor:
+ subprocess.run(["kill", "-9", str(survivor)], check=False)
+ assert not worker.is_alive(), f"install_packages hung {time.monotonic() - started:.1f}s on a surviving descendant"
+ assert any(line.startswith("code ") and line != "code 0" for line in lines), lines
+ assert any("timed out" in line for line in lines), lines
+ install.release(install.claim()) # slot is free again
diff --git a/tools/bot_desktop/install.py b/tools/bot_desktop/install.py
index ee44b7e06b5..ef58869da1a 100644
--- a/tools/bot_desktop/install.py
+++ b/tools/bot_desktop/install.py
@@ -15,10 +15,12 @@ from __future__ import annotations
import contextlib
import logging
import os
+import selectors
import shlex
import signal
import subprocess
import threading
+import time
from typing import Callable, Optional
from hermes_constants import hermes_home_key
@@ -102,24 +104,61 @@ def _run(cmd: str, *, ask_password: Callable[[], str], on_line: Callable[[str],
env = {"DEBIAN_FRONTEND": "noninteractive", "LC_ALL": "C.UTF-8"}
proc = subprocess.Popen( # windows-footgun: ok — Linux-only (is_supported_host)
argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
- env={**os.environ, **env}, text=True, encoding="utf-8", errors="replace", start_new_session=True)
+ env={**os.environ, **env}, start_new_session=True)
try:
if stdin_payload is not None:
- proc.stdin.write(stdin_payload) # type: ignore[union-attr]
+ proc.stdin.write(stdin_payload.encode("utf-8")) # type: ignore[union-attr]
proc.stdin.close() # type: ignore[union-attr]
except OSError:
pass
- # The package manager runs in its own session (start_new_session); killing only sudo would leave apt/dnf
- # running as root with the dpkg lock while the slot is released, so the whole group goes.
- def _kill_group() -> None:
- with contextlib.suppress(ProcessLookupError):
- os.killpg(proc.pid, signal.SIGKILL) # windows-footgun: ok — Linux-only (is_supported_host)
-
- timer = threading.Timer(timeout_seconds, _kill_group)
- timer.start()
try:
- for line in proc.stdout: # type: ignore[union-attr]
- on_line(line.rstrip("\n"))
- return proc.wait()
+ if _drain_until(proc, on_line, time.monotonic() + timeout_seconds):
+ return proc.wait()
+ _kill_group(proc)
+ on_line(f"install timed out after {timeout_seconds:.0f}s; the package manager may still be running as root")
+ return proc.returncode if proc.returncode is not None else -9
finally:
- timer.cancel()
+ proc.stdout.close() # type: ignore[union-attr]
+
+
+_TERM_GRACE_SECONDS = 5.0
+
+
+def _drain_until(proc: subprocess.Popen, on_line: Callable[[str], None], deadline: float) -> bool:
+ """Stream ``proc.stdout`` lines to ``on_line`` until EOF (``True``) or ``deadline`` (``False``).
+
+ Readiness-polled rather than a blocking ``for line in proc.stdout``: from an unprivileged Hermes no
+ signal reaches a root-owned apt/dnf child, and that child keeps the pipe's write end open, so a
+ blocking read would never see EOF and the profile's install slot would be held forever.
+ """
+ fd = proc.stdout.fileno() # type: ignore[union-attr]
+ buf = b""
+ with selectors.DefaultSelector() as sel:
+ sel.register(fd, selectors.EVENT_READ)
+ while True:
+ remaining = deadline - time.monotonic()
+ if remaining <= 0:
+ return False
+ if not sel.select(timeout=min(remaining, 1.0)):
+ continue
+ chunk = os.read(fd, 65536)
+ if not chunk:
+ if buf:
+ on_line(buf.decode("utf-8", "replace"))
+ return True
+ *lines, buf = (buf + chunk).split(b"\n")
+ for line in lines:
+ on_line(line.decode("utf-8", "replace"))
+
+
+def _kill_group(proc: subprocess.Popen) -> None:
+ """The package manager runs in its own session (start_new_session); killing only sudo would leave
+ apt/dnf running as root with the dpkg lock while the slot is released, so the whole group goes: TERM
+ first so dpkg can finish its transaction, KILL after the grace. Best effort — as non-root neither
+ signal reaches a root-owned child, which is why the caller never waits on EOF."""
+ for sig, grace in ((signal.SIGTERM, _TERM_GRACE_SECONDS), (signal.SIGKILL, 1.0)): # windows-footgun: ok — Linux-only (is_supported_host)
+ with contextlib.suppress(ProcessLookupError, PermissionError):
+ os.killpg(proc.pid, sig) # windows-footgun: ok — Linux-only (is_supported_host)
+ with contextlib.suppress(subprocess.TimeoutExpired):
+ proc.wait(timeout=grace)
+ return