Skip to content

fix(discord): prevent uncaught gateway errors from crashing the process - #54182

Merged
steipete merged 2 commits into
openclaw:mainfrom
sfwn:fix/gateway-uncaught-reconnect-error
Mar 25, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
sfwn:fix/gateway-uncaught-reconnect-error

Conversation

@sfwn

@sfwn sfwn commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Move cleanup() after disconnect() in waitForDiscordGatewayStop so the error listener remains active during disconnect — prevents late "error" events from becoming uncaught exceptions
  • Add a safety error listener in the runDiscordGatewayLifecycle finally block to suppress late errors emitted by Carbon during teardown

Fixes the Uncaught exception: Error: Max reconnect attempts (0) reached after code 1006 crash 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:

  1. waitForDiscordGatewayStop calls cleanup() (removes error listener) before disconnect() — if disconnect triggers another error, it's unhandled
  2. The lifecycle finally block releases the early gateway error guard, leaving no error listener on the emitter for any late errors

In Node.js, an EventEmitter "error" event with no listener throws an uncaught exception, crashing the process.

Test plan

  • Verify gateway survives Discord WebSocket disconnection (code 1006) without crashing
  • Verify late gateway errors during teardown are logged but suppressed
  • Verify other channels (Telegram, Slack) remain operational when Discord reconnection fails

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.
@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: XS labels Mar 25, 2026
@greptile-apps

greptile-apps Bot commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a crash where an uncaught Error: Max reconnect attempts reached event on the Carbon gateway's EventEmitter kills the entire gateway process (including Telegram and Slack channels). Two complementary fixes are applied:

  1. monitor.gateway.ts: cleanup() (which removes the error listener) is moved to after gateway?.disconnect?.() in both finishResolve and finishReject, ensuring synchronous "error" events emitted during disconnect are still routed to the registered handler.
  2. provider.lifecycle.ts: A suppressLateError safety listener is attached in the finally block before releasing the early gateway error guard, catching any asynchronous late errors after normal lifecycle cleanup.

The logic in monitor.gateway.ts is clean and correct. The provider.lifecycle.ts change has one issue worth addressing: suppressLateError is registered with .on() but never removed, which will cause listener accumulation on each lifecycle restart if the gatewayEmitter instance is reused across runs.

Confidence Score: 4/5

  • Safe to merge with one targeted fix — the listener leak in the finally block should be resolved before or shortly after landing.
  • Both changes address a real and well-diagnosed crash. The monitor.gateway.ts change is correct with no issues. The provider.lifecycle.ts change is sound in intent but introduces a listener that is never removed, which could accumulate across reconnect cycles. This is a reliability concern in a long-running process but does not block the primary fix from working.
  • extensions/discord/src/monitor/provider.lifecycle.ts — the suppressLateError listener added in the finally block (line 429) is never removed.
Prompt To Fix All With AI
This 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
steipete merged commit 9ab226d into openclaw:main Mar 25, 2026
39 of 40 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Landed via temp rebase onto main.

  • Gate: pnpm test -- extensions/discord/src/monitor/gateway-error-guard.test.ts extensions/discord/src/monitor.gateway.test.ts extensions/discord/src/monitor/provider.lifecycle.test.ts extensions/discord/src/monitor/provider.test.ts; committer also ran pnpm check
  • Land commit: 5fecad2
  • Merge commit: 9ab226d

Thanks @sfwn!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: discord Channel integration: discord size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants