Skip to content

Discord gateway crashes on WebSocket close during lifecycle abort (maxAttempts=0 race condition) #55097

Closed

Description

@ztygemini-tech

Bug Description

When the Discord gateway's abort signal fires (e.g., during lifecycle management), onAbort() sets gateway.options.reconnect = { maxAttempts: 0 } and then calls gateway.disconnect(). However, if a WebSocket close event (code 1005) is received during or shortly after this, handleReconnectionAttempt sees maxAttempts=0, emits an error event, which becomes an uncaught exception and crashes the entire gateway process.

Stack Trace

[openclaw] Uncaught exception: Error: Max reconnect attempts (0) reached after code 1005
    at SafeGatewayPlugin.handleReconnectionAttempt (provider-CAlWEl41.js:3318:47)
    at SafeGatewayPlugin.handleClose (provider-CAlWEl41.js:3364:8)
    at WebSocket.<anonymous> (provider-CAlWEl41.js:3307:9)
    at WebSocket.emit (node:events:508:28)
    at WebSocket.emitClose (ws/lib/websocket.js:273:10)
    at TLSSocket.socketOnClose (ws/lib/websocket.js:1346:15)

Root Cause

Race condition in runDiscordGatewayLifecycle:

  1. onAbort() (line ~6944) sets gateway.options.reconnect = { maxAttempts: 0 } to prevent reconnection
  2. gateway.disconnect() is called
  3. WebSocket close event fires with code 1005 (abnormal closure)
  4. handleClose → handleReconnectionAttempt checks reconnectAttempts >= maxAttempts (0 >= 0 = true)
  5. Emits error event → uncaught exception → process crash

Suggested Fix

In handleReconnectionAttempt, when maxAttempts === 0 (intentional shutdown), silently return instead of emitting an error:

handleReconnectionAttempt(options) {
    const { maxAttempts = 5, baseDelay = 1e3, maxDelay = 3e4 } = this.options.reconnect ?? {};
    if (this.reconnectAttempts >= maxAttempts) {
        if (maxAttempts === 0) {
            // Intentional shutdown — don't emit error
            this.monitor.destroy();
            return;
        }
        this.emitter.emit("error", new Error(`Max reconnect attempts (${maxAttempts}) reached...`));
        this.monitor.destroy();
        return;
    }
    // ...
}

Impact

This bug causes the entire openclaw gateway to crash repeatedly whenever Discord WebSocket connections are interrupted during lifecycle transitions. The gateway must be manually restarted each time.

Environment

  • openclaw version: 2026.3.24
  • Node.js: v24.13.1
  • Platform: macOS (Darwin 25.2.0)

Activity

  1. steipete commented on Mar 26, 2026

    @steipete
    Contributor

    Closing as a duplicate of #54894.

    This matches the same Discord reconnect or abort crash path tracked in #54931 and fixed by PR #55000:

    • Discord gateway lifecycle abort sets reconnect maxAttempts to 0
    • disconnect then emits a synchronous Max reconnect attempts (0) error
    • the error escapes during teardown and crashes the whole gateway

    Keeping the canonical tracking on #54894 plus the fix discussion on #55000.

  2. locked as resolved and limited conversation to collaborators on Mar 29, 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