Skip to content

fix(discord): treat reconnect-exhausted as graceful stop, not crash - #55443

Closed
lml2468 wants to merge 1 commit into
openclaw:mainfrom
lml2468:fix/issue-55421
Closed

lml2468 wants to merge 1 commit into
openclaw:mainfrom
lml2468:fix/issue-55421

Conversation

@lml2468

@lml2468 lml2468 commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixes Discord WebSocket crash: 'Max reconnect attempts (0) reached after code 1005' since 2026.3.24 #55421 — Discord WebSocket crash: "Max reconnect attempts (0) reached after code 1005"
  • reconnect-exhausted events in drainPendingGatewayErrors() now always return "stop" instead of conditionally throwing based on lifecycleStopping
  • Root cause: race condition where the health monitor's onAbort() sets maxAttempts=0 and disconnects, but lifecycleStopping isn't set until the finally block — so the queued reconnect-exhausted event would throw an uncaught exception

Changes

In extensions/discord/src/monitor/provider.lifecycle.ts:

  • Removed lifecycleStopping && guard from the reconnect-exhausted check in drainPendingGatewayErrors(), so reconnect-exhausted always gracefully stops the lifecycle instead of crashing
  • The health monitor already handles reconnection — throwing here was never the right behavior

In extensions/discord/src/monitor/provider.lifecycle.test.ts:

  • Updated the "reconnect-exhausted queued before shutdown" test to expect graceful resolution instead of a thrown error

Test plan

  • pnpm test -- extensions/discord/src/monitor/gateway-supervisor.test.ts extensions/discord/src/monitor/provider.lifecycle.test.ts — 25/25 pass
  • All pre-commit checks pass (format, lint, typecheck, boundary checks)

Fixes openclaw#55421

When Discord WS drops with code 1005 (No Status Received), Carbon
emits "Max reconnect attempts (0) reached". This was crashing the
gateway via uncaught exception because the `lifecycleStopping` flag
hadn't been set yet (race between health-monitor abort and lifecycle
teardown).

reconnect-exhausted should always stop the lifecycle gracefully and
let the channel health monitor handle reconnection. Never throw.
@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: XS labels Mar 27, 2026
@greptile-apps

greptile-apps Bot commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a race condition in the Discord gateway lifecycle where a reconnect-exhausted event queued by the health monitor could crash the provider with an uncaught exception. The root cause was that drainPendingGatewayErrors() required lifecycleStopping=true before treating reconnect-exhausted as a graceful stop, but lifecycleStopping isn't set until the finally block — after the drain has already run.

Changes:

  • provider.lifecycle.ts: Removes the lifecycleStopping && guard from the reconnect-exhausted check in drainPendingGatewayErrors, so all reconnect-exhausted events result in a graceful stop regardless of when they are drained.
  • provider.lifecycle.test.ts: Updates the corresponding test to expect graceful resolution, adds expectLifecycleCleanup assertions, and retains the runtimeError assertion to confirm the event is still observable in logs.

One minor note: handleGatewayEvent itself still guards the info-level "ignoring expected" log path with lifecycleStopping &&. In the race-condition scenario (the exact bug being fixed), reconnect-exhausted now causes a graceful stop but the error is still emitted at danger/error level because the event falls through to the generic error log. The lifecycle is correct; it's purely a cosmetic/observability concern about log noise.

Confidence Score: 5/5

Safe to merge — targeted one-line logic fix with direct test coverage, no behavioral regressions.

The change is minimal and precisely addresses the described race condition. The test suite explicitly verifies both the graceful resolution and the error log. The only open item is a cosmetic log-level inconsistency (P2) that does not affect correctness or reliability.

No files require special attention beyond the minor logging inconsistency in handleGatewayEvent.

Important Files Changed

Filename Overview
extensions/discord/src/monitor/provider.lifecycle.ts Removes the lifecycleStopping && guard from the reconnect-exhausted check in drainPendingGatewayErrors, fixing a race condition where the health monitor could queue this event before lifecycleStopping was set, causing a crash. One minor inconsistency: handleGatewayEvent still guards the info-log path with lifecycleStopping, so the race-condition case still emits at error level despite a graceful exit.
extensions/discord/src/monitor/provider.lifecycle.test.ts Test updated to expect graceful resolution instead of rejection; adds expectLifecycleCleanup assertions to verify full teardown, and explicitly checks that runtimeError is still called so the event is observable in production logs.

Comments Outside Diff (1)

  1. extensions/discord/src/monitor/provider.lifecycle.ts, line 70-75 (link)

    P2 Inconsistent log level for race-condition path

    handleGatewayEvent still has the lifecycleStopping && guard, so when the race condition fires (lifecycleStopping=false but reconnect-exhausted is queued), the event falls through to the danger() error log on line 76 instead of the quieter info log here. The lifecycle now exits gracefully in that case (the whole point of this PR), but an error-level message is still emitted — which could create false production alarms.

    The test explicitly asserts runtimeError is called in this scenario, so it appears intentional for now, but it's worth considering whether the lifecycleStopping && guard should also be dropped here (or an unconditional info-log added for reconnect-exhausted) so the log level reflects the actual severity (graceful stop, not crash).

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: extensions/discord/src/monitor/provider.lifecycle.ts
    Line: 70-75
    
    Comment:
    **Inconsistent log level for race-condition path**
    
    `handleGatewayEvent` still has the `lifecycleStopping &&` guard, so when the race condition fires (`lifecycleStopping=false` but `reconnect-exhausted` is queued), the event falls through to the `danger()` error log on line 76 instead of the quieter info log here. The lifecycle now exits gracefully in that case (the whole point of this PR), but an error-level message is still emitted — which could create false production alarms.
    
    The test explicitly asserts `runtimeError` is called in this scenario, so it appears intentional for now, but it's worth considering whether the `lifecycleStopping &&` guard should also be dropped here (or an unconditional info-log added for `reconnect-exhausted`) so the log level reflects the actual severity (graceful stop, not crash).
    
    
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: extensions/discord/src/monitor/provider.lifecycle.ts
Line: 70-75

Comment:
**Inconsistent log level for race-condition path**

`handleGatewayEvent` still has the `lifecycleStopping &&` guard, so when the race condition fires (`lifecycleStopping=false` but `reconnect-exhausted` is queued), the event falls through to the `danger()` error log on line 76 instead of the quieter info log here. The lifecycle now exits gracefully in that case (the whole point of this PR), but an error-level message is still emitted — which could create false production alarms.

The test explicitly asserts `runtimeError` is called in this scenario, so it appears intentional for now, but it's worth considering whether the `lifecycleStopping &&` guard should also be dropped here (or an unconditional info-log added for `reconnect-exhausted`) so the log level reflects the actual severity (graceful stop, not crash).

```suggestion
    if (event.type === "reconnect-exhausted") {
      params.runtime.log?.(
        `discord: reconnect-exhausted (graceful stop): ${event.message}`,
      );
      return "stop";
    }
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(discord): treat reconnect-exhausted ..." | Re-trigger Greptile

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a058b3792f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +91 to 92
if (event.type === "disallowed-intents" || event.type === "reconnect-exhausted") {
return "stop";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Do not swallow reconnect-exhausted during startup drain

Returning "stop" for reconnect-exhausted here can strand the lifecycle when that event is drained inside ensureStartupReady()’s beforePoll path: drainPending clears the queued event, ensureStartupReady() returns without throwing, the post-startup drain sees nothing, and waitForDiscordGatewayStop() can then block indefinitely if no further gateway event arrives. Before this change the same startup failure propagated as an error and triggered the normal restart path immediately; now a real reconnect exhaustion can leave the account stuck in a running-but-disconnected state until an external abort/health-monitor intervention.

Useful? React with 👍 / 👎.

@lml2468

lml2468 commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

Closing: duplicate of #55177 which fixes the same issue (#55116/#55421). The fix is already pending review.

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.

1 participant