← Back to PRs

#11147: fix(daemon): stop gateway by port when no daemon service is active

by jasonthewhale open 2026-02-07 13:12 View on GitHub →
cli stale
**PR title:** `fix(daemon): stop gateway by port when no daemon service is active` **Base:** `main` | **Compare:** `fix/gateway-stop-port-fallback` --- #### Summary `daemon stop` now detects and kills gateway processes by port when no daemon service (systemd/launchd) is loaded, and also cleans up orphan gateway processes after a daemon stop. lobster-biscuit #### Repro Steps 1. Stop the systemd service: `systemctl --user stop openclaw-gateway.service` 2. Start gateway manually: `nohup openclaw gateway run --port 11235 --force &` 3. Run `openclaw daemon stop` 4. Gateway keeps running — `daemon stop` only checked the service, not the port #### Root Cause `runDaemonStop()` only used `service.stop()` via systemd/launchd. When the gateway was started outside the daemon (e.g. `nohup`), there was no fallback to detect and stop it. Additionally, `service.isLoaded()` checks whether the unit file is *enabled*, not whether the service is *active* — so even when systemd was stopped, `isLoaded()` returned `true`, and the daemon stop path ran without actually killing the standalone process. #### Behavior Changes - When no daemon service is loaded and a gateway process is listening on the configured port, `daemon stop` now sends SIGTERM to that process and waits up to 5s for it to exit. - After a successful `service.stop()`, orphan gateway processes on the configured port are also cleaned up. - JSON output includes the stop result in both paths. #### Tests - `pnpm build` — pass - `pnpm test src/cli/daemon-cli.coverage.test.ts` — 9 tests pass (2 new) - `pnpm test` — 952/961 test files pass; 9 failures are pre-existing (memory/lobster tests, unrelated) New tests: - "stops gateway by port when service is not loaded" — verifies SIGTERM is sent to the PID found on the port - "reports not-loaded when no service and no gateway on port" — verifies original behavior when port is free #### Manual Testing ##### Prerequisites - Linux server with systemd and OpenClaw installed ##### Steps 1. Stop systemd service, start gateway via `nohup openclaw gateway run --port 11235 --force &` 2. Run `openclaw daemon stop` — gateway process killed successfully, logs "Stopped gateway process (pid XXXXX) on port 11235." 3. Restore systemd service, verify `openclaw daemon stop` still works normally (no regression) <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR updates the `daemon stop` lifecycle to fall back to stopping a running gateway process by inspecting the configured gateway port when no system service (systemd/launchd) is loaded, and it also attempts a best-effort cleanup of orphan gateway processes after a successful service stop. It adds CLI coverage tests for the new “stop by port” fallback and for the unchanged “not-loaded” behavior when the port is free. <h3>Confidence Score: 3/5</h3> - Mostly safe to merge, but stop-status reporting is currently inaccurate and one success message can be incorrect in real environments. - Core behavior change is contained and tests cover the new fallback path, but `runDaemonStop()` still uses `service.isLoaded()` as a post-stop verification (which doesn’t reflect active state), and the port-based stopper can report success without having a PID to signal when listener details are unavailable. - src/cli/daemon-cli/lifecycle.ts <!-- greptile_other_comments_section --> <sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub> <!-- /greptile_comment -->

Most Similar PRs