Skip to content

The agent that grows with you

fix(browser): bound real-profile resolver lock wait

Brooklyn Nicholson4b8a43719b2a
diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py
index cd8b6aa6311..1b6fd5ad171 100644
--- a/tests/tools/test_browser_real_profile.py
+++ b/tests/tools/test_browser_real_profile.py
@@ -189,6 +189,40 @@ 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."""
+ import threading
+
+ import tools.browser_tool as bt
+
+ self._reset()
+ monkeypatch.setattr(bt, "_REAL_PROFILE_CDP_LOCK_TIMEOUT_S", 0.05, raising=False)
+ result = {}
+ started = threading.Event()
+
+ def resolve():
+ started.set()
+ result.setdefault("value", bt_real_profile._real_profile_cdp())
+
+ with patch.object(bt_cloud, "_use_real_profile", return_value=True), \
+ patch.object(bt_lightpanda_fallback, "_using_lightpanda_engine", return_value=False), \
+ patch("hermes_cli.browser_connect.detect_default_chromium", return_value=None):
+ bt._real_profile_cdp_lock.acquire()
+ worker = threading.Thread(target=resolve, daemon=True)
+ try:
+ worker.start()
+ assert started.wait(timeout=2.0), "resolver worker was not scheduled"
+ worker.join(timeout=2.0)
+ stalled = worker.is_alive()
+ finally:
+ bt._real_profile_cdp_lock.release()
+ worker.join(timeout=2.0)
+
+ assert not stalled, "real-profile resolver waited indefinitely on a stale holder"
+ cdp, err = result["value"]
+ assert cdp is None
+ assert err and "already being prepared" in err
+
def test_launch_is_headless_and_agent_browser_attaches(self, tmp_path):
"""Real-profile browsing runs headless (no focus-stealing window).
diff --git a/tools/browser_tool.py b/tools/browser_tool.py
index f68a62d5c0f..1e7ca10c963 100644
--- a/tools/browser_tool.py
+++ b/tools/browser_tool.py
@@ -265,6 +265,9 @@ from tools import browser_tool_lightpanda_fallback as _lp
# Single shared real-profile copy-browser session: concurrent tasks reuse it
# instead of each launching a rival Chromium on the same copied user-data-dir.
_REAL_PROFILE_SESSION = "hermes-real-profile"
+# Keep lock contention well inside the agent's 420-second outer tool deadline.
+# The holder may be an abandoned daemon worker that Python cannot terminate.
+_REAL_PROFILE_CDP_LOCK_TIMEOUT_S = 30.0
_real_profile_cdp_lock = threading.Lock()
_real_profile_cdp_cache: dict = {}
_real_profile_chrome_procs: list = [] # Popen handles of directly-launched real browsers
diff --git a/tools/browser_tool_real_profile.py b/tools/browser_tool_real_profile.py
index 67faf51d0c1..20fb5fade72 100644
--- a/tools/browser_tool_real_profile.py
+++ b/tools/browser_tool_real_profile.py
@@ -280,7 +280,15 @@ def _real_profile_cdp() -> tuple:
from hermes_cli.browser_connect import (chromium_executable, detect_default_chromium,
real_profile_copy_dir, snapshot_real_profile)
- with _bt._real_profile_cdp_lock:
+ if not _bt._real_profile_cdp_lock.acquire(
+ timeout=_bt._REAL_PROFILE_CDP_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:
cached = _bt._real_profile_cdp_cache.get("cdp")
if cached and _cdp_http_ready(cached):
# Re-claim the shared daemon's socket dir so the orphan reaper's idle clock sees
@@ -332,3 +340,5 @@ def _real_profile_cdp() -> tuple:
_bt._real_profile_cdp_cache["cdp"] = cdp
_bt.logger.info("real-profile browser ready for %s at %s (%s)", browser, cdp, copy_dir)
return cdp, None
+ finally:
+ _bt._real_profile_cdp_lock.release()