Repository navigation
fix(gateway): exit 0 on EADDRINUSE port conflict to prevent crash-loops (#28191) - #29499
BlueBirdBack wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3b2e199402
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| lockCause != null && | ||
| typeof lockCause === "object" && | ||
| (lockCause as NodeJS.ErrnoException).code === "EADDRINUSE"; | ||
| defaultRuntime.exit(isPortConflict ? 0 : 1); |
There was a problem hiding this comment.
Keep non-gateway EADDRINUSE as startup failure
This now returns exit code 0 for any EADDRINUSE, but that errno is also raised when an unrelated process is bound to the configured port (for example, another service or stale local dev process). In those cases startup still failed, and returning success prevents supervisors using Restart=on-failure from retrying, which can leave the gateway permanently down until manual intervention. Please only exit 0 when you can confirm the listener is an already-running OpenClaw gateway instance; otherwise keep the non-zero exit.
Useful? React with 👍 / 👎.
Greptile SummaryThis PR fixes a crash-loop issue where a second gateway instance would repeatedly restart when encountering a port conflict. The fix correctly distinguishes between
Confidence Score: 5/5
Last reviewed commit: 3b2e199 |
3b2e199 to
35b0333
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 35b0333c1a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| lockCause != null && | ||
| typeof lockCause === "object" && | ||
| (lockCause as NodeJS.ErrnoException).code === "EADDRINUSE"; | ||
| defaultRuntime.exit(isPortConflict ? 0 : 1); |
There was a problem hiding this comment.
Return failure for unknown EADDRINUSE conflicts
listenGatewayHttpServer converts any socket bind EADDRINUSE into GatewayLockError without verifying the owner process (src/gateway/server/http-listen.ts:26-31), so this new exit(0) path also treats unrelated listeners (for example, another local service already on the configured port) as a successful gateway start. In that scenario the gateway is still down, but callers/supervisors that rely on non-zero status (especially Restart=on-failure setups and health scripts) will stop retrying because startup is reported as success.
Useful? React with 👍 / 👎.
|
This pull request has been automatically marked as stale due to inactivity. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 247294cd8e
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, | ||
| timeoutMs: 3000, | ||
| }).catch(() => null); |
There was a problem hiding this comment.
Include resolved credentials in EADDRINUSE probe
When EADDRINUSE is hit, this branch probes ws://127.0.0.1:${port} without passing auth, even though this command already resolved token/password earlier. probeGateway only reports ok after a successful authenticated connect and follow-up RPCs, so if the already-running gateway requires auth (a common production setup), the probe returns false and the process still exits 1, leaving Restart=on-failure crash-loops unresolved in exactly the scenario this change is meant to fix.
Useful? React with 👍 / 👎.
| (lockCause as NodeJS.ErrnoException).code === "EADDRINUSE"; | ||
| if (isPortConflict) { | ||
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, |
There was a problem hiding this comment.
Probe the configured bind host, not only loopback
The probe hardcodes 127.0.0.1, so duplicate-start detection fails when the gateway is intentionally bound to a non-loopback custom host (for that mode the server listens on the configured host, not loopback aliases). In that valid configuration, EADDRINUSE from an already-running gateway still leads to probe failure and exit 1, so supervisors continue restart loops instead of treating the existing instance as healthy.
Useful? React with 👍 / 👎.
|
Still active — pushed a fix for the Codex P1 review (commit Issue: The previous fix exited 0 for any EADDRINUSE, including when an unrelated process holds the port. Supervisors with Fix: When EADDRINUSE is detected, probe |
247294c to
95bc5ba
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 95bc5baf68
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, | ||
| timeoutMs: 3000, | ||
| }).catch(() => null); |
There was a problem hiding this comment.
Reuse resolved credentials in EADDRINUSE probe
When handling EADDRINUSE, the new probe omits auth even though this command already resolved token/password earlier. probeGateway only returns ok after a successful authenticated connection and follow-up RPCs, so if the already-running gateway is in token/password mode (a common production configuration), this probe returns not-ok and the process still exits non-zero, preserving the restart crash-loop this change is meant to prevent.
Useful? React with 👍 / 👎.
| (lockCause as NodeJS.ErrnoException).code === "EADDRINUSE"; | ||
| if (isPortConflict) { | ||
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, |
There was a problem hiding this comment.
Probe the actual bind host instead of loopback
The duplicate-instance check hardcodes ws://127.0.0.1:${port}, but the gateway can be configured to bind to a specific non-loopback host (bind=custom with customBindHost). In that valid setup an already-running gateway will not accept loopback connections, so the probe reports failure and startup exits non-zero even though the port conflict is the expected “already running” case.
Useful? React with 👍 / 👎.
95bc5ba to
e85222e
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e85222e8c4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, | ||
| timeoutMs: 3000, |
There was a problem hiding this comment.
Handle trusted-proxy mode in EADDRINUSE probe
In trusted-proxy auth mode, this duplicate-instance check cannot succeed because the probe is launched without any proxy-auth context, and probeGateway only supports token/password auth inputs (src/gateway/probe.ts), so an already-running trusted-proxy gateway is still treated as a startup failure and exits non-zero. That means Restart=on-failure crash loops remain for trusted-proxy deployments even though the port conflict is the expected "already running" case.
Useful? React with 👍 / 👎.
e85222e to
4a4456b
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4a4456badc
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| (lockCause as NodeJS.ErrnoException).code === "EADDRINUSE"; | ||
| if (isPortConflict) { | ||
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, |
There was a problem hiding this comment.
Use wss probe when gateway TLS is enabled
The EADDRINUSE recovery probe always targets ws://127.0.0.1:${port}, but the gateway can run with TLS (gateway.tls.enabled), where the listener is created via createHttpsServer and only accepts wss:// upgrades (src/gateway/server-http.ts:598-600). In that valid configuration, probing over plaintext WS will fail even when another OpenClaw gateway instance is healthy, so this branch exits 1 and supervisors keep restart-looping instead of treating the conflict as “already running.”
Useful? React with 👍 / 👎.
4a4456b to
d220d09
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d220d0900a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, | ||
| timeoutMs: 3000, |
There was a problem hiding this comment.
Pass resolved auth to the EADDRINUSE probe
The new conflict probe is invoked without credentials, even though this command already resolved token/password auth earlier in runGatewayCommand. probeGateway only returns ok after a successful authenticated connection and follow-up RPCs (src/gateway/probe.ts), so when an existing gateway uses token or password auth, this branch still exits with code 1 and supervisors continue crash-looping instead of treating the instance as already running.
Useful? React with 👍 / 👎.
| (lockCause as NodeJS.ErrnoException).code === "EADDRINUSE"; | ||
| if (isPortConflict) { | ||
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, |
There was a problem hiding this comment.
Probe the actual listener endpoint instead of ws://127.0.0.1
The probe URL is hardcoded to ws://127.0.0.1:${port}, which breaks duplicate-instance detection for valid gateway configs that do not listen on plaintext loopback. In particular, TLS mode creates an HTTPS server (src/gateway/server-http.ts) so a plaintext ws:// probe cannot connect, and bind=custom listens on the configured non-loopback host only (src/gateway/server-runtime-config.ts + src/gateway/server-runtime-state.ts), so probing 127.0.0.1 fails; both cases incorrectly return exit code 1 even when an OpenClaw gateway is already running.
Useful? React with 👍 / 👎.
800627f to
e1f3c61
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e1f3c618ac
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, | ||
| timeoutMs: 3000, | ||
| }).catch(() => null); |
There was a problem hiding this comment.
Pass resolved auth credentials to the EADDRINUSE probe
The new EADDRINUSE recovery path probes without auth, even though this function already resolved token/password credentials earlier in the same command path, so any already-running gateway in token or password mode will fail the probe and still exit non-zero. That means the intended crash-loop mitigation does not work for secured deployments, which are a common production configuration.
Useful? React with 👍 / 👎.
| (lockCause as NodeJS.ErrnoException).code === "EADDRINUSE"; | ||
| if (isPortConflict) { | ||
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, |
There was a problem hiding this comment.
Probe the actual gateway endpoint instead of fixed loopback ws
The probe URL is hardcoded to ws://127.0.0.1:${port}, so duplicate-instance detection fails when the running gateway is not reachable at that exact endpoint (for example TLS-enabled wss:// listeners or non-loopback bind modes like custom/tailnet). In those valid configurations this path still exits with failure on an existing healthy instance, so supervisors continue restart loops.
Useful? React with 👍 / 👎.
e1f3c61 to
5dd7ea8
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5dd7ea80d4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, | ||
| timeoutMs: 3000, | ||
| }).catch(() => null); |
There was a problem hiding this comment.
Pass resolved auth to EADDRINUSE probe
runGatewayCommand already resolves gateway credentials (tokenValue/passwordValue) before startup, but this probe call omits auth, and probeGateway only sets ok after an authenticated onHelloOk plus follow-up RPCs. In token/password deployments, an already-running gateway will reject this unauthenticated probe, so the new conflict path still exits non-zero and supervisors continue restart loops instead of treating the instance as already running.
Useful? React with 👍 / 👎.
| (lockCause as NodeJS.ErrnoException).code === "EADDRINUSE"; | ||
| if (isPortConflict) { | ||
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, |
There was a problem hiding this comment.
Probe actual bind/scheme instead of fixed ws loopback
The conflict probe is hardcoded to ws://127.0.0.1:${port}, but the gateway can legitimately listen on a non-loopback bind host (bind=custom/other modes) and can run TLS (HTTPS/WSS). In those configurations, this endpoint is not the running listener, so the probe incorrectly fails and the process exits with failure even when the port is held by a healthy OpenClaw gateway instance.
Useful? React with 👍 / 👎.
… foreign process Addresses Codex P1 review: the previous fix exited 0 for any EADDRINUSE, including when an unrelated process (another service, stale dev server) holds the configured port. In that case the gateway is still down but supervisors with Restart=on-failure stop retrying because startup reports success. Fix: when EADDRINUSE is detected, probe ws://127.0.0.1:<port> with a 3s timeout to confirm an OpenClaw gateway actually answers there. Exit 0 only when the probe succeeds; exit 1 if the probe fails (foreign process or transient bind error) so the supervisor retries.
5dd7ea8 to
22a52aa
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 22a52aaf89
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, | ||
| timeoutMs: 3000, | ||
| }).catch(() => null); |
There was a problem hiding this comment.
Pass resolved credentials to the port-conflict probe
This probe call omits auth even though runGatewayCommand has already resolved tokenValue/passwordValue, so any already-running gateway in token/password mode will reject the handshake and probeGateway will return ok: false (it only sets ok after authenticated onHelloOk and follow-up RPCs in src/gateway/probe.ts). In those common secured deployments, EADDRINUSE still exits non-zero and supervisors keep restart-looping, so the crash-loop mitigation does not actually work.
Useful? React with 👍 / 👎.
| (lockCause as NodeJS.ErrnoException).code === "EADDRINUSE"; | ||
| if (isPortConflict) { | ||
| const probe = await probeGateway({ | ||
| url: `ws://127.0.0.1:${port}`, |
There was a problem hiding this comment.
Derive probe endpoint from active gateway listener settings
The duplicate-instance check hardcodes ws://127.0.0.1:${port}, which does not match valid listener configurations such as TLS-enabled gateways (wss://) or non-loopback/custom bind hosts; in those cases the probe fails even when an OpenClaw gateway is already healthy on the conflicting port. That causes this new branch to fall back to exit 1, so restart-on-failure loops continue in exactly the configurations this change is meant to handle.
Useful? React with 👍 / 👎.
|
Superseded by #44476 which fixes the same issue with proper auth credential passing and bind/TLS-aware URL resolution. Thanks for identifying this! 🦞 |
Problem
When a second gateway instance tries to start and finds the port already bound (
EADDRINUSE), it throws aGatewayLockErrorwhich propagates as a non-zero exit (code 1). Under systemd withRestart=on-failure(the default), exit 1 triggers an immediate restart — which hits the same error — creating a tight crash-loop that burns CPU and floods logs.Reproduced in
openclaw-lab:2026.2.26(Docker): second gateway exits code 1 on port conflict.Root Cause
src/gateway/server/http-listen.ts:26-31throwsGatewayLockError(withcause.code === "EADDRINUSE") on port conflict. This is caught insrc/cli/gateway-cli/run.tsand unconditionally callsdefaultRuntime.exit(1).Introduced by commit
bcbfb357be(Peter Steinberger, 2026-01-14). Partially mitigated by16ccd5a87(ThrottleInterval for launchd) anddd07c06d0(tighter restart loop), but the core EADDRINUSE → clean exit was never addressed.Fix
In the
GatewayLockErrorcatch block inrun.ts, inspecterr.cause.code. If it isEADDRINUSE, the port is already owned by a live gateway — this process has nothing to do, so exit 0. The error message and diagnostics are still printed. All otherGatewayLockErrorvariants (lock-file timeout, stale lock, etc.) continue to exit 1.Testing
gateway-cli.coverage.test.tsGatewayLockError tests still pass (they do not setcause.code === EADDRINUSE).Restart=on-failurewill not restart the gateway.Fixes #28191