Skip to content

Discord WebSocket reconnect failure crashes entire gateway (affects all channels) #54894

Closed

Description

@kagura-chen

Bug

A transient Discord WebSocket disconnection (close code 1005) triggers an uncaught exception that crashes the entire gateway process. This kills all channels (Feishu, WhatsApp, etc.), not just Discord.

This happened twice in one morning during normal Feishu usage. Both times, the gateway had to be manually restarted.

Error

[openclaw] Uncaught exception: Error: Max reconnect attempts (0) reached after code 1005
    at SafeGatewayPlugin.handleReconnectionAttempt (file:///…/node_modules/@buape/carbon/src/plugins/gateway/GatewayPlugin.ts:400:5)
    at SafeGatewayPlugin.handleClose (…/GatewayPlugin.ts:484:8)
    at WebSocket.<anonymous> (…/GatewayPlugin.ts:376:9)
    at WebSocket.emit (node:events:519:28)
    …
ELIFECYCLE  Command failed with exit code 1.

Analysis

Root cause chain

  1. Discord WebSocket closes with code 1005 (No Status Received — normal network fluctuation)
  2. @buape/carbon GatewayPlugin attempts reconnect, but maxAttempts resolves to 0 despite being configured as 50 in createDiscordGatewayPlugin() (extensions/discord/src/monitor/gateway-plugin.ts)
  3. handleReconnectionAttempt immediately gives up → emits an Error via this.emitter.emit("error", ...)
  4. The error propagates as an uncaught exception (no global process.on('uncaughtException') handler in the gateway)
  5. Entire gateway process exits → all channels (Feishu, Discord, WhatsApp, etc.) go down

Two issues

  1. maxAttempts mismatch: The code passes { reconnect: { maxAttempts: 50 } } to the GatewayPlugin constructor, but at runtime the value is 0. This may be a @buape/[email protected] bug or a state mutation during the session.

  2. No fault isolation: A single channel's WebSocket failure should not crash the entire gateway. Currently there is no uncaughtException/unhandledRejection handler, and no error boundary around individual channel plugins.

Expected behavior

  • Discord WebSocket disconnection should retry with the configured 50 max attempts
  • If Discord permanently fails, it should be marked as degraded — other channels should continue working
  • The gateway process should not exit due to a single channel's connection failure

Environment

  • OpenClaw: 2026.3.24
  • Node.js: v22.22.1
  • @buape/carbon: 0.0.0-beta-20260216184201
  • OS: Linux 6.8.0-101-generic (x64)
  • Channels: Feishu (primary) + Discord

Suggested fixes

  1. Immediate: Add a try/catch or error listener around SafeGatewayPlugin's error emission so it doesn't become an uncaught exception
  2. Short-term: Add global process.on('uncaughtException') / process.on('unhandledRejection') handlers in the gateway entry point that log the error and attempt graceful recovery
  3. Long-term: Isolate channel plugins so one channel's failure doesn't affect others (e.g., per-channel error boundaries, automatic restart of failed channels)

Activity

  1. added a commit that references this issue on Mar 26, 2026
    3dfa622
  2. openperf commented on Mar 26, 2026

    @openperf
    Member

    Hi @kagura-agent, thanks for reporting this and for the analysis.
    Your observation about maxAttempts resolving to 0 at runtime (despite being configured as 50) is spot on — that happens because onAbort() explicitly sets gateway.options.reconnect = { maxAttempts: 0 } before calling disconnect(). Once that mutation happens, any subsequent WebSocket close event hits the 0 >= 0 check and emits the fatal error.
    The real problem is that when onAbort() calls disconnect(), the gateway supervisor is still in the active phase, so it routes that error to the lifecycle handler instead of suppressing it. The supervisor already knows how to swallow errors during teardown — it just wasn't being told to enter teardown before the disconnect call.
    This is actually the same root cause as #54931 (health-monitor crash loop), just a different trigger path. In #54931 it's the health-monitor detecting a stale socket; here it's a transient network disconnection. Both end up in the same onAbort() → disconnect() → uncaught error chain.
    PR #55000 fixes the shared root cause by calling detachLifecycle() before disconnect() in onAbort(), so the supervisor is in the teardown phase when the error fires. It also adds a safety net in the catch block for any edge cases. This should resolve both issues.

  3. kagura-chen commented on Mar 26, 2026

    @kagura-chen
    ContributorAuthor

    Thanks @openperf — that makes total sense. The onAbort() → disconnect() → uncaught error chain explains exactly what I saw in the logs. Good to know #55000 fixes both paths. 👍

  4. Takhoffman commented on Mar 29, 2026

    @Takhoffman
    Contributor

    Closing as fixed upstream. This crash family was addressed by upstream Discord lifecycle changes merged in #55991, and PR #55000 was closed after the upstream maintainer confirmed the fix landed there.

    If you still hit the same Max reconnect attempts (0) / stale-socket gateway crash on the latest OpenClaw release, comment here with fresh logs and version details and we can reopen or spin a new issue.

  5. locked as resolved and limited conversation to collaborators on Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions