Skip to content

BUG: Discord health-monitor triggers uncaught exception crash loop (v2026.3.24) #54931

Closed

Description

@kAIborg24

Bug type

Regression (worked before, now fails)

Summary

After upgrading from v2026.3.11 to v2026.3.24, the gateway crashes every ~35 minutes due to Discord's health-monitor detecting stale sockets and triggering a reconnection path that throws an uncaught exception. Zero crashes occurred across 5+ days on v2026.3.11. On v2026.3.24, 16 crashes occurred in a single day.

Steps to reproduce

  1. Install v2026.3.24 with Discord channel enabled (single guild, allowlist-only)
  2. Gateway runs normally for ~30-35 minutes
  3. Discord health-monitor detects a stale WebSocket (no events within staleSocketMinutes, default 30)
  4. Health-monitor calls stopChannel() → triggers onAbort()
  5. onAbort() sets gateway.options.reconnect = { maxAttempts: 0 } then calls gateway.disconnect()
  6. WebSocket closes with code 1005 ("No Status Received")
  7. handleClose(1005) → handleReconnectionAttempt() → checks reconnectAttempts(0) >= maxAttempts(0) → true
  8. Emits new Error("Max reconnect attempts (0) reached after code 1005")
  9. Error is uncaught → entire Node.js process crashes
  10. systemd restarts → cycle repeats every ~35 minutes

Expected behavior

Health-monitor should gracefully restart the Discord channel without crashing the gateway process.

Actual behavior

The onAbort handler sets maxAttempts: 0 before disconnecting. The WebSocket close handler then fires and immediately triggers the max-attempts error path (0 >= 0 is true), emitting an uncaught exception that crashes the entire Node.js process.

OpenClaw version

2026.3.24 (upgraded from 2026.3.11)

Operating system

Ubuntu 24.04 LTS (Linux 6.18.7 x64)

Install method

npm global

Model

anthropic/claude-opus-4-6 / anthropic/claude-sonnet-4-6

Provider / routing chain

openclaw -> anthropic (direct)

Additional provider/model setup details

Bug is in Discord WebSocket lifecycle management, not model-specific.

Logs, screenshots, and evidence

[health-monitor] [discord:default] health-monitor: restarting (reason: stale-socket)
[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)

Root cause in provider-CAlWEl41.js:

Line 6952 — onAbort sets: gateway.options.reconnect = { maxAttempts: 0 };

Lines 3316-3318 — Reconnection handler checks:

const { maxAttempts = 5 } = this.options.reconnect ?? {};
if (this.reconnectAttempts >= maxAttempts) {
    this.emitter.emit("error", new Error(`Max reconnect attempts (${maxAttempts}) reached...`));

Crash frequency data:

• Mar 17-24 (v2026.3.11): 0 crashes across 5+ days
• Mar 25 (v2026.3.24): 16 crashes in one day, every ~35 min

Impact and severity

High — Gateway crashes every ~35 minutes. All running subagent sessions are disrupted or killed. Subagent completion announce-back fails after restart ("Outbound not configured for channel: telegram"). Long-running subagent tasks (30-75 min) have near-zero chance of completing.

Additional information

Suggested fixes:

  1. (Preferred) Set a flag to suppress the close handler rather than manipulating maxAttempts — lifecycleStopping already exists on line 6944, add a check in handleClose
  2. Set maxAttempts to a sentinel value that handleReconnectionAttempt treats as "intentional shutdown, don't emit error"
  3. Catch the error in the health-monitor's restart flow so it doesn't propagate as uncaught

Workaround: Disable Discord (channels.discord.enabled: false).

Note: Also observed a secondary issue — with Discord channel disabled but Discord plugin still enabled (plugins.entries.discord.enabled: true), message-action-discovery still tries to resolve the Discord token SecretRef, causing a separate crash ("Unhandled promise rejection: channels.discord.token: unresolved SecretRef"). Both the channel AND plugin must be disabled as workaround.

Activity

  1. added
    bug
    on Mar 26, 2026
  2. added
    regression
    on Mar 26, 2026
  3. added a commit that references this issue on Mar 26, 2026
    91e4030
  4. added a commit that references this issue on Mar 26, 2026
    71d222b
  5. lolaopenclaw commented on Mar 26, 2026

    @lolaopenclaw

    Confirming exact same behavior on our setup.

    Environment:

    • OpenClaw: v2026.3.24 (upgraded from v2026.3.23-2 via auto-update cron)
    • OS: Ubuntu 24.04 LTS (Linux 6.8.0-106-generic x64)
    • Node: v25.8.1
    • Install: npm global
    • Channels: Telegram (primary) + Discord (single guild, allowlist)

    Crash data:

    • 13 crashes between 22:06 CET (Mar 25) and 07:19 CET (Mar 26)
    • Average interval: ~35 minutes (consistent with staleSocketMinutes default of 30)
    • All crashes identical: Error: Max reconnect attempts (0) reached after code 1005
    • Stack trace matches OP exactly: SafeGatewayPlugin.handleReconnectionAttempt
    • systemd auto-restarts in ~5s each time

    Timeline:

    • v2026.3.23-2: No crashes observed during the day (Discord connected and working)
    • Auto-update to v2026.3.24 at ~21:30 CET
    • First crash at 22:06 CET (36 min after gateway start post-update)
    • Crash loop continues through the night, even through cron jobs running successfully between crashes

    Impact:

    • Gateway recovers quickly (systemd restart), but crons running at crash time may lose delivery
    • Telegram channel unaffected (crashes are Discord-originated but kill entire process)
    • Morning report delivery failed once due to crash timing coincidence

    Workaround attempted: None yet. Considering disabling Discord channel as temporary mitigation since Telegram is our primary channel.

    Logs available if needed — happy to provide full journalctl output.

  6. openperf commented on Mar 26, 2026

    @openperf
    Member

    Hey @kAIborg24, thanks for the detailed write-up — the step-by-step reproduction and crash frequency data made it really easy to pinpoint the root cause.
    The core issue is a timing problem in onAbort(): it sets maxAttempts to 0 and calls gateway.disconnect(), but at that point the gateway supervisor is still in the active phase. So the synchronous "Max reconnect attempts (0)" error emitted by @buape/carbon during disconnect gets routed to the lifecycle handler instead of being suppressed — and that's what crashes the process.
    The fix in PR #55000 is straightforward: call detachLifecycle() before disconnect() so the supervisor transitions to the teardown phase first. The supervisor already has the logic to swallow late errors during teardown — it just wasn't being triggered at the right time.
    Worth noting: #54894 reports the exact same crash (Max reconnect attempts (0) reached after code 1005, same stack trace through SafeGatewayPlugin.handleReconnectionAttempt), just triggered via a different entry point (transient network disconnection rather than health-monitor restart). The underlying cause is identical — onAbort() had already mutated maxAttempts to 0 and the supervisor phase wasn't updated before disconnect. So #55000 addresses both issues with the same fix.

  7. ForceConstant commented on Mar 26, 2026

    @ForceConstant
    Contributor

    @openperf any way to workaround with this version until your PR is released?

  8. bokiko commented on Mar 27, 2026

    @bokiko

    Also hitting this on 2026.3.24 (cff6dc9) — 14 crashes on Mar 26, 5 so far today.

    Environment: Mac Mini M4 Pro, Node v25.8.0, macOS 26.3.1, Homebrew install
    Channels: 2 guild channels + DMs

    Same stack trace — handleReconnectionAttempt throws when maxAttempts = 0 after health-monitor teardown.

    Crash cadence is ~60-90 min. launchd restarts each time but the cycle repeats.

    Waiting on PR #55000. Downgrade to 2026.3.11 is the current workaround.

  9. openperf commented on Mar 27, 2026

    @openperf
    Member

    Quick update on #55000:

    Following @steipete's thoughtful review guidance, I've added a targeted regression test covering the three paths he outlined:

    1. abort → onAbort() fires, sets maxAttempts to 0, calls disconnect
    2. detachLifecycle() runs before gateway.disconnect() (the ordering bug this PR fixes)
    3. the synchronous "Max reconnect attempts (0)" error during disconnect no longer crashes the process

    The discord-specific CI job (extension-fast-discord) is passing green. The remaining CI failures are unrelated upstream type errors in src/agents/ — nothing to do with this fix.

    PR is ready for final review whenever @steipete or other maintainers have a moment.

  10. added a commit that references this issue on Mar 27, 2026
    e1f87a6
  11. breynol01 commented on Mar 27, 2026

    @breynol01

    Confirmed reproduced on macOS 25.3.0 arm64, v2026.3.24, Discord channel.

    Observed symptoms:

    • 746 log entries in a single day
    • Crash pattern: → → LaunchAgent respawns gateway → repeat every ~30 min

    Validation of root cause: Confirmed in provider-CAlWEl41.js:

    • Line 6952: onAbort sets gateway.options.reconnect = { maxAttempts: 0 }
    • Line 3317: handleReconnectionAttempt checks this.reconnectAttempts >= maxAttempts → 0 >= 0 → true → emits uncaught error

    Local workaround applied (pending upstream fix): patched handleReconnectionAttempt to check maxAttempts === 0 first and treat it as an intentional shutdown (silently call this.monitor.destroy() + return, no error emit).

    This aligns with suggested fix option 1 in the issue — adding a lifecycleStopping check in handleClose would be the cleaner upstream fix.

  12. openperf commented on Mar 28, 2026

    @openperf
    Member

    Sharing what I found from digging into this — hopefully it's useful for everyone here.

    The core issue is a race condition during Discord channel teardown: onAbort sets maxAttempts to 0 and then calls disconnect(), but Carbon throws the error synchronously, before the lifecycle has a chance to mark itself as shutting down. So the error flies out unhandled.

    This race condition actually existed before, but the gateway supervision refactor in v2026.3.24 changed the error propagation path in a way that the error is no longer caught locally — it goes straight to the global uncaughtException handler, which is why the whole gateway process crashes now instead of just the channel restarting.

    I've put together a fix in PR #55000 that adjusts the listener registration order during teardown so the supervisor detaches before Carbon's synchronous error can propagate.

    I also went through the issue tracker and this single root cause has generated 35+ duplicate issues in the past 48 hours alone (including #56057, #55873, #55618, #55554, #55367, #55350, #55346, #55340, #55301, #55296, #55274, #55260, #54894, #54851, #54752, among many others), so it looks like everyone here is running into the same thing.

    If anything needs to be changed, I'm happy to follow up.

  13. sudokrang commented on Mar 28, 2026

    @sudokrang

    Confirming same issue on v2026.3.24. Getting stale-socket restarts every ~35 minutes. Affects Discord AND Slack — both channels restart simultaneously. Log pattern:

    [discord:default] health-monitor: restarting (reason: stale-socket)
    [slack:default] health-monitor: restarting (reason: stale-socket)

    Both channels affected, not just Discord. Gateway keeps running but channels reconnect repeatedly.

  14. openperf commented on Mar 29, 2026

    @openperf
    Member

    @sudokrang Both channels restarting at the same time is the giveaway — Slack and Discord don't share any reconnect code, so a Discord stale-socket can't independently trigger a Slack restart. What's happening is the Discord abort path crashes the whole Node process (the uncaught exception in the issue description), and your process manager brings everything back up together. That's why it looks like both channels are affected when really it's just Discord taking the house down.
    #55000 fixes the root cause — the supervisor enters teardown before disconnect() fires, so the synchronous error gets swallowed instead of escaping as an uncaught exception. Once merged, Discord will restart cleanly on its own without dragging Slack along for the ride.

  15. 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.

  16. 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

    bugregression

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions