fix(browser): a shared daemon is owned only by the Hermes whose command started it
Two same-user Hermes processes can reach one daemon. Both read bu-default.pid on the default Browser Use name, and every Hermes shares the real-profile attach session. The identity checks (uid, module, session leader, socket-dir binding) prove what the daemon is, not who started it, so both Hermes registered its group. Killing either one then killed the browser the other was still using. The real-profile attach's before/after pid-file diff had the same hole: two concurrent attaches both see the file change. Each agent-browser and browser-harness command now carries this process's spawn tag (spawn_env: pid plus create time). The daemon it forks inherits the tag, and _own_local_daemon / _own_harness_daemon register a daemon only when its tag names this process (process_identity.spawned_by_this_process; an unreadable environment fails closed). The pid-file diff is gone. Probed on macOS against agent-browser 0.26.0: the forked daemon carries the tag. New tests start a verified daemon tagged with another live process. Each fails with its check removed (the daemon gets registered) and passes with it. Reported by Greptile on #137057 (two threads).
This commit is contained in:
@@ -23,7 +23,8 @@ import time
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Dict, List, NamedTuple, Optional
|
||||
|
||||
from hermes_cli.process_identity import own_process_groups, register_child, release_process_group_once_empty
|
||||
from hermes_cli.process_identity import (
|
||||
own_process_groups, register_child, release_process_group_once_empty, spawn_env, spawned_by_this_process)
|
||||
from hermes_constants import get_hermes_home, hermes_home_key
|
||||
from tools.browser_harness_dirs import (daemon_env, daemon_identity, fit_default_socket_dir,
|
||||
namespace_harness_dirs)
|
||||
@@ -196,6 +197,8 @@ def _base_subprocess_env() -> dict:
|
||||
env["PATH"] = _floor_subprocess_path(env.get("PATH", ""))
|
||||
env.setdefault("ANONYMIZED_TELEMETRY", "false")
|
||||
namespace_harness_dirs(env, _served_profile_tag()) # a served profile's own harness runtime/tmp dirs
|
||||
# A daemon the CLI starts inherits this tag; _own_harness_daemon owns only one that names us.
|
||||
env.update(spawn_env("browser-harness-daemon"))
|
||||
return env
|
||||
|
||||
|
||||
@@ -686,6 +689,8 @@ def _own_harness_daemon(env: dict) -> None:
|
||||
if (os.getsid(pid) != pid or proc.uids().real != os.getuid() # windows-footgun: ok — posix-only, returned above
|
||||
or argv[argv.index("-m") + 1] != "browser_harness.daemon"):
|
||||
return
|
||||
if not spawned_by_this_process(pid):
|
||||
return # another Hermes on the same BU_NAME started it: that Hermes owns it
|
||||
except (OSError, ValueError, KeyError, TypeError, IndexError, psutil.Error) as exc:
|
||||
logger.debug("browser_exec: no harness daemon to supervise for %s: %s", name, exc)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user