Skip to content

fix(discord): suppress expected reconnect-exhausted shutdown errors - #55324

Merged
steipete merged 2 commits into
openclaw:mainfrom
joelnishanth:fix/discord-stale-socket-crash-55274
Mar 26, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
joelnishanth:fix/discord-stale-socket-crash-55274

Conversation

@joelnishanth

@joelnishanth joelnishanth commented Mar 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • treat Discord reconnect-exhausted as expected during intentional lifecycle shutdown so startup-abort cleanup stops surfacing a false gateway failure
  • keep the regression focused on the buffered pre-lifecycle window that #55373 did not cover
  • repair unrelated red tests on current main by fixing the moved memory search-manager mock seam and routing memory test helpers through the plugin-sdk bridge, plus exclude test harness files from the core-production guardrail scan

Why this PR still matters after #55373

#55373 already fixed the supervisor-side late-error handling in gateway-supervisor.ts, so that overlap was dropped from this branch.

The remaining bug is earlier: if the lifecycle is intentionally stopping before waitForDiscordGatewayStop() attaches the active lifecycle handler, Carbon can still buffer reconnect-exhausted, and provider.lifecycle.ts would log/rethrow it from drainPendingGatewayErrors() even though shutdown was deliberate.

Changes

  • extensions/discord/src/monitor/provider.lifecycle.ts
    • log buffered reconnect-exhausted at info level during intentional shutdown
    • return cleanly instead of rethrowing the expected shutdown event
  • extensions/discord/src/monitor/provider.lifecycle.test.ts
    • add a regression covering the startup-abort / buffered-event window
  • src/memory/search-manager.test.ts
    • point the runtime mock at the moved extensions/memory-core seam again
  • src/memory/test-manager.ts
  • src/memory/test-manager-helpers.ts
  • src/plugin-sdk/memory-core.ts
    • use the plugin-sdk bridge for memory test helpers instead of direct extension private-src imports
  • src/plugin-sdk/channel-import-guardrails.test.ts
    • skip test-only harness/helper files when enforcing the core-production import guardrail

Testing

  • pnpm check
  • pnpm build
  • pnpm test

@greptile-apps

greptile-apps Bot commented Mar 26, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a production crash in the Discord gateway monitor where a stale-socket reconnect cycle could kill the entire gateway process. Two complementary issues are addressed: Carbon firing a reconnect-exhausted error event asynchronously after gatewaySupervisor.dispose() removed the only error listener (triggering Node's uncaught-exception mechanism), and the resulting error being incorrectly classified as fatal during an intentional shutdown.\n\nChanges\n- gateway-supervisor.ts: dispose() no longer calls emitter.removeListener(). The existing disposed guard in onGatewayError turns the listener into a safe noop, so late Carbon error events are absorbed rather than thrown as uncaught exceptions.\n- provider.lifecycle.ts: Both handleGatewayEvent and drainPendingGatewayErrors now recognise reconnect-exhausted during an intentional shutdown (lifecycleStopping=true) as an expected condition — logged at info level and resolved with "stop" instead of re-thrown. This mirrors the existing disallowed-intents pattern.\n- Tests added for both scenarios and all pass.\n\nNotes\n- The only non-blocking concern is that keeping the error listener permanently attached means each supervisor instance adds one permanent listener to the emitter. If Carbon reuses the same EventEmitter across repeated health-monitor restart cycles, listeners will accumulate over time and may eventually trigger MaxListenersExceededWarning. This is worth verifying against Carbon's emitter lifecycle.

Confidence Score: 4/5

Safe to merge; the fix correctly prevents a real crash and is well-tested, with only a minor potential listener-accumulation concern depending on the Carbon emitter lifecycle.

Both root causes are addressed correctly and the logic is consistent with existing patterns (disallowed-intents). Tests directly reproduce the production failure scenario. The one P2 concern — permanent listener attachment potentially accumulating on a long-lived emitter — does not affect correctness but could trigger MaxListenersExceededWarning if supervisors are recycled against the same emitter; worth a quick check before or after merge.

extensions/discord/src/monitor/gateway-supervisor.ts — verify that Carbon recreates the EventEmitter on each gateway reconnect (or that supervisor lifetime is correlated with emitter lifetime) to rule out listener accumulation.

Important Files Changed

Filename Overview
extensions/discord/src/monitor/gateway-supervisor.ts dispose() no longer removes the error listener; the disposed flag makes the handler a safe noop, preventing uncaught-exception crashes from late Carbon error events. Minor concern: if the emitter is long-lived across multiple supervisor instances, permanent listeners may accumulate.
extensions/discord/src/monitor/provider.lifecycle.ts handleGatewayEvent and drainPendingGatewayErrors now treat reconnect-exhausted as an expected no-error condition during intentional shutdown (lifecycleStopping=true), logging at info level and returning stop without throwing. Logic is sound and consistent with the existing disallowed-intents pattern.
extensions/discord/src/monitor/gateway-supervisor.test.ts New test directly covers the crash scenario: emits an error after dispose() and asserts it does not throw. Accurately reproduces the production failure mode.
extensions/discord/src/monitor/provider.lifecycle.test.ts New lifecycle test correctly sequences: start lifecycle, push reconnect-exhausted to pending queue, abort, then assert promise resolves cleanly with an info-level log and no error log.

Comments Outside Diff (1)

  1. extensions/discord/src/monitor/gateway-supervisor.ts, line 140-154 (link)

    P2 Potential MaxListeners accumulation if emitter is long-lived

    By keeping onGatewayError attached indefinitely after dispose(), each supervisor instance adds one permanent listener to the emitter it was created with. If the Carbon gateway plugin reuses the same EventEmitter across multiple supervisor lifetimes (e.g., repeated health-monitor restart cycles), listeners will accumulate and eventually trigger Node.js's MaxListenersExceededWarning (default threshold: 10).

    The disposed flag correctly makes each stale listener a no-op, so there's no functional bug — but the warning could obscure real issues.

    If the emitter does outlive individual supervisor instances, consider calling emitter.setMaxListeners(emitter.getMaxListeners() + 1) on attach and logging a note, or auditing whether Carbon recreates the emitter on each gateway reconnect (in which case this is a non-issue).

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: extensions/discord/src/monitor/gateway-supervisor.ts
    Line: 140-154
    
    Comment:
    **Potential MaxListeners accumulation if emitter is long-lived**
    
    By keeping `onGatewayError` attached indefinitely after `dispose()`, each supervisor instance adds one permanent listener to the emitter it was created with. If the Carbon gateway plugin reuses the same `EventEmitter` across multiple supervisor lifetimes (e.g., repeated health-monitor restart cycles), listeners will accumulate and eventually trigger Node.js's `MaxListenersExceededWarning` (default threshold: 10).
    
    The `disposed` flag correctly makes each stale listener a no-op, so there's no functional bug — but the warning could obscure real issues.
    
    If the emitter does outlive individual supervisor instances, consider calling `emitter.setMaxListeners(emitter.getMaxListeners() + 1)` on attach and logging a note, or auditing whether Carbon recreates the emitter on each gateway reconnect (in which case this is a non-issue).
    
    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/gateway-supervisor.ts
Line: 140-154

Comment:
**Potential MaxListeners accumulation if emitter is long-lived**

By keeping `onGatewayError` attached indefinitely after `dispose()`, each supervisor instance adds one permanent listener to the emitter it was created with. If the Carbon gateway plugin reuses the same `EventEmitter` across multiple supervisor lifetimes (e.g., repeated health-monitor restart cycles), listeners will accumulate and eventually trigger Node.js's `MaxListenersExceededWarning` (default threshold: 10).

The `disposed` flag correctly makes each stale listener a no-op, so there's no functional bug — but the warning could obscure real issues.

If the emitter does outlive individual supervisor instances, consider calling `emitter.setMaxListeners(emitter.getMaxListeners() + 1)` on attach and logging a note, or auditing whether Carbon recreates the emitter on each gateway reconnect (in which case this is a non-issue).

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

Reviews (1): Last reviewed commit: "discord: fix stale-socket reconnect cras..." | Re-trigger Greptile

@steipete
steipete force-pushed the fix/discord-stale-socket-crash-55274 branch from fd737eb to b5cdf31 Compare March 26, 2026 23:27
@steipete steipete changed the title fix(discord): prevent gateway crash on stale-socket reconnect-exhausted fix(discord): suppress expected reconnect-exhausted shutdown errors Mar 26, 2026
@steipete
steipete merged commit e46e655 into openclaw:main Mar 26, 2026
7 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Landed via temp rebase onto main.

  • Gate: pnpm check && pnpm build && pnpm test
  • Land commit: a5819fd
  • Merge commit: e46e655

Thanks @joelnishanth!

pxnt pushed a commit to pxnt/openclaw that referenced this pull request Mar 27, 2026
godlin-gh pushed a commit to YouMindInc/openclaw that referenced this pull request Mar 27, 2026
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
Nachx639 pushed a commit to Nachx639/clawdbot that referenced this pull request Jun 17, 2026
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request Sep 29, 2026
lhfer pushed a commit to lhfer/openclaw that referenced this pull request Oct 11, 2026
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: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants