fix: release stream state on end to prevent memory leaks#943
Open
Gummygamer wants to merge 1 commit intoanthropics:mainfrom
Open
fix: release stream state on end to prevent memory leaks#943Gummygamer wants to merge 1 commit intoanthropics:mainfrom
Gummygamer wants to merge 1 commit intoanthropics:mainfrom
Conversation
When stream instances are retained after completion (e.g. Claude Code retains all yielded BetaMessageStream objects from BetaToolRunner in a React ref), the `messages` and `receivedMessages` arrays accumulate the full conversation history for the entire session lifetime, causing unbounded O(n²) memory growth. This fix caches the final message before emitting 'end', then clears `messages`, `receivedMessages`, `#currentMessageSnapshot`, `#params`, and `#listeners` once the 'end' event listeners have been called. The public `finalMessage()` and `finalText()` methods continue to work via `#cachedFinalMessage`. Observed impact: Claude Code sessions growing from 2GB → 20GB+ within a single session when using tool-heavy workflows.
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
MessageStreamandBetaMessageStreamaccumulate all data inmessagesandreceivedMessagesarrays for the entire lifetime of the stream objectBetaToolRunneryields a newBetaMessageStreamper iteration; if the caller retains all yielded streams, each holds a copy of the full message history at that point — O(n²) totalFix
Cache the final message in
_emitFinal()(before the'end'event fires), then after all'end'listeners are called, clear:messages(copy of all API params — grows with conversation)receivedMessages(accumulates all API responses)#currentMessageSnapshot,#params,#listenersfinalMessage()andfinalText()continue to work via#cachedFinalMessage.Observed impact
Claude Code sessions growing from 2GB → 20GB+ in a single session when using tool-heavy workflows. The process had to be restarted repeatedly due to OOM. The root cause was identified by extracting embedded JS from the Claude Code binary and tracing the retention chain:
BetaToolRunneryields a newBetaMessageStreamper iterationmessages(copy of all params at that iteration) +receivedMessagesparams.messagesin the runner but old stream copies are unaffectedTest plan
finalMessage()resolves correctly afterendfinalText()resolves correctly afterend'end'listeners) still fire before cleanup