The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(browser): take over a crashed owner's record when attaching to its browser
Teknium2cb6c996e252
diff --git a/tests/tools/test_browser_orphan_reaper.py b/tests/tools/test_browser_orphan_reaper.pyindex 6b9474459b1..2c08ece5cc7 100644--- a/tests/tools/test_browser_orphan_reaper.py+++ b/tests/tools/test_browser_orphan_reaper.py@@ -212,6 +212,30 @@ class TestLeakedBrowserOfThisProcess:assert records.terminated == []+def test_browser_attached_after_its_owner_crashed_is_never_reaped_while_in_use(records, tmp_path, monkeypatch):+ """The next process ATTACHES to a crashed owner's persistent-profile Chromium (owned=False). The record+ still names the dead owner; this process takes it over, so no reaper kills the browser it handed out."""+ udd = str(tmp_path / "persistent-profile")+ monkeypatch.setattr(launcher, "_pid_alive", _alive(4242)) # the browser survived, its owner did not+ monkeypatch.setattr(launcher, "_start_time", lambda pid: 777)+ monkeypatch.setattr(launcher, "_cmdline_names_dir", lambda pid, udd: True)+ monkeypatch.setattr(launcher, "running_instance", lambda d: (41234, "/devtools/browser/abc", 4242) if d == udd else None)+ from hermes_constants import hermes_home_key+ crashed = launcher.LaunchedBrowser(key=f"profile:{udd}", pid=4242, start_time=777, cdp_url="", http_url="",+ user_data_dir=udd, owned=True, ephemeral=False, home_key=hermes_home_key())+ record = launcher._record_path(crashed) # where the crashed owner left it+ record.write_text(json.dumps({"owner_pid": _DEAD_OWNER, "owner_start_time": None, "pid": 4242, "start_time": 777,+ "user_data_dir": udd, "home_key": crashed.home_key, "ephemeral": False}))++ attached, err = launcher.launch_chromium(f"profile:{udd}", executable="/opt/chrome", user_data_dir=udd,+ ephemeral=False)+ assert err is None and attached.owned is False++ assert launcher.reap_orphaned_browsers() == 0+ assert records.terminated == []+ assert json.loads(record.read_text())["owner_pid"] == os.getpid() # other processes see a live owner++class TestPrivatePerProfileRecords:"""Launcher state lives in a private per-profile dir: a records dir that is not a real directory of thisuser closed to other writers is never read, and the reaper deletes nothing outside the profile'sdiff --git a/tools/browser_launcher.py b/tools/browser_launcher.pyindex 156e9268637..0552d17e644 100644--- a/tools/browser_launcher.py+++ b/tools/browser_launcher.py@@ -362,8 +362,10 @@ def _attach_running(key: str, user_data_dir: str, ephemeral: bool, home_key: strreturn Noneport, path, pid = runninglogger.info("browser: attaching to the Chromium already running on %s (pid %s)", user_data_dir, pid)- return _make_browser(key, port, path, pid=pid, start_time=_start_time(pid), user_data_dir=user_data_dir,- owned=False, ephemeral=False, home_key=home_key)+ browser = _make_browser(key, port, path, pid=pid, start_time=_start_time(pid), user_data_dir=user_data_dir,+ owned=False, ephemeral=False, home_key=home_key)+ _adopt_record(browser)+ return browserdef launch_chromium(key: str, *, executable: Optional[str] = None, user_data_dir: Optional[str] = None,@@ -632,9 +634,20 @@ def _is_recorded_browser(record: dict, home: str) -> bool:return True-def _tracked_here(record: dict) -> bool:+def _tracked_browser(record: dict) -> Optional[LaunchedBrowser]:with _lock:- return any(b.pid == record.get("pid") for b in _browsers.values())+ return next((b for b in _browsers.values() if b.pid == record.get("pid")), None)+++def _adopt_record(browser: LaunchedBrowser) -> None:+ """A crashed owner's record still names the browser this process now uses (attached after the crash):+ take the record over, or every reaper (this process's included) would kill it as an orphan."""+ path = _record_path(browser)+ if path is None or not path.exists():+ return+ record = _load_record(path)+ if record is not None and record.get("start_time") == browser.start_time and not _owner_alive(record):+ _write_record(browser)def _load_record(path: Path) -> Optional[dict]:@@ -649,15 +662,15 @@ def _load_record(path: Path) -> Optional[dict]:return None-def _record_in_use(record: dict) -> bool:- if record.get("owner_pid") == os.getpid():- return _tracked_here(record)- return _owner_alive(record)--def _reap_record(path: Path, home: str) -> bool:record = _load_record(path)- if record is None or _record_in_use(record):+ if record is None:+ return False+ tracked = _tracked_browser(record)+ if tracked is not None: # in use here, whoever launched it+ _adopt_record(tracked)+ return False+ if record.get("owner_pid") != os.getpid() and _owner_alive(record):return Falsereaped = _is_recorded_browser(record, home)if reaped: