From efc2b8e28d1b55b0cf075060e577969b0eb49ed2 Mon Sep 17 00:00:00 2001 From: John Paul Soliva Date: Tue, 15 Sep 2026 17:34:49 +0900 Subject: [PATCH] fix(update): verify the launchd job comes back on a fresh PID, follow wrapper plists, warn on --stop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adopted from the sibling PRs in this cluster, with thanks: - #99807 (@Chevron7Locked) and #101541 (@ivcdigital26): a kickstart that returns 0 is only "restart requested" — success now requires launchd to report a live PID other than the one that was stopped (the gateway's _wait_for_launchd_service_pid, 15 s), and a job that never comes back is reported with the manual `launchctl kickstart -k` command instead of a tick. - #89793 (@mettamyron) and #101541: a plist may wrap the backend in `/bin/sh -c …` without exec, so launchd's live PID is the shell and the stale backend is its child — ownership now follows the parent chain (bounded, cycle-safe). - #89793: `hermes dashboard --stop` on a launchd-owned backend now says a KeepAlive job restarts itself and gives the `launchctl bootout` command, instead of the misleading "restart it yourself" hint. The job scan runs for --stop too, on macOS only. --- hermes_cli/dashboard_procs.py | 57 ++++++--- hermes_cli/main_dashboard.py | 33 +++-- .../hermes_cli/test_update_stale_dashboard.py | 117 ++++++++++++++---- 3 files changed, 160 insertions(+), 47 deletions(-) diff --git a/hermes_cli/dashboard_procs.py b/hermes_cli/dashboard_procs.py index 88c7ba4651..70f6f003aa 100644 --- a/hermes_cli/dashboard_procs.py +++ b/hermes_cli/dashboard_procs.py @@ -345,22 +345,27 @@ def _kill_stale_dashboard_processes( # Snapshot systemd unit/cgroup and argv BEFORE killing (the cgroup dies with the process). pid_cgroup: dict[int, str | None] = {} pid_service: dict[int, str | None] = {} - pid_launchd: dict[int, tuple[str, str]] = {} + pid_launchd: dict[int, tuple[str, str, int | None]] = {} pid_cmdline: dict[int, list[str]] = {} pid_home: dict[int, str | None] = {} + # macOS: a backend supervised by a launchd job (LaunchAgent / LaunchDaemon) must come back + # through launchd, never as a detached argv respawn — the respawn holds the job's port, the + # job then fails every KeepAlive restart with "port already in use", and the running backend + # is left unsupervised. Snapshot the loaded jobs once, before the kill; ``--stop`` reads them + # too, so it can say that a KeepAlive job will undo the stop. + launchd_jobs = _dash._loaded_launchd_backend_jobs() if sys.platform != "win32" else [] + + def _launchd_owner(pid: int, cmdline: list[str] | None): + return _dash._launchd_job_owning_backend(pid, cmdline, launchd_jobs, ancestors=_process_ancestors(pid)) + if restart_managed and sys.platform != "win32": - # macOS: a backend supervised by a launchd job (LaunchAgent / LaunchDaemon) must come back - # through launchd, never as a detached argv respawn — the respawn holds the job's port, the - # job then fails every KeepAlive restart with "port already in use", and the running - # backend is left unsupervised. Snapshot the loaded jobs once, before the kill. - launchd_jobs = _dash._loaded_launchd_backend_jobs() for pid in pids: pid_cgroup[pid] = _dash._get_pid_cgroup_path(pid) pid_service[pid] = _dash._get_systemd_service_for_pid(pid) if pid_service[pid]: continue cmdline = _dash._dashboard_cmdline_for_pid(pid) - if launchd_jobs and (job := _dash._launchd_job_owning_backend(pid, cmdline, launchd_jobs)): + if launchd_jobs and (job := _launchd_owner(pid, cmdline)): pid_launchd[pid] = job elif cmdline: # Manual process: exact argv + HERMES_HOME for the respawn and its profile cap. @@ -374,6 +379,10 @@ def _kill_stale_dashboard_processes( not in already_restarted_units] if not pids: return _empty_result() + elif launchd_jobs: + for pid in pids: + if job := _launchd_owner(pid, _dash._dashboard_cmdline_for_pid(pid)): + pid_launchd[pid] = job print(f"\n⟲ Stopping {len(pids)} dashboard process(es) ({reason})") killed: list[int] = [] failed: list[tuple[int, str]] = [] @@ -387,7 +396,12 @@ def _kill_stale_dashboard_processes( killed, pid_service, pid_cgroup, pid_cmdline, pid_home, pid_launchd=pid_launchd) else: unrecovered = list(killed) - if killed: + # A stopped launchd job with KeepAlive restarts itself: say so instead of a misleading + # "restart it yourself" hint, and give the command that actually keeps it down. + for target in sorted({f"{d}/{l}" for p in killed if (j := pid_launchd.get(p)) for d, l, _ in (j,)}): + print(f" ⚠ PID(s) supervised by launchd job {target}: a KeepAlive job restarts itself.\n" + f" To keep it down: launchctl bootout {target}") + if any(p not in pid_launchd for p in killed): print(" Restart the dashboard when you're ready:\n hermes dashboard --port ") return {"matched": list(pids), "killed": list(killed), "failed": list(failed), "unrecovered": list(unrecovered)} @@ -396,7 +410,7 @@ def _kill_stale_dashboard_processes( def _restart_killed_backends( killed: list[int], pid_service: dict[int, str | None], pid_cgroup: dict[int, str | None], pid_cmdline: dict[int, list[str]], pid_home: dict[int, str | None], *, - pid_launchd: dict[int, tuple[str, str]] | None = None) -> list[int]: + pid_launchd: dict[int, tuple[str, str, int | None]] | None = None) -> list[int]: """Update path: restart systemd units, kickstart launchd jobs (macOS), respawn manual argv (detached, headless, logged to logs/dashboard-restart.log; one per profile, no ``--port 0``). Returns PIDs not brought back.""" @@ -422,17 +436,19 @@ def _restart_killed_backends( failed_restarts.append((svc_name, "systemctl restart returned non-zero")) unrecovered.append(pid) elif launchd_job: - # launchd owns the backend: the job brings it back (KeepAlive, or the kickstart below); - # an argv respawn would sit on the job's port and leave it failing forever. - domain, label = launchd_job + # launchd owns the backend: the job brings it back (KeepAlive, or the kickstart below), + # and success means launchd reports a fresh supervised PID — an argv respawn would sit + # on the job's port and leave it failing forever. + domain, label, old_pid = launchd_job target = f"{domain}/{label}" if target in seen_services: continue seen_services.add(target) - if _dash._try_kickstart_launchd_job(domain, label): + if _dash._restart_launchd_job(domain, label, old_pid): print(f" ✓ restarted launchd job {target}") else: - failed_restarts.append((target, f"launchctl kickstart failed; run: launchctl kickstart -k {target}")) + failed_restarts.append( + (target, f"launchd is not supervising a fresh process; run: launchctl kickstart -k {target}")) unrecovered.append(pid) elif pid in pid_cmdline: respawn_candidates.append((pid, pid_cmdline[pid], pid_home.get(pid))) @@ -540,6 +556,19 @@ def _flag_value(tokens: list[str], flag: str) -> str | None: return None +def _process_ancestors(pid: int, *, max_depth: int = 8) -> list[int]: + """Parent chain of *pid* (nearest first), stopping at init / a lookup failure / *max_depth*.""" + chain: list[int] = [] + current = pid + while len(chain) < max_depth: + parent = _process_ppid(current) + if parent is None or parent <= 1 or parent == pid or parent in chain: + break + chain.append(parent) + current = parent + return chain + + def _process_ppid(pid: int) -> int | None: """Best-effort parent pid; None on failure (always None on Windows: desktop tree-kill reaps).""" try: diff --git a/hermes_cli/main_dashboard.py b/hermes_cli/main_dashboard.py index 7c152ad78f..9e4589280d 100644 --- a/hermes_cli/main_dashboard.py +++ b/hermes_cli/main_dashboard.py @@ -270,23 +270,32 @@ def _loaded_launchd_backend_jobs( def _launchd_job_owning_backend( pid: int, cmdline: list[str] | None, jobs: list[tuple[str, str, list[str], int | None]], -) -> tuple[str, str] | None: - """``(domain, label)`` of the loaded launchd job that owns *pid*: launchd reports *pid* as the - job's live process, OR the process runs the job's exact ``ProgramArguments`` — a detached copy of - a supervised backend (an earlier respawn) holds the port the job needs, and respawning it again - would only re-create that conflict. None when no loaded job claims the process.""" + ancestors: "list[int] | tuple[int, ...]" = (), +) -> tuple[str, str, int | None] | None: + """``(domain, label, live_pid)`` of the loaded launchd job that owns *pid*: launchd reports *pid* + (or one of its *ancestors* — a plist may wrap the backend in ``/bin/sh -c …`` without ``exec``) + as the job's live process, OR the process runs the job's exact ``ProgramArguments`` — a detached + copy of a supervised backend (an earlier respawn) holds the port the job needs, and respawning it + again would only re-create that conflict. None when no loaded job claims the process.""" for domain, label, argv, live_pid in jobs: - if live_pid == pid or (cmdline is not None and list(cmdline) == argv): - return (domain, label) + if live_pid is not None and (live_pid == pid or live_pid in ancestors): + return (domain, label, live_pid) + if cmdline is not None and list(cmdline) == argv: + return (domain, label, live_pid) return None -def _try_kickstart_launchd_job(domain: str, label: str) -> bool: - """``launchctl kickstart /