Repository navigation
fix(discord): prevent uncaught gateway errors from crashing the process - #54182
Merged
steipete merged 2 commits intoMar 25, 2026
Merged
Conversation
Move cleanup() after disconnect() in waitForDiscordGatewayStop so the error listener is still active during disconnect. Add a safety error listener in the lifecycle finally block to suppress late errors emitted by Carbon during teardown. Fixes the "Max reconnect attempts (0) reached after code 1006" uncaught exception that kills the entire gateway process when a Discord WebSocket drops and reconnection fails.
Contributor
Greptile SummaryThis PR fixes a crash where an uncaught
The logic in Confidence Score: 4/5
Prompt To Fix All With AIThis is a comment left during a code review.
Path: extensions/discord/src/monitor/provider.lifecycle.ts
Line: 429
Comment:
**`suppressLateError` listener is never removed**
`suppressLateError` is registered with `.on()` but never removed from `gatewayEmitter`. Every time `runDiscordGatewayLifecycle` finishes and is subsequently restarted (e.g. after a disconnect/reconnect cycle), another permanent listener is added to the same `gatewayEmitter`. If the underlying Carbon emitter instance is reused across lifecycle runs (which is plausible since `params.client` is passed in externally), listeners accumulate on each teardown, eventually triggering Node.js `MaxListenersExceededWarning` and causing listener count to grow unboundedly.
The existing `onAbort` handler already uses `gatewayEmitter?.once("error", () => {})` to handle a single error event in a self-cleaning way. The same pattern would be safer here, or the listener should be explicitly removed at the end of the `finally` block:
```
gatewayEmitter?.on("error", suppressLateError);
params.releaseEarlyGatewayErrorGuard?.();
unregisterGateway(params.accountId);
stopGatewayLogging();
reconnectStallWatchdog.stop();
clearHelloWatch();
gatewayEmitter?.removeListener("debug", onGatewayDebug);
params.abortSignal?.removeEventListener("abort", onAbort);
if (params.voiceManager) {
await params.voiceManager.destroy();
params.voiceManagerRef.current = null;
}
if (params.execApprovalsHandler) {
await params.execApprovalsHandler.stop();
}
params.threadBindings.stop();
+ gatewayEmitter?.removeListener("error", suppressLateError);
```
Alternatively, if only a single late error needs to be suppressed, switch `.on` → `.once`:
```suggestion
gatewayEmitter?.once("error", suppressLateError);
```
Note that `.once` would leave subsequent late errors unhandled, so only use it if Carbon is known to emit at most one error after disconnect. Explicit removal at the end of the block is the safer general-purpose fix.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(discord): prevent uncaught gateway e..." | Re-trigger Greptile |
steipete
added a commit
that referenced
this pull request
Mar 25, 2026
Contributor
|
Landed via temp rebase onto main.
Thanks @sfwn! |
This was referenced Mar 25, 2026
vixco
pushed a commit
to vixco/openclaw
that referenced
this pull request
Aug 16, 2026
zooqueen
pushed a commit
to zooqueen/openclaw
that referenced
this pull request
Sep 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
cleanup()afterdisconnect()inwaitForDiscordGatewayStopso the error listener remains active during disconnect — prevents late "error" events from becoming uncaught exceptionsrunDiscordGatewayLifecyclefinally block to suppress late errors emitted by Carbon during teardownFixes the
Uncaught exception: Error: Max reconnect attempts (0) reached after code 1006crash that kills the entire gateway process (including Telegram, Slack, etc.) when a Discord WebSocket drops and reconnection fails.Related: #18410, #14703, #21099, #20714
Root cause
When the Carbon gateway emits an "error" event on its EventEmitter during disconnect or teardown, no listener is attached:
waitForDiscordGatewayStopcallscleanup()(removes error listener) beforedisconnect()— if disconnect triggers another error, it's unhandledfinallyblock releases the early gateway error guard, leaving no error listener on the emitter for any late errorsIn Node.js, an EventEmitter "error" event with no listener throws an uncaught exception, crashing the process.
Test plan