openclaw - 💡(How to fix) Fix Gateway crash: race condition causes maxAttempts=0 exception on disconnect [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#59927Fetched 2026-04-08 02:38:45
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

Error Message

Gateway disconnected: Max reconnect attempts (0) reached
C [ERROR] Gateway disconnect error: Error: Max reconnect attempts (0) reached

Root Cause

Race condition in SafeGatewayPlugin (or Carbon GatewayPlugin):

  1. maxAttempts defaults to 0 as a class property
  2. Constructor sets it to 5 via config — but disconnect events can fire before constructor completes
  3. When disconnect fires with maxAttempts === 0, maxAttempts === 0 check fails (because line 304 just returns early), BUT the disconnect handler still tries to reconnect and throws: throw new Error(Max reconnect attempts (${maxAttempts}) reached)

The safe disconnect check on line 304:

if (maxAttempts === 0) return;

...returns silently. But the actual disconnect event handler later calls reconnect logic which hits the throw:

throw new Error(`Max reconnect attempts (${maxAttempts}) reached`);

There are TWO code paths — one guards, one doesn't.

Fix Action

Fix

Either:

  1. Initialize maxAttempts = 5 as class property (instead of 0), OR
  2. Add the same guard to the disconnect handler that actually throws, OR
  3. Ensure config is fully loaded before any gateway events can fire

Code Example

Gateway disconnected: Max reconnect attempts (0) reached
C [ERROR] Gateway disconnect error: Error: Max reconnect attempts (0) reached

---

if (maxAttempts === 0) return;

---

throw new Error(`Max reconnect attempts (${maxAttempts}) reached`);
RAW_BUFFERClick to expand / collapse

Problem

The Discord gateway crashes with an uncaught exception when a disconnect event fires before the reconnect config is fully initialized.

Error

Gateway disconnected: Max reconnect attempts (0) reached
C [ERROR] Gateway disconnect error: Error: Max reconnect attempts (0) reached

Root Cause

Race condition in SafeGatewayPlugin (or Carbon GatewayPlugin):

  1. maxAttempts defaults to 0 as a class property
  2. Constructor sets it to 5 via config — but disconnect events can fire before constructor completes
  3. When disconnect fires with maxAttempts === 0, maxAttempts === 0 check fails (because line 304 just returns early), BUT the disconnect handler still tries to reconnect and throws: throw new Error(Max reconnect attempts (${maxAttempts}) reached)

The safe disconnect check on line 304:

if (maxAttempts === 0) return;

...returns silently. But the actual disconnect event handler later calls reconnect logic which hits the throw:

throw new Error(`Max reconnect attempts (${maxAttempts}) reached`);

There are TWO code paths — one guards, one doesn't.

Fix

Either:

  1. Initialize maxAttempts = 5 as class property (instead of 0), OR
  2. Add the same guard to the disconnect handler that actually throws, OR
  3. Ensure config is fully loaded before any gateway events can fire

Impact

  • Gateway crashes roughly every 35 minutes
  • Requires OpenClaw restart to recover
  • No auto-restart mechanism recovers from this specific crash

Environment

  • OpenClaw: OpenClaw 2026.3.24 (cff6dc9)
  • Node: v25.8.1
  • OS: macOS Darwin 23.6.0
  • Channel: Discord

extent analysis

TL;DR

Initialize maxAttempts to a non-zero value or add a guard to the disconnect handler to prevent the gateway from crashing due to a race condition.

Guidance

  • Verify that the maxAttempts property is being set to a non-zero value before the disconnect event fires by checking the constructor and config initialization order.
  • Consider adding a temporary guard to the disconnect handler to prevent it from throwing an error when maxAttempts is zero, similar to the existing guard on line 304.
  • Review the config loading process to ensure it completes before any gateway events can fire, potentially by using a callback or promise to synchronize the initialization.
  • Test the fix by simulating a disconnect event before the reconnect config is fully initialized to verify that the gateway no longer crashes.

Example

// Initialize maxAttempts to a non-zero value as a class property
class SafeGatewayPlugin {
  maxAttempts = 5;
  // ...
}

Notes

The fix may require additional testing to ensure that the gateway behaves correctly in all scenarios, including when the config is loaded after the disconnect event fires.

Recommendation

Apply workaround by initializing maxAttempts to a non-zero value or adding a guard to the disconnect handler, as this is a more straightforward and immediate solution to prevent the gateway from crashing.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING