The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(whatsapp): the Node bridge dies with the gateway that started it (orphans after kill -9: 1 -> 0)
kshitijk4poor73804ca2f92b
diff --git a/plugins/platforms/whatsapp/adapter.py b/plugins/platforms/whatsapp/adapter.pyindex fe890b0879e..1756fccb031 100644--- a/plugins/platforms/whatsapp/adapter.py+++ b/plugins/platforms/whatsapp/adapter.py@@ -18,7 +18,8 @@ from gateway.platforms._shared import (apply_yaml_bridge as _apply_yaml_bridge, extra_or_secret as _extra_or_secret, get_scoped_secret, send_error)from gateway.platforms.send_targets import StandaloneSendPolicy, regex_stripped_parser-from hermes_cli._subprocess_compat import windows_detach_popen_kwargs+from hermes_cli._subprocess_compat import windows_detach_flags+from hermes_cli.process_identity import release_process_groups, spawn_ownedfrom hermes_constants import (find_node_executable, get_hermes_dir, with_hermes_node_path)_IS_WINDOWS = platform.system() == "Windows"@@ -246,6 +247,14 @@ def _write_bridge_pidfile(session_path: Path, pid: int, port: Optional[int] = No(session_path / "bridge.pid").write_text(text, encoding="utf-8")+def _spawn_bridge_process(argv: list, log_fh, env: dict) -> subprocess.Popen:+ """Start the Node bridge as an owned child. It leads its own session (so a container stop+ reaches it directly, #127047) and dies with this process however this process dies: a SIGKILLed+ or OOM-killed gateway no longer leaves its bridge serving the port and the WhatsApp session."""+ return spawn_owned(argv, purpose="whatsapp-bridge", stdin=subprocess.DEVNULL, stdout=log_fh,+ stderr=log_fh, env=env, creationflags=windows_detach_flags())++def _terminate_bridge_process(proc, *, force: bool = False) -> None:"""Terminate the bridge process using process-tree semantics where possible."""action = "kill" if force else "terminate"@@ -758,9 +767,9 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):node = find_node_executable("node")if node is None:raise RuntimeError("Node.js is no longer available; run `hermes pm install`")- self._bridge_process = subprocess.Popen( # noqa: ASYNC220 -- persistent bridge; callers use the Popen object (pid, poll)+ self._bridge_process = _spawn_bridge_process([node, str(bridge_path), "--port", str(self._bridge_port), "--session", str(self._session_path),- "--mode", _wenv("WHATSAPP_MODE", "self-chat")], stdin=subprocess.DEVNULL, stdout=bridge_log_fh, stderr=bridge_log_fh, env=self._bridge_env(), **windows_detach_popen_kwargs())+ "--mode", _wenv("WHATSAPP_MODE", "self-chat")], bridge_log_fh, self._bridge_env())_write_bridge_pidfile(self._session_path, self._bridge_process.pid, self._bridge_port)if not await self._wait_for_bridge():return False@@ -853,6 +862,10 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):await asyncio.to_thread(self._bridge_process.wait, _SIGTERM_GRACE_S)except Exception as e:print(f"[{self.name}] Error stopping bridge: {e}")+ if not _IS_WINDOWS and self._bridge_process.poll() is not None:+ # Stopped by us; a bridge we failed to stop stays covered by the owner-death supervisor.+ # (POSIX only: Windows has no supervised group, and its stop path stays as on main.)+ release_process_groups([self._bridge_process.pid])if self._bridge_process or not getattr(self, "_runtime_status_platform_key", ""):_unlink_quietly(self._session_path / "bridge.pid")await cancel_task(self._poll_task)diff --git a/tests/gateway/test_whatsapp_connect.py b/tests/gateway/test_whatsapp_connect.pyindex cb39dae1f32..7c88ff33b29 100644--- a/tests/gateway/test_whatsapp_connect.py+++ b/tests/gateway/test_whatsapp_connect.py@@ -76,7 +76,7 @@ def _connect_patches(mock_proc, mock_fh):patch.object(Path, "exists", return_value=True),patch.object(Path, "mkdir", return_value=None),patch("subprocess.run", return_value=MagicMock(returncode=0)),- patch("subprocess.Popen", return_value=mock_proc),+ patch("plugins.platforms.whatsapp.adapter.spawn_owned", return_value=mock_proc),patch("builtins.open", return_value=mock_fh),patch("plugins.platforms.whatsapp.adapter.asyncio.sleep", new_callable=AsyncMock),patch("plugins.platforms.whatsapp.adapter.asyncio.create_task"),diff --git a/tests/gateway/test_whatsapp_stale_bridge.py b/tests/gateway/test_whatsapp_stale_bridge.pyindex 778977defcc..fec98ab3a2e 100644--- a/tests/gateway/test_whatsapp_stale_bridge.py+++ b/tests/gateway/test_whatsapp_stale_bridge.py@@ -151,7 +151,7 @@ class TestStaleBridgeHandshake:patch("plugins.platforms.whatsapp.adapter.asyncio.sleep", new_callable=AsyncMock), \patch("plugins.platforms.whatsapp.adapter._kill_stale_bridge_by_pidfile"), \patch("plugins.platforms.whatsapp.adapter._kill_port_process"), \- patch("subprocess.Popen", return_value=mock_proc) as mock_popen, \+ patch("plugins.platforms.whatsapp.adapter.spawn_owned", return_value=mock_proc) as mock_popen, \patch.object(adapter, "_acquire_platform_lock", return_value=True, create=True):await adapter.connect()@@ -200,7 +200,7 @@ class TestStaleBridgeHandshake:patch("plugins.platforms.whatsapp.adapter._IS_WINDOWS", windows),patch("plugins.platforms.whatsapp.adapter._listener_pids_on_port", return_value=[] if windows else [4242]),patch("plugins.platforms.whatsapp.adapter._windows_listener_pids", return_value=[4242] if windows else []),- patch("subprocess.Popen", return_value=MagicMock()) as mock_popen,+ patch("plugins.platforms.whatsapp.adapter.spawn_owned", return_value=MagicMock()) as mock_popen,patch.object(adapter, "_acquire_platform_lock", return_value=True, create=True),):assert await adapter.connect() is (fatal is None)@@ -237,7 +237,7 @@ class TestDepRefreshStamp:patch("plugins.platforms.whatsapp.adapter._kill_stale_bridge_by_pidfile"), \patch("plugins.platforms.whatsapp.adapter._kill_port_process"), \patch("subprocess.run") as mock_run, \- patch("subprocess.Popen", return_value=mock_proc), \+ patch("plugins.platforms.whatsapp.adapter.spawn_owned", return_value=mock_proc), \patch.object(adapter, "_acquire_platform_lock", return_value=True, create=True):await adapter.connect()@@ -263,7 +263,7 @@ class TestCacheDirEnvPassthrough:patch("plugins.platforms.whatsapp.adapter.asyncio.sleep", new_callable=AsyncMock), \patch("plugins.platforms.whatsapp.adapter._kill_stale_bridge_by_pidfile"), \patch("plugins.platforms.whatsapp.adapter._kill_port_process"), \- patch("subprocess.Popen", return_value=mock_proc) as mock_popen, \+ patch("plugins.platforms.whatsapp.adapter.spawn_owned", return_value=mock_proc) as mock_popen, \patch.object(adapter, "_acquire_platform_lock", return_value=True, create=True):await adapter.connect()diff --git a/tests/hermes_cli/test_owned_agent_subprocs.py b/tests/hermes_cli/test_owned_agent_subprocs.pynew file mode 100644index 00000000000..190ae26cca2--- /dev/null+++ b/tests/hermes_cli/test_owned_agent_subprocs.py@@ -0,0 +1,148 @@+"""Long-lived agent/plugin subprocess clients die with the Hermes process that started them.++Each kill -9 case drives the REAL production spawn path in two disposable owner processes, SIGKILLs+one (no atexit, no finally: nothing of ours runs) and requires that owner's child tree to be gone+while the live owner's tree is untouched. On origin/main every one of these trees survived kill -9+forever: plain Popen children reparent to init, and detached ones lead their own session.+"""++import json+import os+import subprocess+import sys+from pathlib import Path++import psutil+import pytest++from tests.waiting import wait_until++pytestmark = pytest.mark.platforms("posix")++REPO = Path(__file__).resolve().parents[2]++# The child each production path starts: records itself, starts a grandchild that stays in its+# process group (the way a node bridge's helpers or a dev server's workers do), then idles.+_CHILD = r'''+import json, os, subprocess, sys, time+from pathlib import Path+import psutil+out = Path(sys.argv[1]); role = sys.argv[2]+p = psutil.Process()+(out / (role + '.tmp')).write_text(json.dumps({'pid': p.pid, 'created': p.create_time()}))+(out / (role + '.tmp')).replace(out / (role + '.json'))+if role == 'child':+ subprocess.Popen([sys.executable, __file__, str(out), 'grandchild'], stdin=subprocess.DEVNULL)+time.sleep(120)+'''++# Owner bodies: ``child`` is the argv starting the child, ``child_cmd`` the same as a shell string,+# ``out`` the receipt directory. Each starts the child through the production path; the owner idles.+_OWNERS = {+ "whatsapp-bridge": (+ "from plugins.platforms.whatsapp.adapter import _spawn_bridge_process\n"+ "_spawn_bridge_process(child, open(out / 'bridge.log', 'a', encoding='utf-8'), dict(os.environ))\n"+ ),+}+++def _owner_source(site: str) -> str:+ return (+ "import os, shlex, sys, threading, time\n"+ "from pathlib import Path\n"+ "out = Path(sys.argv[1]); child_script = sys.argv[2]\n"+ "child = [sys.executable, child_script, str(out), 'child']\n"+ "child_cmd = ' '.join(shlex.quote(a) for a in child)\n"+ + _OWNERS[site]+ + "time.sleep(300)\n"+ )+++def _read(path: Path) -> dict:+ wait_until(path.exists, timeout=30, what=f"receipt {path.name}")+ return json.loads(path.read_text(encoding="utf-8-sig"))+++def _alive(identity: dict) -> bool:+ """Zombie-aware: a killed orphan lingers as a zombie until init reaps it."""+ try:+ proc = psutil.Process(identity["pid"])+ return proc.create_time() == identity["created"] and proc.status() != psutil.STATUS_ZOMBIE+ except psutil.NoSuchProcess:+ return False+++def _start_owner(tmp_path: Path, name: str, site: str) -> tuple[subprocess.Popen, Path]:+ out = tmp_path / name+ out.mkdir()+ child_script = tmp_path / "child.py"+ child_script.write_text(_CHILD, encoding="utf-8")+ home = tmp_path / f"home-{name}"+ home.mkdir()+ env = dict(os.environ, HERMES_HOME=str(home / ".hermes"), HOME=str(home), HERMES_REAL_HOME=str(home),+ PYTHONPATH=str(REPO))+ with (tmp_path / f"{name}.err").open("w", encoding="utf-8") as err:+ owner = subprocess.Popen([sys.executable, "-c", _owner_source(site), str(out), str(child_script)],+ env=env, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=err)+ return owner, out+++# The children reparent to init once their owner dies, so cleaning them up leaves this test's+# subtree; real signal delivery to a real orphan is the point of the test.[email protected]_system_guard_bypass[email protected]("site", sorted(_OWNERS))+def test_a_sigkilled_owner_takes_its_child_tree_and_spares_a_live_owners(tmp_path, site):+ owners: dict = {}+ ids: dict = {}+ try:+ for name in ("dead", "live"):+ owners[name], out = _start_owner(tmp_path, name, site)+ ids[name] = {role: _read(out / f"{role}.json") for role in ("child", "grandchild")}+ assert all(_alive(i) for tree in ids.values() for i in tree.values())++ owners["dead"].kill() # SIGKILL: no atexit, no finally, nothing of ours runs+ owners["dead"].wait(timeout=10)++ wait_until(lambda: not any(_alive(i) for i in ids["dead"].values()), timeout=15,+ what=f"the {site} tree to die with its kill -9'd owner")+ assert all(_alive(i) for i in ids["live"].values()), f"a live owner's {site} tree was killed"+ finally:+ for tree in ids.values():+ for identity in tree.values():+ if _alive(identity):+ psutil.Process(identity["pid"]).kill()+ for owner in owners.values():+ if owner.poll() is None:+ owner.kill()+ owner.wait(timeout=10)+++def _sleeper() -> subprocess.Popen:+ return subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"], stdin=subprocess.DEVNULL,+ start_new_session=True)+++def _reap(proc: subprocess.Popen) -> None:+ if proc.poll() is None:+ proc.kill()+ proc.wait(timeout=10)+++async def test_whatsapp_disconnect_releases_the_bridge_it_stopped(monkeypatch):+ from plugins.platforms.whatsapp import adapter as wa++ released = []+ monkeypatch.setattr(wa, "release_process_groups", lambda pgids: released.append(list(pgids)))+ bridge = _sleeper()+ from tests.gateway.test_whatsapp_connect import _make_adapter++ adapter = _make_adapter()+ adapter._bridge_process, adapter._poll_task = bridge, None+ adapter._running, adapter._session_lock_identity = True, None+ try:+ await adapter.disconnect()+ assert bridge.poll() is not None, "disconnect() left the bridge running"+ assert released == [[bridge.pid]]+ finally:+ _reap(bridge)