fix: prevent SSE listener leak in console-logs stream#751
Open
zerray wants to merge 1 commit intodecolua:masterfrom
Open
fix: prevent SSE listener leak in console-logs stream#751zerray wants to merge 1 commit intodecolua:masterfrom
zerray wants to merge 1 commit intodecolua:masterfrom
Conversation
The ReadableStream cancel() callback is not reliably invoked on client disconnect under Next.js, causing emitter listeners (line/clear) and the keepalive interval to accumulate, eventually triggering MaxListenersExceededWarning. Use request.signal as the primary disconnect trigger, with cancel() and enqueue failures as fallbacks. Cleanup is idempotent via state.closed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MaxListenersExceededWarning: Possible EventTarget memory leak detected. 51 line listeners added to [EventEmitter](same forclear) appears after many client reconnects on the console-logs SSE endpoint. Node memory grows slowly alongside the listener count.Root cause
src/app/api/translator/console-logs/stream/route.jsattachesline/clearlisteners on the singletonconsoleEmitterinsideReadableStream.start(), and only removes them incancel(). In Next.js 16 / Node fetch runtime,ReadableStream.cancel()is not reliably invoked when the client aborts the connection (tab close, navigation, reload), so listeners accumulate every time the dashboard reconnects. The buffered-logs emitter hassetMaxListeners(50), so the warning fires on the 51st leaked connection.request.signal(theAbortSignalon theRequest) does fire on client disconnect — it is the authoritative disconnect source here.Fix
request.signal.addEventListener("abort", cleanup, { once: true })as the primary disconnect hook.cancel()as a fallback.state.closedguard so abort, cancel, and enqueue-failure paths can all call it safely.emitter.off(...)+clearInterval(keepalive)inside the singlecleanup()function.No behavior change for clients; only server-side listener bookkeeping is affected. The
usage/streamSSE route has the same pattern and is a candidate for the same fix in a follow-up — this PR keeps the change minimal and focused on the one route where the warning is observed.Test plan
listenerCount("line")on the emitter should return to ~1 after each client disconnect, not accumulateMaxListenersExceededWarningin server logsclearaction still reaches connected clients🤖 Generated with Claude Code