Repository navigation
Conversation
…estart When the health-monitor triggers a stale-socket restart it sets gateway.options.reconnect.maxAttempts = 0 before disconnecting. Carbon immediately emits "Max reconnect attempts (0) reached after code 1005", which arrives in the supervisor pending queue while the lifecycle is still inside an early await (e.g. execApprovalsHandler). The previous guard checked lifecycleStopping, which is only set in the finally block — after drainPendingGatewayErrors has already run. So the queued event fell through to throw event.err, crashing the gateway process. Fix: replace the timing-sensitive lifecycleStopping check with isExpectedReconnectExhausted, which checks gateway.options.reconnect.maxAttempts === 0. This value is set before the disconnect, so it is true at drain time regardless of async ordering. Also add a regression test confirming that reconnect-exhausted with maxAttempts !== 0 (genuine Carbon retry exhaustion) still propagates as a fatal error. Closes openclaw#55554 Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Greptile SummaryThis PR fixes a gateway crash that occurred during health-monitor stale-socket restarts on Discord. The root cause was a timing race: Confidence Score: 5/5Safe to merge — the fix is narrowly scoped, well-tested with both positive and negative regression cases, and the synchronous ordering invariant it relies on is sound. No P0 or P1 issues found. The approach is correct: maxAttempts=0 is set synchronously in the abort handler before any async drain occurs, making the check reliably true at the right time. Both the positive-suppression and negative-propagation paths are exercised by tests. No files require special attention.
|
| Filename | Overview |
|---|---|
| extensions/discord/src/monitor/provider.lifecycle.ts | Replaced timing-sensitive lifecycleStopping guard with isExpectedReconnectExhausted (checks maxAttempts===0), eliminating the race condition between drainPendingGatewayErrors (try block) and lifecycleStopping=true (finally block). |
| extensions/discord/src/monitor/provider.lifecycle.test.ts | Repurposed existing queued-before-drain test to assert clean resolution when maxAttempts=0; added negative regression test confirming fatal propagation when maxAttempts is non-zero. |
Reviews (1): Last reviewed commit: "fix(discord): suppress expected reconnec..." | Re-trigger Greptile
|
Closing in favour of #55991, which has already been merged into main and covers the same race condition. Quick comparison of the three approaches that tackled this bug:
#55991 fixes the crash. #55000 fixes the root cause (wrong supervisor phase transition ordering) and is worth tracking. |
Summary
gateway.options.reconnect.maxAttempts = 0then callsdisconnect(). Carbon immediately emits"Max reconnect attempts (0) reached after code 1005", which lands in the supervisor's pending queue while the lifecycle is still awaiting an early step (e.g.execApprovalsHandler.start()).lifecycleStopping, which is only set in thefinallyblock — afterdrainPendingGatewayErrors()has already run. The queued event fell through tothrow event.err, crashing the entire gateway process (~54s downtime per occurrence, recurring every ~35 minutes).lifecycleStoppingcheck withisExpectedReconnectExhausted, which checksgateway.options.reconnect.maxAttempts === 0. This value is set synchronously beforedisconnect(), so it is reliably true at drain time regardless of async ordering. Added a regression test confirming that reconnect-exhausted withmaxAttempts !== 0(genuine Carbon retry exhaustion) still propagates as a fatal error.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
Root Cause / Regression History (if applicable)
lifecycleStoppingis set in thefinallyblock ofrunDiscordGatewayLifecycle, butdrainPendingGatewayErrors()is called inside thetryblock — beforefinallyever runs. On a stale-socket restart, the reconnect-exhausted event is queued synchronously (Carbon emits in the same tick asdisconnect()), so it is always drained beforelifecycleStoppingbecomestrue.lifecycleStoppingguard was added to handle intentional shutdowns, but did not account for the async ordering betweendrainPendingGatewayErrorsand thefinallyblock.maxAttempts=0and disconnects — Carbon's error fires faster than the lifecycle flag advances.Regression Test Plan (if applicable)
extensions/discord/src/monitor/provider.lifecycle.test.tsmaxAttempts=0; verify it still rejects whenmaxAttemptsis not 0.User-visible / Behavior Changes
Gateway no longer crashes on health-monitor stale-socket restarts. The restart proceeds silently (log at info level instead of an uncaught exception).
Diagram (if applicable)
Security Impact (required)
Repro + Verification
Environment
Steps
Expected
Gateway handles reconnect-exhausted gracefully, logs at info level, continues running.
Actual (before fix)
Uncaught exception:
Error: Max reconnect attempts (0) reached after code 1005— gateway process crashes.Evidence
"does not suppress reconnect-exhausted already queued before shutdown"→ now assertsresolves.toBeUndefined()and confirms the info log. Added"does not suppress reconnect-exhausted when maxAttempts is not 0"as the negative regression guard. All 22 tests pass.Human Verification (required)
maxAttemptsundefined (default gateway options) still propagates as fatal;maxAttempts=0with code 1005 resolves cleanly.Review Conversations
Compatibility / Migration
Risks and Mitigations
isExpectedReconnectExhaustedmight suppress a genuine exhaustion if something else setsmaxAttempts=0.maxAttempts=0is only set in one place (provider.lifecycle.reconnect.ts:404, theonAborthandler). The negative regression test guards against over-suppression.🤖 Generated with Claude Code