Cancel the generation when a response stream's consumer stops early - #202
Open
james-333i wants to merge 1 commit into
Open
Cancel the generation when a response stream's consumer stops early#202james-333i wants to merge 1 commit into
james-333i wants to merge 1 commit into
Conversation
streamResponse wraps the model's stream in a relay that appends the response to the transcript when generation completes. The relay had no termination handler, so a consumer that stopped iterating early — a break, a cancelled task, or a dropped stream — left the relay consuming and the backend generating to its token limit in the background. That ghost generation kept the model and its growing KV cache alive for tens of seconds after the response visibly ended. Cancel the relay task when the stream terminates by cancellation. The cancellation propagates through the upstream stream's own termination handler to the backend's generation task, releasing the model promptly. Natural completion still finishes the relay and records the transcript entry as before.
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.
streamResponse wraps the model's stream in a relay that appends the response to the transcript when generation completes. The relay had no termination handler, so a consumer that stopped iterating early, whether by breaking out of the loop, cancelling its task, or dropping the stream, left the relay consuming and the backend generating to its token limit in the background. That ghost generation kept the model weights and a growing KV cache alive for tens of seconds after the response visibly ended, which surfaces as multi-gigabyte memory spikes and delayed releases on device.
Cancel the relay task when the stream terminates by cancellation. The cancellation propagates through the upstream stream's own termination handler to the backend's generation task, releasing the model promptly. Natural completion still finishes the relay and records the transcript entry exactly as before.
Diagnosed from a memory graph capture showing the model module retained by two suspended task stacks (the relay and the backend generation) long after the consumer had stopped.