The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-screen): the orphan reaper finds a lock-less X server through the socket it binds
teknium19be0e710d527
diff --git a/tests/tools/test_bot_desktop_runtime.py b/tests/tools/test_bot_desktop_runtime.pyindex c01423bef04..45500aee831 100644--- a/tests/tools/test_bot_desktop_runtime.py+++ b/tests/tools/test_bot_desktop_runtime.py@@ -246,6 +246,37 @@ def test_orphaned_x_server_of_a_dead_launcher_is_reaped_on_next_start(in_processassert runtime.stop() is True[email protected]_only[email protected]_system_guard_bypass # the orphan is reparented to init: signalling it is the point+def test_orphaned_x_server_is_found_by_its_socket_when_the_lock_file_is_gone(tmp_path, monkeypatch):+ """Case B of #109941: the launcher was SIGKILLed AND a /tmp reaper removed ``.X<n>-lock`` (or the failed+ launch dropped ``display``). The lock was the reaper's only handle, so the live Xvnc leaked forever and+ each restart allocated a new number beside it. The socket path on its command line names it too."""+ import os+ import shutil+ import subprocess++ sd = tmp_path / "bot-desktop"+ sd.mkdir()+ (sd / "rfb.sock").touch()+ (sd / "launcher.pid").write_text("1 0.0", encoding="utf-8") # a dead launcher, not our process group+ monkeypatch.setattr(runtime, "_X_LOCK_DIR", tmp_path / "xlocks") # no lock file at all+ (tmp_path / "xlocks").mkdir()+ # argv[0] names the fake Xvnc and argv carries our socket path, exactly what launcher.sh's Xvnc shows;+ # `tail -f` on the socket file just blocks like a server would (a multicall coreutils rejects a symlink).+ orphan = subprocess.Popen([str(tmp_path / "Xvnc"), "-f", str(sd / "rfb.sock")], executable=shutil.which("tail"),+ start_new_session=True, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL)+ try:+ assert runtime._reap_orphaned_server(sd) is True+ assert _wait_until(lambda: _gone(orphan.pid)), "the lock-less orphan must be reaped, not leaked"+ assert not (sd / "rfb.sock").exists()+ finally:+ with contextlib.suppress(ProcessLookupError):+ os.kill(orphan.pid, 9)+ orphan.wait()++_SLOW_LAUNCHER = """#!/usr/bin/env bash# Publishes only AFTER runtime.start()'s readiness deadline has passed.sleep 30 &diff --git a/tools/bot_desktop/runtime.py b/tools/bot_desktop/runtime.pyindex c747aca7d3a..f7b17d3768d 100644--- a/tools/bot_desktop/runtime.py+++ b/tools/bot_desktop/runtime.py@@ -213,26 +213,33 @@ def _reap_orphaned_server(sd: Path) -> bool:``status()`` keys on the launcher and says stopped, and a naive restart allocates a second server nextto it and overwrites the socket path both now claim. The X lock of the recorded display names thatserver: it is ours when it sits in the dead launcher's process group or its command line binds OUR- socket. Kill it (group first), drop the state it left, and report whether anything was signalled."""+ socket. When a /tmp reaper took the lock too (or the failed launch dropped ``display``), the socket path+ on the Xvnc command line is the remaining handle — without it one X server leaks per occurrence. Kill it+ (group first), drop the state it left, and report whether anything was signalled."""import psutil+ def _binds_our_socket(cmdline: list) -> bool:+ return "Xvnc" in Path(cmdline[0] if cmdline else "").name and str(sd / "rfb.sock") in cmdline+recorded = _read(sd / "display")pid = _x_lock_pid(int(recorded)) if recorded and recorded.isdigit() else Noneif pid is None or not _pid_alive(pid):- return False+ pid = next((p.pid for p in psutil.process_iter(["cmdline"]) if _binds_our_socket(p.info["cmdline"] or [])), None)+ if pid is None:+ return Falselauncher = _recorded_launcher_pid()try:pgid = os.getpgid(pid) # windows-footgun: ok — Linux-only runtime (is_supported_host gates start/stop)cmdline = psutil.Process(pid).cmdline()except (ProcessLookupError, psutil.Error):return False- binds_our_socket = "Xvnc" in Path(cmdline[0] if cmdline else "").name and str(sd / "rfb.sock") in cmdline- if pgid != launcher and not binds_our_socket:+ if pgid != launcher and not _binds_our_socket(cmdline):return False # somebody else's server took the number after we died; never touch itlogger.warning("Bot Desktop launcher %s is gone but its X server (pid %s) survived on :%s; reaping",- launcher, pid, recorded)+ launcher, pid, recorded or "?")_kill_group_then_wait(pgid if pgid == launcher else None, pid)- (_X_LOCK_DIR / f".X{recorded}-lock").unlink(missing_ok=True)+ if recorded:+ (_X_LOCK_DIR / f".X{recorded}-lock").unlink(missing_ok=True)for name in ("launcher.pid", "env", "rfb.sock"):(sd / name).unlink(missing_ok=True)return True