Skip to content

/restart never respawns outside service managers: detached restart helper inherits _HERMES_GATEWAY=1 and self-refuses (#30719 regression) #44312

Closed as not planned

Description

@matthewod11-stack

Summary

Since 5cd6c17 (2026-05-23, #30719), hermes gateway restart refuses to run when _HERMES_GATEWAY=1 is in the environment (hermes_cli/gateway.py, restart subcommand). The gateway sets that marker process-wide at import (gateway/run.py:835 — os.environ["_HERMES_GATEWAY"] = "1"), and _launch_detached_restart_command() (gateway/run.py ~3981) spawns its successor helper via subprocess.Popen without scrubbing the env. The helper therefore inherits the marker, its hermes gateway restart exits 1 with "Refusing to restart the gateway from inside the gateway process", and the refusal is invisible (stdout/stderr → DEVNULL).

Net effect: on any platform where /restart takes the detached path — i.e. anywhere INVOCATION_ID (systemd) is unset and no container is detected — the in-chat /restart tears the gateway down, exits 0, and nothing respawns it.

Observed live (macOS / launchd)

macOS 2026-06-08, launchd agent generated by an older Hermes (KeepAlive {SuccessfulExit: false}):

  • /restart from Telegram → gateway.exit_clean in gateway-exit-diag.log → no successor gateway.start ever appears → gateway fully dark (no poller, no cron) until a manual launchctl kickstart -k.
  • The helper's refusal never surfaces anywhere (DEVNULL).

Note: the #30719 defense exists to stop agent-initiated restart loops, but it also kills the gateway's own restart mechanism — the helper is guaranteed to run only after the old process has exited (its while kill -0 $PID wait loop), so it is not the in-process case the defense targets.

Suggested fixes

  1. Scrub _HERMES_GATEWAY from the detached helper's child env in _launch_detached_restart_command() (all three spawn branches: setsid/bash/Windows watcher). The helper runs strictly post-exit, so the anti-loop defense shouldn't apply to it.
  2. Optionally: detect launchd as a service manager in the /restart handler (the comment there already says "systemd/launchd" but only INVOCATION_ID is checked). XPC_SERVICE_NAME is set by launchd (verified: XPC_SERVICE_NAME=ai.hermes.gateway in the agent's env) — routing macOS through the via_service path would use the exit-75 branch added in 5987b24 and avoid the fragile detached helper entirely.

Mitigation that works today

Current generate_launchd_plist() emits unconditional KeepAlive: true, which revives the clean exit — installs that refresh their plist (hermes gateway install) recover from /restart fine. But users who recover with raw launchctl kickstart never trigger plist regeneration, so older SuccessfulExit:false plists persist indefinitely — and the detached helper is silently dead code for everyone not under systemd/container either way.

Environment

  • Hermes 68a997fed (also present at current HEAD by code inspection)
  • macOS (darwin), Python 3.11.14, launchd LaunchAgent supervision

Activity

  1. added
    type/bugSomething isn't working
    P2Medium — degraded but workaround exists
    comp/gatewayGateway runner, session dispatch, delivery
    comp/cliCLI entry point, hermes_cli/, setup wizard
    on Jun 11, 2026
  2. liuhao1024 commented on Jun 11, 2026

    @liuhao1024

    This has already been fixed on main by commit cb2c13055 (fix(gateway): scrub _HERMES_GATEWAY from POSIX detached restart watcher too). All three spawn paths (Windows watcher, setsid, bash) now create a watcher_env copy with _HERMES_GATEWAY removed before passing it to subprocess.Popen.

  3. lifuyue commented on Jun 11, 2026

    @lifuyue

    I checked current main, and this specific _HERMES_GATEWAY inheritance regression appears fixed by #43969.

    gateway/run.py now scrubs _HERMES_GATEWAY from the detached restart watcher environment on both paths:

    • Windows watcher env
    • POSIX setsid / bash -lc watcher env

    I also verified the related regression coverage locally:

    scripts/run_tests.sh tests/gateway/test_restart_drain.py tests/gateway/test_restart_notification.py -- -q

    Result: 46 passed, 0 failed.

    The remaining macOS launchd service-manager routing concern looks related but separate; it appears to be tracked by #29180 and open PRs such as #43888.

  4. MuBeiGe commented on Jun 13, 2026

    @MuBeiGe

    Additional blocker on Windows: is_installed()=False → prompt_yes_no → EOFError

    Confirmed the Layer 1 root cause (_HERMES_GATEWAY env inheritance) on Windows 0.16.0. After stripping _HERMES_GATEWAY from the watcher env, there's a second blocking layer specific to Windows installs that haven't run hermes gateway install:

    1. _launch_detached_restart_command() watcher runs hermes gateway restart
    2. gateway_windows.start() calls is_installed() → returns False (Scheduled Task Hermes_Gateway never created)
    3. Falls through to prompt_yes_no("Install it now?", True) (gateway_windows.py:1193)
    4. Watcher process has no stdin (stdout/stderr → DEVNULL) → input() raises EOFError → sys.exit(1) (setup.py:286)

    So even with the env scrub fix from #40141, Windows users without a prior hermes gateway install are still blocked — the watcher helper hits prompt_yes_no with no TTY and dies.

    Alternative fix: route watcher through gateway run --replace instead of gateway restart

    The run subcommand handler (hermes_cli/gateway.py:6201-6208) has no _HERMES_GATEWAY defense check and bypasses gateway_windows.start() entirely — sidestepping both Layer 1 and Layer 2 in a single-line change per platform:

    • gateway/run.py:4115 (Windows watcher Popen target): restart → run --replace
    • gateway/run.py:4174 (POSIX watcher shell_cmd): restart → run --replace

    --replace handles residual process cleanup. The watcher runs strictly post-exit (after the while kill -0 $PID loop), so there's no risk of duplicate instances.

    Verified this approach on Windows 10 with Hermes 0.16.0 — is_installed()=False confirmed as the second failure point, and the run --replace path avoids both the defense check and the prompt_yes_no blocking.

  5. teknium1 commented on Jul 14, 2026

    @teknium1
    Collaborator

    Thanks for the precise report and the follow-up discussion. This is an automated hermes-sweeper review; the reported detached-helper inheritance regression is already fixed on current main.

    • cb2c13055ed9c3f467be61dbef1f3a7a5dcdc5f6 removes _HERMES_GATEWAY from the POSIX detached watcher environment in gateway/run.py:6293-6315.
    • The Windows watcher applies the same scrub in gateway/run.py:6251-6284, with regression coverage in tests/gateway/test_restart_drain.py:317-353.
    • The launchd routing concern noted in the discussion is also addressed by abc3662bf6076045e4d4dc1e14a74cb35d69b86e: gateway/slash_commands.py:1289-1309 detects XPC_SERVICE_NAME and uses the service restart path.

    Closing as implemented on main.

  6. added
    os/windowsNative Windows behaviour or breakage: PowerShell install, Windows paths, cmd.exe, WSL-vs-native diff
    and removed on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/cliCLI entry point, hermes_cli/, setup wizardcomp/gatewayGateway runner, session dispatch, deliveryos/windowsNative Windows behaviour or breakage: PowerShell install, Windows paths, cmd.exe, WSL-vs-native diffsweeper:implemented-on-mainSweeper: behavior already present on current mainsweeper:risk-message-deliverySweeper risk: may drop, duplicate, misroute, or suppress messagessweeper:risk-platform-windowsSweeper risk: may break or behave differently on native Windowstype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions