Skip to content

The agent that grows with you

fix(browser): a stop racing a relaunch of the same key never forgets the replacement

Tekniumffc748638713
diff --git a/tests/tools/test_browser_launcher.py b/tests/tools/test_browser_launcher.py
index ed841a658a1..1ff21f8ebc4 100644
--- a/tests/tools/test_browser_launcher.py
+++ b/tests/tools/test_browser_launcher.py
@@ -15,6 +15,7 @@ import stat
import subprocess
import sys
import textwrap
+import threading
import time
from pathlib import Path
@@ -199,6 +200,40 @@ def test_a_dead_browser_is_dropped_so_the_next_launch_starts_a_new_one(fake_chro
assert err is None and second.pid != first.pid
[email protected]_system_guard_bypass # the fake browser's reparented helper is ours to stop
+def test_a_stop_racing_a_relaunch_never_forgets_the_replacement(fake_chromium):
+ """Once a stop has signalled the old browser, a concurrent launch of the same key sees it dead and starts a
+ replacement. The stop finishing afterwards must not drop the replacement from the registry nor delete its
+ throwaway profile (the same per-process dir for the same key)."""
+ old, _ = launcher.launch_chromium("racy", env=dict(os.environ))
+ real_terminate, relaunch = launcher._terminate, {}
+
+ def terminate_then_relaunch(pid, start_time, proc):
+ real_terminate(pid, start_time, proc)
+ launcher._terminate = real_terminate # one race only
+ worker = threading.Thread(target=lambda: relaunch.update(
+ browser=launcher.launch_chromium("racy", env=dict(os.environ))[0]))
+ relaunch["worker"] = worker
+ worker.start()
+ worker.join(timeout=2) # a launch serialized behind this stop is still waiting here
+
+ launcher._terminate = terminate_then_relaunch
+ try:
+ launcher.stop_browser("racy")
+ relaunch["worker"].join(timeout=60)
+ finally:
+ launcher._terminate = real_terminate
+ new = relaunch.get("browser")
+ try:
+ assert new is not None and new.pid != old.pid
+ assert launcher.get_browser("racy") is new, "the stop forgot the replacement browser"
+ assert Path(new.user_data_dir, "DevToolsActivePort").exists(), "the stop deleted the replacement's profile"
+ finally:
+ if new is not None and launcher.get_browser("racy") is not new:
+ real_terminate(new.pid, new.start_time, None) # untracked by the bug: stop it here
+
+
@pytest.mark.platforms("posix")
@pytest.mark.live_system_guard_bypass # the fake browser's reparented helper is ours to stop
def test_launch_failure_reports_the_exit_and_leaves_nothing_registered(tmp_path, monkeypatch, fake_chromium):
diff --git a/tools/browser_launcher.py b/tools/browser_launcher.py
index d77a341e7af..c08ae4dfc49 100644
--- a/tools/browser_launcher.py
+++ b/tools/browser_launcher.py
@@ -488,8 +488,10 @@ def get_browser(key: str) -> Optional[LaunchedBrowser]:
def _forget(key: str, browser: LaunchedBrowser, *, drop_record: bool = True) -> None:
- _browsers.pop(key, None)
- proc = _procs.pop(key, None)
+ proc = None
+ if _browsers.get(key) is browser: # a replacement registered since is not this browser's to drop
+ _browsers.pop(key)
+ proc = _procs.pop(key, None)
if proc is not None:
try:
proc.wait(timeout=0) # reap the zombie
@@ -525,7 +527,13 @@ def _terminate(pid: Optional[int], start_time: Optional[int], proc: Optional[sub
def stop_browser(key: str) -> None:
"""Let go of ``key``'s browser. A throwaway one this process launched is stopped. A shared-profile one is
stopped only by its LAST holder across Hermes processes (:func:`_last_holder_out`); one a human started is
- never stopped."""
+ never stopped. Holds ``key``'s launch lock: a relaunch of ``key`` waits for this stop instead of
+ registering a replacement on the dir it is about to delete."""
+ with _key_lock(key):
+ _stop_locked(key)
+
+
+def _stop_locked(key: str) -> None:
with _lock:
browser = _browsers.get(key)
proc = _procs.get(key)