Skip to content

The agent that grows with you

fix(browser): the real-profile copy runs inside the copy dir's cross-process launch step

Teknium1d05b1c851d5
diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py
index bda46b748d3..938c30b34ef 100644
--- a/tests/tools/test_browser_real_profile.py
+++ b/tests/tools/test_browser_real_profile.py
@@ -203,6 +203,7 @@ class TestRealProfileCdpLaunch:
def test_snapshot_failure_fails_closed(self):
with patch.object(bs_cloud, "use_real_profile", return_value=True), \
patch("hermes_cli.browser_connect.detect_default_chromium", return_value="chrome"), \
+ patch("hermes_cli.browser_connect.chromium_executable", return_value="/usr/bin/chrome"), \
patch("hermes_cli.browser_connect.snapshot_real_profile", return_value=(None, "boom")):
cdp, err = rp.real_profile_cdp()
assert cdp is None
diff --git a/tests/tools/test_browser_real_profile_copy_lock.py b/tests/tools/test_browser_real_profile_copy_lock.py
new file mode 100644
index 00000000000..6b743b5ac81
--- /dev/null
+++ b/tests/tools/test_browser_real_profile_copy_lock.py
@@ -0,0 +1,119 @@
+"""The real-profile snapshot (the overlay of the user's Cookies / Login Data onto the profile copy) is part of the
+launch step under the copy dir's cross-process launcher lock: two Hermes processes of one profile never copy into
+the same snapshot dir at once, nor over a copy-browser that just came up (the later one attaches instead), and a
+different profile's copy never waits on it (``tools/browser_session_real_profile.py``).
+"""
+
+from __future__ import annotations
+
+import json
+import os
+import sqlite3
+import subprocess
+import sys
+import textwrap
+from contextlib import closing
+from pathlib import Path
+
+import pytest
+
+from tests.tools.browser_launcher_fakes import kill_fake, pids, write_fake_chromium
+
+pytestmark = pytest.mark.platforms("linux")
+
+_REPO = Path(__file__).resolve().parents[2]
+
+# One Hermes process resolving its real-profile browser. The snapshot is the real copier on a fixture profile,
+# wrapped only to log when it runs and to stay inside long enough for a rival process to show up.
+_SESSION = textwrap.dedent("""\
+ import json, os, sys, time
+ from unittest.mock import patch
+ import hermes_cli.browser_connect as bc
+ from tools import browser_lightpanda, browser_session, browser_session_cloud
+ from tools import browser_session_real_profile as rp
+ chrome, src, log, gate = sys.argv[1:5]
+ real_snapshot = bc.snapshot_real_profile
+
+ def note(event):
+ with open(log, "a") as f:
+ f.write(f"{event} {os.getpid()}\\n")
+
+ def snapshot(browser, _src=None):
+ note("enter")
+ deadline = time.monotonic() + 2.0 # wait for a rival's copy to start, if it can
+ while open(log).read().count("enter") < 2 and time.monotonic() < deadline:
+ time.sleep(0.05)
+ try:
+ return real_snapshot(browser, src)
+ finally:
+ note("exit")
+
+ while not os.path.exists(gate):
+ time.sleep(0.01)
+ with patch.object(browser_session_cloud, "use_real_profile", return_value=True), \\
+ patch.object(browser_lightpanda, "using_lightpanda_engine", return_value=False), \\
+ patch.object(browser_session, "ensure_screen_for_headed_chromium", lambda: None), \\
+ patch.object(bc, "detect_default_chromium", return_value="chrome"), \\
+ patch.object(bc, "chromium_executable", return_value=chrome), \\
+ patch.object(bc, "snapshot_real_profile", side_effect=snapshot):
+ cdp, err = rp.real_profile_cdp()
+ from tools.browser_launcher import get_browser
+ running = get_browser(rp.real_profile_key())
+ print(json.dumps({"cdp": cdp, "err": err, "owned": bool(running and running.owned)}), flush=True)
+ sys.stdin.readline()
+ rp.terminate_real_profile_browser()
+""")
+
+
+def _profile(root: Path) -> Path:
+ (root / "Default").mkdir(parents=True)
+ (root / "Local State").write_text('{"os_crypt": {}}', encoding="utf-8")
+ with closing(sqlite3.connect(root / "Default" / "Cookies")) as conn, conn:
+ conn.execute("create table marker(value)")
+ conn.execute("insert into marker values('signed-in')")
+ return root
+
+
+def _overlapped(events: list[str]) -> bool:
+ inside = 0
+ for event in events:
+ inside += 1 if event == "enter" else -1
+ if inside > 1:
+ return True
+ return False
+
+
[email protected]_system_guard_bypass # the fake browsers are ours to stop if a leg failed
[email protected]("second", ["same-profile", "other-profile"])
+def test_only_one_process_of_a_profile_copies_its_real_profile_at_a_time(tmp_path, second):
+ chrome = write_fake_chromium(tmp_path / "chromium")
+ src, log, gate = _profile(tmp_path / "real-profile"), tmp_path / "snapshots.log", tmp_path / "go"
+ homes = [tmp_path / "home-a", tmp_path / "home-a" if second == "same-profile" else tmp_path / "home-b"]
+ sessions = []
+ for home in homes:
+ home.mkdir(exist_ok=True)
+ sessions.append(subprocess.Popen(
+ [sys.executable, "-c", _SESSION, str(chrome), str(src), str(log), str(gate)], cwd=_REPO,
+ env={**os.environ, "HERMES_HOME": str(home)}, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True))
+ try:
+ gate.touch()
+ results = [json.loads(session.stdout.readline()) for session in sessions]
+ events = [line.split()[0] for line in log.read_text().splitlines()]
+
+ assert all(r["err"] is None and r["cdp"] for r in results), results
+ if second == "same-profile":
+ assert not _overlapped(events), f"two processes copied into one snapshot dir at once: {events}"
+ assert events == ["enter", "exit"], "the later process re-copied over the running copy-browser"
+ assert results[0]["cdp"] == results[1]["cdp"] and sorted(r["owned"] for r in results) == [False, True]
+ else:
+ assert _overlapped(events), f"another profile's copy waited on this one: {events}"
+ assert results[0]["cdp"] != results[1]["cdp"]
+ copy = homes[0] / "browser-profile" / "chrome" / "Default" / "Cookies"
+ with closing(sqlite3.connect(copy)) as conn:
+ assert conn.execute("select value from marker").fetchone()[0] == "signed-in"
+ finally:
+ for session in sessions:
+ session.kill()
+ for home in set(homes):
+ for pid in pids(home / "browser-profile" / "chrome", "launches"):
+ kill_fake(pid, chrome)
diff --git a/tools/browser_launcher.py b/tools/browser_launcher.py
index f7e52b319b6..8f9cbcede7f 100644
--- a/tools/browser_launcher.py
+++ b/tools/browser_launcher.py
@@ -30,7 +30,7 @@ import threading
import time
from dataclasses import dataclass
from pathlib import Path
-from typing import Any, Optional
+from typing import Any, Callable, Optional
logger = logging.getLogger(__name__)
@@ -407,7 +407,7 @@ def _launch_record_name(records: Path, browser: LaunchedBrowser) -> Optional[str
return own if own in names else next(iter(names), None)
-def _start_shared(key: str, executable: str, udd: str, home_key: str,
+def _start_shared(key: str, executable: str, udd: str, home_key: str, prepare,
**launch) -> tuple[Optional[LaunchedBrowser], Optional[str]]:
"""Attach to, or launch, shared profile ``udd``'s one Chromium as ONE step under the profile's cross-process
lock: of two processes starting it at once the later waits, then attaches (a second launch would wipe the
@@ -419,6 +419,9 @@ def _start_shared(key: str, executable: str, udd: str, home_key: str,
with holders.records_lock(state) if state is not None else contextlib.nullcontext():
attached = _attach_running(key, udd, home_key)
if attached is None:
+ failure = prepare() if prepare is not None else None
+ if failure:
+ return None, failure
return _launch_owned(key, executable, udd, ephemeral=False, home_key=home_key, **launch)
with _lock: # registered under the lock too: another key's stop must see this key as its heir
_browsers[key] = attached
@@ -428,6 +431,7 @@ def _start_shared(key: str, executable: str, udd: str, home_key: str,
def launch_chromium(key: str, *, executable: Optional[str] = None, user_data_dir: Optional[str] = None,
ephemeral: bool = True, headed: Optional[bool] = None, extra_args=(),
env: Optional[dict] = None, timeout: Optional[float] = None, sandbox_bypass: bool = True,
+ prepare: Optional[Callable[[], Optional[str]]] = None,
) -> tuple[Optional[LaunchedBrowser], Optional[str]]:
"""Start (or reuse) the Chromium for ``key``; ``(browser, None)`` or ``(None, error)``.
@@ -435,7 +439,10 @@ def launch_chromium(key: str, *, executable: Optional[str] = None, user_data_dir
Bot Desktop's, the user's real browser) is used as given. ``user_data_dir=None`` is a fresh
ephemeral profile under ``<HERMES_HOME>/cache/browser/chromium/``; a persistent dir that already has
a live Chromium on it is ATTACHED (``owned=False``, never killed). ``sandbox_bypass=False`` (the
- user's real browser) never gets the host's sandbox-bypass flags."""
+ user's real browser) never gets the host's sandbox-bypass flags. ``prepare`` (a persistent dir only) runs
+ right before a fresh launch, inside the same cross-process step, never when attaching: a profile copy
+ written there can neither race another process's copy nor land under a browser that just came up; a
+ message it returns is the launch's error, as is."""
from hermes_constants import hermes_home_key
# 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). An
@@ -459,7 +466,7 @@ def launch_chromium(key: str, *, executable: Optional[str] = None, user_data_dir
sandbox_bypass=sandbox_bypass)
if ephemeral: # a per-process dir: nobody else starts or attaches to it
return _launch_owned(key, executable, udd, ephemeral=True, home_key=home_key, **launch)
- return _start_shared(key, executable, udd, home_key, **launch)
+ return _start_shared(key, executable, udd, home_key, prepare, **launch)
def _key_lock(key: str) -> threading.Lock:
diff --git a/tools/browser_session_real_profile.py b/tools/browser_session_real_profile.py
index 38e53a3cb5d..b516bcf7909 100644
--- a/tools/browser_session_real_profile.py
+++ b/tools/browser_session_real_profile.py
@@ -92,25 +92,31 @@ def _consent_revoked() -> tuple[None, None]:
def _launch(browser_name: str) -> tuple[Optional[str], Optional[str]]:
from hermes_cli.browser_connect import chromium_executable, real_profile_copy_dir, snapshot_real_profile
- from tools.browser_launcher import launch_chromium, running_instance
+ from tools.browser_launcher import launch_chromium
from tools.browser_session import ensure_screen_for_headed_chromium
- copy_dir = real_profile_copy_dir(browser_name)
- # The snapshot overlay rewrites Cookies / Login Data: it must never run while a live copy-browser
- # (maybe from an earlier Hermes process) holds the dir open. The launcher ATTACHES to such a browser.
- if running_instance(copy_dir) is None:
- copy_dir, err = snapshot_real_profile(browser_name)
- if err or not copy_dir:
- return None, _snapshot_error(err)
real_binary = chromium_executable(browser_name)
if real_binary is None:
return None, f"{_RP}the real browser binary for '{browser_name}' could not be found. Reinstall it or turn the toggle off."
+ snapshot_failure: list[str] = []
+
+ def overlay() -> Optional[str]:
+ # The snapshot overlay rewrites Cookies / Login Data, so it runs inside the launcher's cross-process
+ # step on the copy dir: never beside another process's copy, never under a live copy-browser (maybe
+ # from an earlier Hermes process; the launcher ATTACHES to that one instead).
+ copied, err = snapshot_real_profile(browser_name)
+ if err or not copied:
+ snapshot_failure.append(_snapshot_error(err))
+ return snapshot_failure[0]
+ return None
+
ensure_screen_for_headed_chromium()
- launched, err = launch_chromium(real_profile_key(), executable=real_binary, user_data_dir=copy_dir,
- ephemeral=False, extra_args=_REAL_PROFILE_EXTRA_FLAGS, timeout=30.0,
- sandbox_bypass=False)
+ launched, err = launch_chromium(real_profile_key(), executable=real_binary,
+ user_data_dir=real_profile_copy_dir(browser_name), ephemeral=False,
+ extra_args=_REAL_PROFILE_EXTRA_FLAGS, timeout=30.0, sandbox_bypass=False,
+ prepare=overlay)
if launched is None:
- return None, f"{_RP}the real-profile browser failed to start: {err}"
- logger.info("real-profile browser ready for %s at %s (%s)", browser_name, launched.http_url, copy_dir)
+ return None, snapshot_failure[0] if snapshot_failure else f"{_RP}the real-profile browser failed to start: {err}"
+ logger.info("real-profile browser ready for %s at %s (%s)", browser_name, launched.http_url, launched.user_data_dir)
return launched.cdp_url, None