Skip to content

The agent that grows with you

fix(browser): a sandbox Chromium that fails to start is stopped, not leaked

Tekniumb5f7e5207fe2
diff --git a/tests/tools/test_browser_cleanup.py b/tests/tools/test_browser_cleanup.py
index 8e3ed3b4f2b..0c78d7d7e98 100644
--- a/tests/tools/test_browser_cleanup.py
+++ b/tests/tools/test_browser_cleanup.py
@@ -1,8 +1,10 @@
"""Regression tests for browser session cleanup (tools/browser_session_lifecycle.py)."""
+import contextlib
import os
import subprocess
import sys
+import threading
from types import SimpleNamespace
from unittest.mock import patch
@@ -232,15 +234,25 @@ class _FakeSandbox:
self.forward_failures = 0 # connections through the port forward that never reached Chromium
self.browser_id = "abc" # a relaunch on the same port gets a new /devtools/browser/<id>
self.launches = 0
+ self.port_published = True # False: the launched Chromium never writes DevToolsActivePort
+ self.timeouts = set() # argv[0] of execs ("cat", "kill") whose docker/ssh exec times out
+ self.probe_barrier = None # holds each liveness probe until a rival session probes too
def run_in(self, env, argv, **kw):
cmd = " ".join(argv)
+ if argv[0] in self.timeouts:
+ raise subprocess.TimeoutExpired(argv, kw.get("timeout"))
if "setsid" in cmd:
self.running = 4242
self.launches += 1
return _Completed(0, b"4242\n")
+ if argv[0] == "cat" and not self.port_published:
+ return _Completed(1)
if "/dev/tcp" in cmd:
self.probes += 1
+ if self.probe_barrier is not None:
+ with contextlib.suppress(threading.BrokenBarrierError):
+ self.probe_barrier.wait(timeout=1.0)
if self.probe_failure == "exit-255":
return _Completed(255)
if self.probe_failure == "timeout":
@@ -345,6 +357,29 @@ class TestSandboxSharedBrowser:
assert fake_sandbox.probes == 1
+class TestSandboxBrowserOwnership:
+ """A sandbox Chromium this process spawned is always stoppable, and one container gets one launch."""
+
+ @pytest.mark.parametrize("failure", ["never-ready", "interrupted"])
+ def test_a_failed_start_stops_the_chromium_it_spawned(self, fake_sandbox, monkeypatch, failure):
+ from tools import browser_session_sandbox
+ from tools.browser_session_sandbox import create_sandbox_session
+ from tools.environments import streams
+ if failure == "never-ready":
+ fake_sandbox.port_published = False
+ monkeypatch.setattr(browser_session_sandbox, "_LAUNCH_TIMEOUT_S", 0.0)
+ expected = RuntimeError
+ else:
+ def interrupted(env, port, user=None):
+ raise KeyboardInterrupt
+ monkeypatch.setattr(streams, "forward_port", interrupted)
+ expected = KeyboardInterrupt
+ with pytest.raises(expected):
+ create_sandbox_session("task-A")
+ assert fake_sandbox.killed == [4242]
+ assert browser_session_sandbox._launched == {}
+
+
class _FakeContainers:
"""Several terminal sandboxes (one ``env`` object per container), each with its own Chromium."""
diff --git a/tools/browser_session_sandbox.py b/tools/browser_session_sandbox.py
index e760b4b19a5..f7ee0679eaa 100644
--- a/tools/browser_session_sandbox.py
+++ b/tools/browser_session_sandbox.py
@@ -117,6 +117,21 @@ def _wait_for_port(env, user, udd: str) -> tuple[int, str]:
raise RuntimeError("Chromium in the sandbox did not expose a DevTools endpoint in time")
+def _spawn_owned(env, user, udd: str, key: str, published: dict) -> int:
+ """Launch the sandbox Chromium and own it under ``key`` from the moment it exists, before its port is
+ up, so stop_sandbox_browser can always stop it; returns its sandbox pid."""
+ from tools.bot_desktop import sandbox_host
+ from tools.environments import streams
+ exe = sandbox_host.chromium_executable(env)
+ if not exe:
+ raise RuntimeError("the sandbox image has no Chromium to run the browser")
+ child_env = {**published, "TMPDIR": streams.sandbox_temp_dir(env)}
+ pid = _start_chromium(env, user, exe, udd, child_env)
+ with _launched_lock:
+ _launched[key] = (env, pid)
+ return pid
+
+
def create_sandbox_session(cache_key: str) -> dict[str, Any]:
"""Session on the sandbox Chromium (attach to the running one, else launch it)."""
from tools.bot_desktop import sandbox_host
@@ -130,16 +145,16 @@ def create_sandbox_session(cache_key: str) -> dict[str, Any]:
if found is None:
# Launching blind would wipe a live browser's DevToolsActivePort and lose to its singleton.
raise RuntimeError("the terminal backend's sandbox did not answer whether its browser is running; try again")
- if found is GONE:
- exe = sandbox_host.chromium_executable(env)
- if not exe:
- raise RuntimeError("the sandbox image has no Chromium to run the browser")
- child_env = {**published, "TMPDIR": streams.sandbox_temp_dir(env)}
- pid = _start_chromium(env, user, exe, udd, child_env)
- found = _wait_for_port(env, user, udd)
- with _launched_lock:
- _launched[key] = (env, pid)
- local_port = streams.forward_port(env, found[0], user=user)
+ spawned: Optional[int] = None
+ try:
+ if found is GONE:
+ spawned = _spawn_owned(env, user, udd, key, published)
+ found = _wait_for_port(env, user, udd)
+ local_port = streams.forward_port(env, found[0], user=user)
+ except BaseException:
+ if spawned is not None: # a timeout, a failed forward or an interrupt must not leave it running unowned
+ _stop_launched(key)
+ raise
info = _session_record("sb", f"ws://127.0.0.1:{local_port}{found[1]}", {"local": True, "sandbox": True})
_confirm(info["cdp_url"], env, found[0])
info.update(shared_profile=True, launcher_key=key)
@@ -187,8 +202,12 @@ def stop_sandbox_browser(session_info: dict[str, Any]) -> None:
"""SIGTERM the sandbox Chromium this process launched on the session's shared key (best effort; the
sandbox's lifetime bounds it). The caller guarantees no other live session uses it."""
_confirmed.pop(session_info.get("cdp_url") or "", None)
+ _stop_launched(session_info.get("launcher_key") or "")
+
+
+def _stop_launched(key: str) -> None:
with _launched_lock:
- launched = _launched.pop(session_info.get("launcher_key") or "", None)
+ launched = _launched.pop(key, None)
if launched is None:
return
from tools.bot_desktop import sandbox_host