From 5abba578ca4e29add44345411d172e830ec93403 Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Sat, 24 Oct 2026 22:54:28 +0530 Subject: [PATCH] test(browser): restore the end-to-end SIGKILL test for the harness daemon; fix a stale example Gate (quality): the port onto main's browser rewrite dropped test_a_sigkilled_owner_takes_its_harness_daemon_with_it with the agent-browser and exit-path tests, though it uses only browser_use_cli surface that is still here. It is the only end-to-end proof through the real supervisor that a kill -9'd Hermes takes its harness daemon along; restored unchanged (fails when _own_harness_daemon stops handing the group to the supervisor). The process_identity comment's example of a child something else spawns named the agent-browser daemon's Chromium, which nothing owns any more; it names the browser-harness daemon. --- hermes_cli/process_identity.py | 2 +- .../test_browser_use_cli_owned_daemon.py | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.py index 36ac575ddd..d020628fcb 100644 --- a/hermes_cli/process_identity.py +++ b/hermes_cli/process_identity.py @@ -817,7 +817,7 @@ def attach_self_to_kill_on_close_job() -> bool: # # ``spawn_owned`` is THE way to start a long-lived child that must die with this process: the child # leads its own process group, that group is handed to the supervisor, and the child is recorded in -# the spawn ledger. Children something else spawns (the MCP SDK, a daemon's own Chromium) hand their +# the spawn ledger. Children something else spawns (the MCP SDK, the browser-harness daemon) hand their # group to ``own_process_groups`` where we learn the pgid; a descendant that leaves the group on # purpose is registered by its own pgid the same way. Stopping an owned child yourself ends with # ``release_process_groups``. ``scripts/check_detached_spawns.py`` flags new detached spawns diff --git a/tests/tools/test_browser_use_cli_owned_daemon.py b/tests/tools/test_browser_use_cli_owned_daemon.py index f525c72c7f..86404ade93 100644 --- a/tests/tools/test_browser_use_cli_owned_daemon.py +++ b/tests/tools/test_browser_use_cli_owned_daemon.py @@ -138,6 +138,48 @@ def _shared_runtime(tmp_path) -> tuple[Path, dict]: return runtime, {"BH_RUNTIME_DIR": str(runtime), "BH_RUNTIME_DIR_SHARED": "1"} +@pytest.mark.live_system_guard_bypass +def test_a_sigkilled_owner_takes_its_harness_daemon_with_it(tmp_path): + """End to end through the real supervisor: the owner registers its detached daemon, then dies + by SIGKILL (no Python cleanup runs), and the daemon must go with it.""" + pkg = tmp_path / "pkgroot" / "browser_harness" + pkg.mkdir(parents=True) + (pkg / "__init__.py").write_text("", encoding="utf-8") + (pkg / "daemon.py").write_text("import time\ntime.sleep(300)\n", encoding="utf-8") + runtime = tmp_path / "runtime" + runtime.mkdir() + owner_src = ( + "import json, os, subprocess, sys, time\n" + "from tools import browser_use_cli\n" + f"d = subprocess.Popen([sys.executable, '-m', 'browser_harness.daemon'], cwd={str(tmp_path / 'pkgroot')!r}," + " stdin=subprocess.DEVNULL, start_new_session=True, env=browser_use_cli._base_subprocess_env())\n" + f"open({str(runtime / 'bu.pid')!r}, 'w', encoding='utf-8').write(json.dumps({{'pid': d.pid}}))\n" # windows-footgun: ok — a write + f"browser_use_cli._own_harness_daemon({{'BH_RUNTIME_DIR': {str(runtime)!r}}})\n" + "print(d.pid, flush=True)\n" + "time.sleep(300)\n" + ) + env = dict(os.environ, HERMES_HOME=str(tmp_path / "home"), PYTHONPATH=str(Path(__file__).resolve().parents[2])) + owner = subprocess.Popen([sys.executable, "-c", owner_src], env=env, stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, text=True) + daemon_pid = None + try: + daemon_pid = int(owner.stdout.readline()) + owner.kill() + owner.wait(timeout=10) + wait_until(lambda: not _group_alive(daemon_pid), timeout=15, + what="the harness daemon to die with the kill -9'd Hermes process that drove it") + finally: + if daemon_pid is not None: + try: + os.killpg(daemon_pid, 9) # windows-footgun: ok — posix-marked test + except (ProcessLookupError, PermissionError): + pass + if owner.poll() is None: + owner.kill() + owner.wait(timeout=10) + process_identity._supervised_pgids.clear() + + def test_a_daemon_another_live_hermes_started_on_the_same_name_is_never_registered(tmp_path, registered): """Two Hermes processes on the default BU_NAME read the same bu-default.pid. The daemon belongs to the one whose CLI started it: the other must not hand it to its supervisor, or that Hermes