Repository navigation
fix(discord): guard gateway cleanup races - #55373
Conversation
Greptile SummaryThis PR fixes two classes of gateway cleanup races in the Discord monitor: late Key changes:
Confidence Score: 5/5Safe to merge — the changes are targeted, well-tested, and correct; the only open item is a minor cosmetic redundancy. All three fixes (post-dispose error logging, listener retention, and startup-path gateway disconnect) are logically sound and directly tested by the new regression cases. The removed removeListener call is intentional and prevents the original crash. No behavioural regression paths were introduced. No files require special attention; the single P2 comment on gateway-supervisor.ts is cosmetic and non-blocking.
|
| Filename | Overview |
|---|---|
| extensions/discord/src/monitor/gateway-supervisor.ts | Keeps error listener active after dispose so late reconnect-exhausted errors are logged rather than crashing the process; minor redundant disposed boolean remains. |
| extensions/discord/src/monitor/provider.ts | Hoists lifecycleGateway to outer scope and adds disconnect call in finally block for startup-failure cleanup path. |
| extensions/discord/src/monitor/gateway-supervisor.test.ts | Adds regression test verifying post-dispose errors are logged rather than thrown. |
| extensions/discord/src/monitor/provider.test.ts | Adds regression test verifying gateway disconnect is called when startup fails before lifecycle begins. |
Comments Outside Diff (1)
-
extensions/discord/src/monitor/gateway-supervisor.ts, line 89 (link)Redundant
disposedboolean after refactorAfter this PR,
disposedis only used as an idempotency guard insidedispose()itself. TheonGatewayErrorhandler now usesphase === "disposed"for the same check, makingdisposedandphasecarry duplicate state. The guard indispose()could be written asif (phase === "disposed") { return; }anddisposedcould be removed entirely to reduce confusion for future maintainers.Not a bug — just redundant state that might raise a "why does this exist?" question later.
Prompt To Fix With AI
This is a comment left during a code review. Path: extensions/discord/src/monitor/gateway-supervisor.ts Line: 89 Comment: **Redundant `disposed` boolean after refactor** After this PR, `disposed` is only used as an idempotency guard inside `dispose()` itself. The `onGatewayError` handler now uses `phase === "disposed"` for the same check, making `disposed` and `phase` carry duplicate state. The guard in `dispose()` could be written as `if (phase === "disposed") { return; }` and `disposed` could be removed entirely to reduce confusion for future maintainers. Not a bug — just redundant state that might raise a "why does this exist?" question later. 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: 89
Comment:
**Redundant `disposed` boolean after refactor**
After this PR, `disposed` is only used as an idempotency guard inside `dispose()` itself. The `onGatewayError` handler now uses `phase === "disposed"` for the same check, making `disposed` and `phase` carry duplicate state. The guard in `dispose()` could be written as `if (phase === "disposed") { return; }` and `disposed` could be removed entirely to reduce confusion for future maintainers.
Not a bug — just redundant state that might raise a "why does this exist?" question later.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(discord): guard gateway cleanup race..." | Re-trigger Greptile
9a20e48 to
9423941
Compare
9423941 to
6815340
Compare
|
Landed via temp rebase onto main.
Thanks @Takhoffman! |
🔒 Aisle Security AnalysisWe found 2 potential security issue(s) in this PR:
Vulnerabilities1. 🟡 EventEmitter 'error' listener not removed on dispose in Discord gateway supervisor (memory/log leak)
DescriptionThe Discord gateway supervisor attaches an Impact:
Vulnerable code: emitter.on("error", onGatewayError);
...
dispose: () => {
...
phase = "disposed";
pending.length = 0;
// missing: emitter.removeListener("error", onGatewayError)
}RecommendationRemove the listener during disposal, while still preventing uncaught Example fix: // when constructing
emitter.on("error", onGatewayError);
dispose: () => {
if (disposed) return;
disposed = true;
lifecycleHandler = undefined;
phase = "disposed";
pending.length = 0;
emitter.removeListener("error", onGatewayError);
}If you need to suppress errors that occur during shutdown, consider:
2. 🔵 Terminal/log injection risk from unsanitized Discord gateway error message in late-dispose logging
Description
Vulnerable code: params.runtime.error?.(
danger(
`discord: suppressed late gateway ${event.type} error after dispose: ${event.message}`,
),
);RecommendationSanitize error strings before logging.
Example: import stripAnsi from "strip-ansi";
const sanitizeForLogs = (s: string) =>
stripAnsi(s).replace(/[\u0000-\u001F\u007F]/g, " ");
params.runtime.error?.(
danger(
`discord: suppressed late gateway ${event.type} error after dispose: ${sanitizeForLogs(event.message)}`,
),
);
// Or structured:
runtime.error?.({
msg: "discord: suppressed late gateway error after dispose",
type: event.type,
message: sanitizeForLogs(event.message),
});Analyzed PR: #55373 at commit Last updated on: 2026-03-26T23:14:54Z |
Summary
Testing