Skip to content

The agent that grows with you

fix(browser): one profile's real-profile launch no longer holds up another's

Tekniumaa8104798332
diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py
index cae7980ad1c..bda46b748d3 100644
--- a/tests/tools/test_browser_real_profile.py
+++ b/tests/tools/test_browser_real_profile.py
@@ -208,36 +208,51 @@ class TestRealProfileCdpLaunch:
assert cdp is None
assert err and "boom" in err
- def test_stale_resolver_holder_fails_fast(self, monkeypatch):
- """A timed-out worker holding the launch lock must not wedge later calls."""
+ @staticmethod
+ def _resolve_in(home, result: dict):
+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override
+ token = set_hermes_home_override(str(home))
+ try:
+ result[str(home)] = rp.real_profile_cdp()
+ finally:
+ reset_hermes_home_override(token)
+
+ @pytest.mark.parametrize("second", ["same-profile", "other-profile"])
+ def test_a_held_launch_only_makes_its_own_profile_wait(self, tmp_path, monkeypatch, second):
+ """A launch stuck in an expensive step (an abandoned worker) fails its own profile's next call fast
+ with a clear error, and never holds up another profile's launch."""
import threading
+ from hermes_constants import get_hermes_home
- monkeypatch.setattr(rp, "_LOCK_TIMEOUT_S", 0.05)
- result = {}
- started = threading.Event()
+ monkeypatch.setattr(rp, "_LOCK_TIMEOUT_S", 0.5)
+ home_a, home_b = tmp_path / "a", tmp_path / "b"
+ inside_a, release_a = threading.Event(), threading.Event()
- def resolve():
- started.set()
- result.setdefault("value", rp.real_profile_cdp())
+ def detect_default_chromium(): # returns None (no Chromium default): answers right after the lock
+ if get_hermes_home() == home_a and not inside_a.is_set():
+ inside_a.set()
+ release_a.wait(timeout=10)
+ result: dict = {}
with patch.object(bs_cloud, "use_real_profile", return_value=True), \
patch.object(browser_lightpanda, "using_lightpanda_engine", return_value=False), \
- patch("hermes_cli.browser_connect.detect_default_chromium", return_value=None):
- rp._lock.acquire()
- worker = threading.Thread(target=resolve, daemon=True)
+ patch("hermes_cli.browser_connect.detect_default_chromium", side_effect=detect_default_chromium):
+ held = threading.Thread(target=self._resolve_in, args=(home_a, result), daemon=True)
+ held.start()
try:
- worker.start()
- assert started.wait(timeout=2.0), "resolver worker was not scheduled"
- worker.join(timeout=2.0)
- stalled = worker.is_alive()
+ assert inside_a.wait(timeout=5), "profile A's launch never started"
+ other: dict = {}
+ self._resolve_in(home_a if second == "same-profile" else home_b, other)
finally:
- rp._lock.release()
- worker.join(timeout=2.0)
+ release_a.set()
+ held.join(timeout=5)
- assert not stalled, "real-profile resolver waited indefinitely on a stale holder"
- cdp, err = result["value"]
+ cdp, err = next(iter(other.values()))
assert cdp is None
- assert err and "already being prepared" in err
+ if second == "same-profile":
+ assert err and "already being prepared" in err
+ else:
+ assert err and "not a supported Chromium browser" in err
def test_launch_is_headless_on_the_real_binary_and_keeps_the_keychain(self, tmp_path, monkeypatch):
"""Real-profile browsing runs headless (no focus-stealing window) on the user's REAL binary, on the
diff --git a/tools/browser_session_real_profile.py b/tools/browser_session_real_profile.py
index 2ddfea5b957..38e53a3cb5d 100644
--- a/tools/browser_session_real_profile.py
+++ b/tools/browser_session_real_profile.py
@@ -19,7 +19,10 @@ _RP = "browser.use_real_profile is on, but "
# Keep lock contention well inside the agent's 420-second outer tool deadline (an abandoned holder
# may be a daemon worker Python cannot terminate).
_LOCK_TIMEOUT_S = 30.0
-_lock = threading.Lock()
+# One launch lock per profile home (the real-profile browser and its copy dir are per home): a slow or
+# abandoned launch in one profile must never make another wait, then fall back to a signed-out browser.
+_locks: dict[str, threading.Lock] = {}
+_locks_guard = threading.Lock()
# Extra flags on top of the launcher's base set: no hang monitor / popup blocking / repost prompts on an
# unattended browser driving the user's own sessions.
_REAL_PROFILE_EXTRA_FLAGS = ("--disable-hang-monitor", "--disable-popup-blocking", "--disable-prompt-on-repost")
@@ -56,6 +59,12 @@ def _snapshot_error(err: str) -> str:
return f"{_RP}{err}"
+def _home_lock() -> threading.Lock:
+ from hermes_constants import hermes_home_key
+ with _locks_guard:
+ return _locks.setdefault(hermes_home_key(), threading.Lock())
+
+
def real_profile_key() -> str:
"""Launcher key of the CURRENT profile's real-profile browser. Per home: one process serves many profiles,
so a consent-off profile must never stop another's browser, nor a consenting one reuse its cookie jar."""
@@ -120,7 +129,8 @@ def real_profile_cdp() -> tuple[Optional[str], Optional[str]]:
if using_lightpanda_engine():
return None, (_RP + "browser.engine is set to 'lightpanda', which cannot load a real Chromium profile. "
"Set browser.engine to 'auto' to use real-profile browsing, or turn the toggle off.")
- if not _lock.acquire(timeout=_LOCK_TIMEOUT_S):
+ lock = _home_lock()
+ if not lock.acquire(timeout=_LOCK_TIMEOUT_S):
return None, (_RP + "the real-profile browser is already being prepared by another call that has not "
"finished. Retry after that call completes, or restart Hermes if it was abandoned.")
try:
@@ -135,4 +145,4 @@ def real_profile_cdp() -> tuple[Optional[str], Optional[str]]:
return None, unsupported
return _launch(browser_name)
finally:
- _lock.release()
+ lock.release()