The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-screen): a live X server whose lock file was reaped no longer gets its display re-allocated
teknium1dea71ed92531
diff --git a/tests/tools/test_bot_desktop_runtime.py b/tests/tools/test_bot_desktop_runtime.pyindex 2ac6c803c90..c01423bef04 100644--- a/tests/tools/test_bot_desktop_runtime.py+++ b/tests/tools/test_bot_desktop_runtime.py@@ -63,6 +63,26 @@ def test_recorded_display_held_by_a_live_server_is_not_reused(tmp_path, monkeypaassert runtime._allocate_display() == 37, "a free recorded number is reclaimed"+def test_live_server_without_its_lock_file_still_owns_the_display(tmp_path, monkeypatch):+ """Regression for #109941: a /tmp reaper removes ``.X<n>-lock`` while Xvnc keeps running. The server's+ abstract socket ``@/tmp/.X11-unix/X<n>`` stays bound for its whole life, so that is the liveness the+ allocator must honour — handing the number out makes the next launcher die with 'server already running'."""+ monkeypatch.setattr(runtime, "_X_LOCK_DIR", tmp_path / "xlocks") # no lock file for anyone+ (tmp_path / "xlocks").mkdir()+ table = tmp_path / "unix"+ table.write_text(+ "Num RefCount Protocol Flags Type St Inode Path\n"+ "0000000000000000: 00000002 00000000 00010000 0001 01 22242 @/tmp/.X11-unix/X20\n"+ "0000000000000000: 00000002 00000000 00010000 0001 01 22243 /tmp/.X11-unix/X21\n"+ "0000000000000000: 00000003 00000000 00000000 0001 03 22244 @/tmp/.X11-unix/X200\n", encoding="utf-8")+ monkeypatch.setattr(runtime, "_X_UNIX_TABLE", table)+ assert runtime._display_in_use(20) is True+ assert runtime._display_in_use(21) is True+ assert runtime._display_in_use(22) is False # :200 is not :22 — no prefix matching+ monkeypatch.setattr(runtime, "_X_UNIX_TABLE", tmp_path / "missing")+ assert runtime._display_in_use(20) is False, "no table (non-Linux procfs) falls back to the lock alone"++_FAKE_LAUNCHER = """#!/usr/bin/env bash# Stands in for launcher.sh + Xvnc: the X lock appears only after a delay (the TOCTOU window), then the@@ -188,6 +208,7 @@ def in_process_runtime(tmp_path, monkeypatch):monkeypatch.setattr(runtime, "state_dir", lambda: home / "bot-desktop")monkeypatch.setattr(runtime, "_LAUNCHER", tmp_path / "launcher.sh")monkeypatch.setattr(runtime, "_X_LOCK_DIR", tmp_path / "xlocks")+ monkeypatch.setattr(runtime, "_X_UNIX_TABLE", tmp_path / "unix") # the host's real X servers stay out of the bandmonkeypatch.setattr(runtime, "_ALLOC_LOCK", tmp_path / "alloc.lock")monkeypatch.setattr(runtime, "missing_binaries", lambda: [])monkeypatch.setattr(runtime, "geometry", lambda: "800x600")@@ -258,6 +279,25 @@ def test_readiness_timeout_terminates_the_launch_it_gave_up_on(in_process_runtimassert _gone(int(lock.read_text())), "the launch's X server must die with its launcher"+_DYING_LAUNCHER = """#!/usr/bin/env bash+# Xvnc refusing the number ('server already running'): the launcher exits non-zero at once.+exit 1+"""++[email protected]_only+def test_failed_start_does_not_pin_the_profile_to_the_number_that_failed(in_process_runtime):+ """Regression for #109941: after a launcher failure the recorded ``display`` kept naming the number, and+ ``_pick_display`` reuses the recorded number first — so every retry picked the same occupied display and+ the profile wedged. A failed start forgets its number; the next attempt allocates afresh."""+ scratch = in_process_runtime+ (scratch / "launcher.sh").write_text(_DYING_LAUNCHER, encoding="utf-8")+ sd = runtime.state_dir()+ with pytest.raises(RuntimeError, match="launcher exited"):+ runtime.start(wait_seconds=5)+ assert not (sd / "display").exists(), "a number that just failed must not be recorded for reuse"++@pytest.mark.linux_onlydef test_allocation_lock_is_released_once_xvnc_claims_the_number(in_process_runtime):"""The host-wide allocation lock exists for the pick→X-lock window only. Holding it for the whole Xfcediff --git a/tools/bot_desktop/runtime.py b/tools/bot_desktop/runtime.pyindex 9210c6d8bd8..07e89c90661 100644--- a/tools/bot_desktop/runtime.py+++ b/tools/bot_desktop/runtime.py@@ -179,6 +179,7 @@ def _recorded_launcher_pid() -> Optional[int]:_X_LOCK_DIR = Path("/tmp") # where X servers write .X<n>-lock (tests point it at a scratch dir)+_X_UNIX_TABLE = Path("/proc/net/unix") # the kernel's list of bound Unix sockets (tests point it at a fixture)def _x_lock_pid(num: int) -> Optional[int]:@@ -188,11 +189,22 @@ def _x_lock_pid(num: int) -> Optional[int]:return None+def _x_socket_bound(num: int) -> bool:+ """A running X server keeps ``@/tmp/.X11-unix/X<n>`` (abstract namespace) bound for its whole life; the+ kernel drops it only when the process exits. A /tmp reaper can remove the lock file under a live Xvnc,+ and only this binding then still says the number is taken (a new server on it dies 'already running')."""+ try:+ lines = _X_UNIX_TABLE.read_text(encoding="utf-8").splitlines()+ except OSError:+ return False+ return any(line.split()[-1].lstrip("@") == f"/tmp/.X11-unix/X{num}" for line in lines if line.strip())++def _display_in_use(num: int) -> bool:- """A live X server owns ``:num``: its lock file names a running pid. A lock left by a crashed- server (dead pid) does not count, so the number can be reclaimed."""+ """A live X server owns ``:num``: its lock file names a running pid, or its X11 socket is bound. A lock+ left by a crashed server (dead pid, no socket) does not count, so the number can be reclaimed."""pid = _x_lock_pid(num)- return pid is not None and _pid_alive(pid)+ return (pid is not None and _pid_alive(pid)) or _x_socket_bound(num)def _reap_orphaned_server(sd: Path) -> bool:@@ -482,7 +494,13 @@ def start(*, wait_seconds: float = 15.0) -> DesktopStatus:if _launcher_pid() is None:_reap_orphaned_server(sd)_ALLOC_LOCK.parent.mkdir(parents=True, exist_ok=True, mode=0o700)- return _spawn_and_wait(sd, wait_seconds)+ try:+ return _spawn_and_wait(sd, wait_seconds)+ except RuntimeError:+ # _pick_display reuses the recorded number first: left in place after a failed launch (e.g. Xvnc+ # 'server already running' on it), every retry would pick the same number and the profile wedges.+ (sd / "display").unlink(missing_ok=True)+ raisedef _spawn_and_wait(sd: Path, wait_seconds: float) -> DesktopStatus: