Repository navigation
fix(discord): suppress expected reconnect-exhausted shutdown errors - #55324
Conversation
Greptile SummaryThis 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 Confidence Score: 4/5Safe 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.
|
| 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)
-
extensions/discord/src/monitor/gateway-supervisor.ts, line 140-154 (link)Potential MaxListeners accumulation if emitter is long-lived
By keeping
onGatewayErrorattached indefinitely afterdispose(), each supervisor instance adds one permanent listener to the emitter it was created with. If the Carbon gateway plugin reuses the sameEventEmitteracross multiple supervisor lifetimes (e.g., repeated health-monitor restart cycles), listeners will accumulate and eventually trigger Node.js'sMaxListenersExceededWarning(default threshold: 10).The
disposedflag 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
fd737eb to
b5cdf31
Compare
|
Landed via temp rebase onto main. Thanks @joelnishanth! |
Summary
reconnect-exhaustedas expected during intentional lifecycle shutdown so startup-abort cleanup stops surfacing a false gateway failure#55373did not covermainby 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 scanWhy this PR still matters after #55373
#55373already fixed the supervisor-side late-error handling ingateway-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 bufferreconnect-exhausted, andprovider.lifecycle.tswould log/rethrow it fromdrainPendingGatewayErrors()even though shutdown was deliberate.Changes
extensions/discord/src/monitor/provider.lifecycle.tsreconnect-exhaustedat info level during intentional shutdownextensions/discord/src/monitor/provider.lifecycle.test.tssrc/memory/search-manager.test.tsextensions/memory-coreseam againsrc/memory/test-manager.tssrc/memory/test-manager-helpers.tssrc/plugin-sdk/memory-core.tssrc/plugin-sdk/channel-import-guardrails.test.tsTesting
pnpm checkpnpm buildpnpm test