#19573: fix(infra): detect OpenClaw service env vars in supervisor hint check
size: XS
trusted-contributor
Cluster:
Gateway Restart Improvements
## Summary
`restartGatewayProcessWithFreshPid()` should avoid spawning a child when the gateway is already managed by a supervisor (launchd/systemd), and instead exit so the supervisor restarts it.
Today, supervised detection in `src/infra/process-respawn.ts` can miss OpenClaw-managed services because it only checked native supervisor env hints (`LAUNCH_JOB_LABEL`, `INVOCATION_ID`, etc.). On macOS in particular, launchd does not reliably provide `LAUNCH_JOB_LABEL` to child environments.
OpenClaw already injects its own service markers (`OPENCLAW_LAUNCHD_LABEL`, `OPENCLAW_SYSTEMD_UNIT`) in service env setup (`src/daemon/service-env.ts`), but those were not considered during respawn detection. That mismatch can cause a false "not supervised" result, leading to detached self-spawn while the supervisor also restarts, which creates duplicate gateway processes and port contention.
## What Changed
### Supervised restart detection for OpenClaw-managed services
- `src/infra/process-respawn.ts`: Added `OPENCLAW_LAUNCHD_LABEL` and `OPENCLAW_SYSTEMD_UNIT` to `SUPERVISOR_HINT_ENV_VARS`.
- `src/infra/process-respawn.test.ts`: Updated `clearSupervisorHints()` to clear both new env vars.
- Added explicit tests that each env var triggers `mode: "supervised"` and avoids spawning.
## Design Notes
This keeps the current lightweight heuristic approach (env-based hints) instead of adding platform-specific process inspection. The tradeoff is that detection still depends on env correctness, but now it aligns with the env contract OpenClaw itself establishes during service installation, which is the most reliable signal for managed deployments.
## Testing
- `pnpm vitest run src/infra/process-respawn.test.ts` (Result: `1` file passed, `6` tests passed)
## AI Disclosure
- [x] AI-assisted
- [x] Fully tested
## Related Issues
- #21685
- #14161
- #14315
- #21995
- #19521
---
Closes #21073
Closes #22257
Most Similar PRs
#20555: fix(gateway): detect launchd supervision via XPC_SERVICE_NAME
by dimat · 2026-02-19
78.4%
#16845: fix(daemon): gateway auto-restart on SIGTERM + agent restart guidel...
by kiminbean · 2026-02-15
65.1%
#10182: fix: skip non-openclaw LaunchAgents in doctor gateway scan
by Yida-Dev · 2026-02-06
64.9%
#22304: Gateway: fix launchd start after stop
by apethree · 2026-02-21
64.8%
#8195: feat(migration): env fallbacks + legacy launchd cleanup
by shuv-amp · 2026-02-03
64.5%
#21591: fix(update): prevent double restart when refreshing service env
by irchelper · 2026-02-20
64.4%
#17835: Fix misleading gateway stop hints for standalone listeners
by ConnorCallison · 2026-02-16
64.4%
#17862: fix: Windows exec returns empty output when pty=false
by MisterGuy420 · 2026-02-16
63.8%
#18236: macOS daemon: bootstrap LaunchAgent on gateway start after stop
by agisilaos · 2026-02-16
63.7%
#20272: fix: LaunchAgent KeepAlive causes restart loop (fixes #20257)
by MisterGuy420 · 2026-02-18
63.6%