Skip to content

The agent that grows with you

fix(browser): the exit path stops only the harness daemons this process started

kshitijk4poor36aa4dcc4031
diff --git a/tests/tools/test_browser_use_cli_owned_daemon.py b/tests/tools/test_browser_use_cli_owned_daemon.py
index be7d2f02554..0db28f82eef 100644
--- a/tests/tools/test_browser_use_cli_owned_daemon.py
+++ b/tests/tools/test_browser_use_cli_owned_daemon.py
@@ -109,6 +109,31 @@ def test_stopping_the_daemons_releases_their_groups(monkeypatch, registered):
assert browser_use_cli._owned_daemon_groups == {}
+def test_the_exit_path_stops_only_the_daemons_this_process_owns(monkeypatch, registered):
+ ours = browser_use_cli.daemon_identity({"BU_NAME": "ours"})
+ theirs = browser_use_cli.daemon_identity({"BU_NAME": "theirs"}) # another live Hermes started it
+ browser_use_cli._owned_daemon_groups.update({ours: 4242})
+ monkeypatch.setattr(browser_use_cli, "_driven_daemons", {ours: "home", theirs: "home"})
+ monkeypatch.setattr(browser_use_cli, "_find_cli", lambda: ["browser-harness"])
+ monkeypatch.setattr(browser_use_cli, "_base_subprocess_env", dict)
+ reloaded = []
+ monkeypatch.setattr(browser_use_cli, "_run_cli_killing_process_group",
+ lambda cmd, code, env, timeout: reloaded.append(env["BU_NAME"]))
+
+ from tools import browser_tool_lifecycle
+ from tools.browser_tool_origin import origin
+ monkeypatch.setattr(origin, "_cleanup_done", False)
+ monkeypatch.setattr(origin, "_active_sessions", {})
+ for step in ("_stop_all_lightpanda", "_reap_orphaned_browser_sessions"):
+ monkeypatch.setattr(browser_tool_lifecycle, step, lambda: None)
+ monkeypatch.setattr(browser_tool_lifecycle._real_profile, "_terminate_real_profile_chrome", lambda: None)
+
+ browser_tool_lifecycle._emergency_cleanup_all_sessions() # the atexit handler
+
+ assert reloaded == ["ours"]
+ assert browser_use_cli._driven_daemons == {theirs: "home"}
+
+
# The daemon reparents to init once its owner dies, so cleanup leaves this test's subtree.
@pytest.mark.live_system_guard_bypass
def test_a_sigkilled_owner_takes_its_harness_daemon_with_it(tmp_path):
diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.py
index 1f42730a52a..9ddf7eb6d1c 100644
--- a/tools/browser_use_cli.py
+++ b/tools/browser_use_cli.py
@@ -635,6 +635,18 @@ def stop_harness_daemons(home_key: Optional[str] = None) -> None:
_reload_daemons(daemons)
+def stop_owned_harness_daemons() -> None:
+ """Stop only the harness daemons this process owns (``_owned_daemon_groups``: started by its own
+ commands). The exit path uses this: a daemon another live Hermes started on the same name was
+ driven here too, but it is that Hermes's to stop."""
+ with _owned_daemon_groups_lock:
+ owned = sorted(_owned_daemon_groups)
+ with _driven_daemons_lock:
+ for key in owned:
+ _driven_daemons.pop(key, None)
+ _reload_daemons(owned)
+
+
def _reload_daemons(daemons) -> None: # each ``daemon_identity`` daemon, with its own dirs
cmd = _find_cli() if daemons else None
if not cmd: