Skip to content

The agent that grows with you

fix(browser): reclaim throwaway profiles left by a process that died mid-launch

Teknium0d6ada107efe
diff --git a/tests/tools/test_browser_launcher.py b/tests/tools/test_browser_launcher.py
index 287b062c8b8..ed841a658a1 100644
--- a/tests/tools/test_browser_launcher.py
+++ b/tests/tools/test_browser_launcher.py
@@ -358,6 +358,34 @@ def test_a_crashed_launchers_shared_browser_is_reaped_only_after_its_last_holder
_kill_fake(pid, fake_chromium)
+def test_a_throwaway_profile_left_by_a_process_that_died_mid_launch_is_reclaimed():
+ """A process that dies between creating its throwaway profile and writing the launch record (a first
+ launch can wait 120 s) leaves no record behind. The next reap reclaims the dir by its owner's pid and
+ start time; the dir of a live process is never touched."""
+ from hermes_constants import hermes_home_key
+ code = ("import sys\n"
+ "from tools.browser_launcher import _default_user_data_dir, _prepare_dir\n"
+ "udd = _default_user_data_dir('browser-exec-default', sys.argv[1])\n"
+ "_prepare_dir(udd, True)\n"
+ "print(udd, flush=True)\n"
+ "if sys.argv[2] == 'hold': sys.stdin.readline()\n")
+ run = dict(cwd=Path(__file__).resolve().parents[2], env=dict(os.environ), text=True)
+ died = Path(subprocess.run([sys.executable, "-c", code, hermes_home_key(), "die"], capture_output=True,
+ check=True, **run).stdout.strip())
+ alive = subprocess.Popen([sys.executable, "-c", code, hermes_home_key(), "hold"], stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, **run)
+ try:
+ held = Path(alive.stdout.readline().strip())
+ assert died.is_dir() and held.is_dir() and died.parent == held.parent
+
+ launcher.reap_orphaned_browsers()
+ assert not died.exists()
+ assert held.is_dir()
+ finally:
+ alive.communicate("\n", timeout=30)
+
+
@pytest.mark.parametrize("headed,display,expect_headless", [
(False, ":1", True),
(True, ":1", False),
diff --git a/tools/browser_launcher.py b/tools/browser_launcher.py
index 412b7dc1aab..d77a341e7af 100644
--- a/tools/browser_launcher.py
+++ b/tools/browser_launcher.py
@@ -19,6 +19,7 @@ import hashlib
import json
import logging
import os
+import re
import shutil
import stat
import subprocess
@@ -176,9 +177,13 @@ def chromium_ready() -> tuple[bool, str]:
def _default_user_data_dir(key: str, home_key: str) -> str:
"""Per process too: two Hermes processes on one profile use the same keys (``browser-exec-default``,
- named sessions), and each launch wipes its throwaway dir first."""
+ named sessions), and each launch wipes its throwaway dir first. The name carries the owner's pid and
+ start time: a process that dies before its launch record exists leaves the dir to the reaper
+ (:func:`_reclaim_throwaway_dirs`)."""
digest = hashlib.sha256(f"{home_key}\0{key}".encode()).hexdigest()[:16]
- return str(_ephemeral_root(home_key) / f"{digest}-{os.getpid()}")
+ pid = os.getpid()
+ started = _start_time(pid)
+ return str(_ephemeral_root(home_key) / (f"{digest}-{pid}-{started}" if started is not None else f"{digest}-{pid}"))
def _launch_timeout(timeout: Optional[float]) -> float:
@@ -752,6 +757,31 @@ def _reap_home(home: str) -> int:
return reaped
+_THROWAWAY_DIR = re.compile(r"^[0-9a-f]{16}-(\d+)(?:-(\d+))?$") # _default_user_data_dir's names
+
+
+def _reclaim_throwaway_dirs(home: str) -> None:
+ """Remove ``home``'s throwaway profiles whose owning process is gone (pid and start time in the name): one
+ that died before writing its launch record left nothing else that would. The dir of a live owner is never
+ touched; a name without a start time is reclaimed only once its pid is gone."""
+ root = _ephemeral_root(home)
+ if not os.path.lexists(root) or not _is_private_dir(root):
+ return
+ from tools.browser_launcher_holders import process_alive
+ try:
+ entries = list(root.iterdir())
+ except OSError as exc:
+ logger.debug("browser: throwaway profiles unavailable in %s: %s", root, exc)
+ return
+ for entry in entries:
+ match = _THROWAWAY_DIR.match(entry.name)
+ if match is None or int(match.group(1)) == os.getpid():
+ continue
+ if not process_alive(int(match.group(1)), int(match.group(2)) if match.group(2) else None):
+ shutil.rmtree(entry, ignore_errors=True) # a symlink is refused, never followed
+ logger.info("browser: removed the throwaway profile %s of a Hermes process that is gone", entry)
+
+
def _served_home_keys(homes) -> set[str]:
"""The launch profile, every profile this process launched a browser for, and ``homes``."""
from hermes_constants import get_process_hermes_home, hermes_home_key
@@ -766,5 +796,9 @@ def reap_orphaned_browsers(homes=()) -> int:
"""Stop Chromium processes whose owning Hermes process is gone, in every profile this process serves
(``_served_home_keys``; the janitor and atexit run outside any profile scope, so each home's records
are read by path); returns the count. A pid is signalled only after its start time AND its
- ``--user-data-dir`` argv token match the record."""
- return sum(_reap_home(home) for home in sorted(_served_home_keys(homes)))
+ ``--user-data-dir`` argv token match the record. Throwaway profiles of owners that are gone go too."""
+ reaped = 0
+ for home in sorted(_served_home_keys(homes)):
+ reaped += _reap_home(home)
+ _reclaim_throwaway_dirs(home)
+ return reaped