Skip to content

The agent that grows with you

fix(bot-screen): launcher.pid carries the process birth time, not just a pid

teknium17a36c99bbaf6
diff --git a/tests/tools/test_bot_desktop_runtime.py b/tests/tools/test_bot_desktop_runtime.py
index 31c28cba953..caa544f006e 100644
--- a/tests/tools/test_bot_desktop_runtime.py
+++ b/tests/tools/test_bot_desktop_runtime.py
@@ -26,6 +26,24 @@ def test_no_running_screen_returns_none_without_grabbing(monkeypatch):
assert thumbnail.thumbnail_data_url() is None
+def test_recycled_pid_is_not_our_launcher(tmp_path, monkeypatch):
+ """launcher.pid names pid + create_time; a live pid born at another time is a stranger (recycled pid)
+ and must read as not running, or stop() would killpg an unrelated session. Legacy single-number
+ files and absurd digit strings are also not running."""
+ import os
+
+ monkeypatch.setattr(runtime, "state_dir", lambda: tmp_path)
+ pidfile = tmp_path / "launcher.pid"
+ pidfile.write_text(f"{os.getpid()} 12345.0", encoding="utf-8") # alive, wrong birth
+ assert runtime._launcher_pid() is None
+ pidfile.write_text(str(os.getpid()), encoding="utf-8") # pre-identity format
+ assert runtime._launcher_pid() is None
+ pidfile.write_text("9" * 40 + " 1.0", encoding="utf-8")
+ assert runtime._launcher_pid() is None
+ pidfile.write_text(f"{os.getpid()} {runtime._create_time(os.getpid())}", encoding="utf-8")
+ assert runtime._launcher_pid() == os.getpid()
+
+
def test_recorded_display_held_by_a_live_server_is_not_reused(tmp_path, monkeypatch):
"""After profile A stops, B may take A's number; A restarting must pick another rather than
unlink B's socket and lock."""
diff --git a/tools/bot_desktop/runtime.py b/tools/bot_desktop/runtime.py
index e6ecb8e6a0a..9f18a3637cc 100644
--- a/tools/bot_desktop/runtime.py
+++ b/tools/bot_desktop/runtime.py
@@ -128,12 +128,28 @@ def _pid_alive(pid: int) -> bool:
return psutil.pid_exists(pid)
+def _create_time(pid: int) -> Optional[float]:
+ import psutil
+ try:
+ return psutil.Process(pid).create_time()
+ except (psutil.Error, OverflowError, ValueError):
+ return None
+
+
def _launcher_pid() -> Optional[int]:
+ """The live launcher's pid, or None. ``launcher.pid`` holds ``"<pid> <create_time>"``: a recycled pid
+ with a different start time is somebody else's process and must never be reported as ours nor
+ killed by :func:`stop`. The pre-identity single-number format is treated as not running."""
raw = _read(state_dir() / "launcher.pid")
- if not raw or not raw.isdigit():
+ pid_s, _, born_s = (raw or "").partition(" ")
+ if not pid_s.isdigit() or not born_s:
+ return None
+ try:
+ pid, born = int(pid_s), float(born_s)
+ except ValueError:
return None
- pid = int(raw)
- return pid if _pid_alive(pid) else None
+ actual = _create_time(pid)
+ return pid if actual is not None and abs(actual - born) < 0.01 else None
def _display_in_use(num: int) -> bool:
@@ -295,7 +311,8 @@ def start(*, wait_seconds: float = 15.0) -> DesktopStatus:
["bash", str(_LAUNCHER)], env=child_env, stdin=subprocess.DEVNULL, stdout=log, stderr=log,
start_new_session=True, close_fds=True)
log.close()
- (sd / "launcher.pid").write_text(str(proc.pid), encoding="utf-8")
+ born = _create_time(proc.pid)
+ (sd / "launcher.pid").write_text(f"{proc.pid} {born if born is not None else 0}", encoding="utf-8")
deadline = time.monotonic() + wait_seconds
while time.monotonic() < deadline: