fix(gateway): /restart uses service restart under systemd instead of detached subprocess

The detached bash subprocess spawned by /restart gets killed by
systemd's KillMode=mixed cgroup cleanup, leaving the gateway dead.

Under systemd (detected via INVOCATION_ID env var), /restart now uses
via_service=True which exits with code 75 — RestartForceExitStatus=75
in the unit file makes systemd auto-restart the service. The detached
subprocess approach is preserved as fallback for non-systemd
environments (Docker, tmux, foreground mode).
This commit is contained in:
Teknium 2026-04-12 22:32:19 -07:00
parent e2a9b5369f
commit 276d20e62c
No known key found for this signature in database
2 changed files with 52 additions and 1 deletions

View file

@ -4167,7 +4167,16 @@ class GatewayRunner:
logger.debug("Failed to write restart notify file: %s", e)
active_agents = self._running_agent_count()
self.request_restart(detached=True, via_service=False)
# When running under a service manager (systemd/launchd), use the
# service restart path: exit with code 75 so the service manager
# restarts us. The detached subprocess approach (setsid + bash)
# doesn't work under systemd because KillMode=mixed kills all
# processes in the cgroup, including the detached helper.
_under_service = bool(os.environ.get("INVOCATION_ID")) # systemd sets this
if _under_service:
self.request_restart(detached=False, via_service=True)
else:
self.request_restart(detached=True, via_service=False)
if active_agents:
return f"⏳ Draining {active_agents} active agent(s) before restart..."
return "♻ Restarting gateway..."