fix(stream): prevent ERR_INVALID_STATE when pushing to closed stream#1894
Open
GdMacmillan wants to merge 5 commits intolangchain-ai:mainfrom
Open
fix(stream): prevent ERR_INVALID_STATE when pushing to closed stream#1894GdMacmillan wants to merge 5 commits intolangchain-ai:mainfrom
GdMacmillan wants to merge 5 commits intolangchain-ai:mainfrom
Conversation
Add a guard check in IterableReadableWritableStream.push() to verify the stream is not closed before attempting to enqueue data. This prevents the "Controller is already closed" TypeError that occurs when StreamMessagesHandler tries to emit tokens after the stream consumer has completed. Fixes langchain-ai#1837 Co-Authored-By: Claude Opus 4.5 <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
Add a guard check in
IterableReadableWritableStream.push()to verify the stream is not closed before attempting to enqueue data.Problem
When using
graph.stream()withstreamMode: ['messages'], aTypeError [ERR_INVALID_STATE]: Invalid state: Controller is already closedoccurs if the stream consumer completes before the LLM finishes generating tokens.This happens because
IterableReadableWritableStream.push()doesn't check the_closedflag before callingcontroller.enqueue().Solution
Add a simple guard:
if (this._closed) return;at the start of thepush()method.The
_closedflag already exists and is set by theclose()method - it just wasn't being checked inpush().Testing
Fixes #1837