Skip to content

The agent that grows with you

fix(browser): load the teardown helper on launch so an exit-time stop works

Teknium1344fbc18c86
diff --git a/tests/tools/test_browser_launcher.py b/tests/tools/test_browser_launcher.py
index b86fd164cdf..3552fc1a2be 100644
--- a/tests/tools/test_browser_launcher.py
+++ b/tests/tools/test_browser_launcher.py
@@ -138,6 +138,31 @@ def test_two_processes_on_one_profile_never_share_an_ephemeral_profile(fake_chro
assert (other / "Default" / "Cookies").read_text() == "the other process's session"
[email protected]_system_guard_bypass # the fake browser is ours to stop if the exit teardown failed
+def test_a_bare_consumer_exiting_normally_stops_its_browser(fake_chromium):
+ """A script / eval that imports only the session layer and exits normally: the atexit teardown must stop
+ the browser (the teardown's process-tree helper is loaded before exit, not first imported during it)."""
+ script = ("import os, sys\n"
+ "import tools.browser_session # registers the exit teardown\n"
+ "from tools import browser_launcher\n"
+ "browser_launcher.needs_chromium_sandbox_bypass = lambda: False\n"
+ "browser, err = browser_launcher.launch_chromium('bare', executable=sys.argv[1], headed=False,\n"
+ " env=dict(os.environ), timeout=30)\n"
+ "print(browser.pid if browser else err, flush=True)\n")
+ done = subprocess.run([sys.executable, "-c", script, str(fake_chromium)], check=False,
+ cwd=Path(__file__).resolve().parents[2],
+ env=dict(os.environ), capture_output=True, text=True, timeout=120)
+ pid = int(done.stdout.strip().splitlines()[0])
+ deadline = time.monotonic() + 10
+ while launcher._pid_alive(pid) and time.monotonic() < deadline:
+ time.sleep(0.1)
+ survived = launcher._pid_alive(pid)
+ if survived and str(fake_chromium) in Path(f"/proc/{pid}/cmdline").read_text(errors="replace"):
+ os.kill(pid, signal.SIGKILL)
+ assert not survived, done.stderr[-2000:]
+
+
@pytest.mark.platforms("posix")
@pytest.mark.live_system_guard_bypass # the fake browser's reparented helper is ours to stop
def test_stop_signals_the_browser_before_its_helpers_and_removes_the_ephemeral_profile(fake_chromium):
diff --git a/tools/browser_launcher.py b/tools/browser_launcher.py
index 1c827c91f08..1e803529e71 100644
--- a/tools/browser_launcher.py
+++ b/tools/browser_launcher.py
@@ -417,6 +417,9 @@ def _launch_owned(key: str, executable: str, udd: str, *, ephemeral: bool, packa
if env is None:
from tools.browser_session import build_browser_env
env = build_browser_env()
+ # Loaded now, not first at teardown: stopping the browser from an atexit hook must not be this module's
+ # first import (Python refuses new atexit registrations during shutdown, orphaning the browser).
+ import tools.process_registry
try:
_prepare_dir(udd, ephemeral)
except OSError as exc: