-
Notifications
You must be signed in to change notification settings - Fork 616
fix(js/plugins/google-genai): prevent potential server crash from unhandled stream errors #4005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -394,12 +394,18 @@ async function* generateResponseSequence( | |
| stream: ReadableStream<GenerateContentResponse> | ||
| ): AsyncGenerator<GenerateContentResponse> { | ||
| const reader = stream.getReader(); | ||
| while (true) { | ||
| const { value, done } = await reader.read(); | ||
| if (done) { | ||
| break; | ||
| try { | ||
| while (true) { | ||
| const { value, done } = await reader.read(); | ||
| if (done) { | ||
| break; | ||
| } | ||
| yield value; | ||
| } | ||
| yield value; | ||
| } catch (error) { | ||
| throw error; | ||
| } finally { | ||
| reader.releaseLock(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -408,12 +414,22 @@ async function getResponsePromise( | |
| ): Promise<GenerateContentResponse> { | ||
| const allResponses: GenerateContentResponse[] = []; | ||
| const reader = stream.getReader(); | ||
| while (true) { | ||
| const { done, value } = await reader.read(); | ||
| if (done) { | ||
| try { | ||
| while (true) { | ||
| const { done, value } = await reader.read(); | ||
| if (done) { | ||
| return aggregateResponses(allResponses); | ||
| } | ||
| allResponses.push(value); | ||
| } | ||
| } catch (error) { | ||
| if (allResponses.length) { | ||
| return aggregateResponses(allResponses); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While returning a partial response on stream error is a reasonable strategy, the original error is completely swallowed here. This can make debugging difficult for the library user, as they would receive an incomplete response without any indication of what went wrong. Consider logging the error before returning the aggregated responses to provide visibility into the underlying issue. For example: // Inside the catch block
if (allResponses.length) {
console.error('Stream processing failed. Returning partial response. Error:', error);
return aggregateResponses(allResponses);
} |
||
| } | ||
| allResponses.push(value); | ||
|
|
||
| throw error; | ||
| } finally { | ||
| reader.releaseLock(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
catchblock is redundant as it just re-throws the error. An error thrown within thetryblock will propagate automatically from the async generator. You can simplify the code by removing thecatchblock.