Skip to content

Handle ERR_STREAM_PREMATURE_CLOSE as a retryable error#25360

Open
RbBtSn0w wants to merge 1 commit intogoogle-gemini:mainfrom
RbBtSn0w:main
Open

Handle ERR_STREAM_PREMATURE_CLOSE as a retryable error#25360
RbBtSn0w wants to merge 1 commit intogoogle-gemini:mainfrom
RbBtSn0w:main

Conversation

@RbBtSn0w
Copy link
Copy Markdown

@RbBtSn0w RbBtSn0w commented Apr 14, 2026

Added ERR_STREAM_PREMATURE_CLOSE to RETRYABLE_NETWORK_CODES to automatically recover from premature stream closures during long generation.

  • test(core): add regression test for ERR_STREAM_PREMATURE_CLOSE with large content

Summary

#This PR resolves the frequent ERR_STREAM_PREMATURE_CLOSE errors encountered during long-form content generation or large-payload sessions. By categorizing this specific Node.js error code as a retryable network exception, we activate the system's built-in
"retry-with-context" mechanism, significantly improving the reliability of streaming interactions.

Details

  • Core Change: Added ERR_STREAM_PREMATURE_CLOSE to the RETRYABLE_NETWORK_CODES list in packages/core/src/utils/retry.ts.
  • Technical Context: This error typically occurs when the HTTP response headers are received, but the underlying socket is closed before the full stream body is consumed (common in unstable networks or long-lived HTTP/2 streams).
  • Architectural Rational:
    • Since the Gemini API is stateless and the CLI maintains the full conversation Context, re-initiating the request with the existing history is the most effective application-layer self-healing path for stream interruptions.
    • This change is surgical and safe, as it primarily affects idempotent generation requests and does not introduce side effects for non-idempotent tools.
  • AI Review Synthesis: This solution has passed 10 consecutive rounds of deep AI code review, confirming its correctness, performance, and adherence to engineering standards.

Related Issues

#25253

How to Validate

The fix has been rigorously verified using a TDD (Test-Driven Development) workflow:

  1. Reproduction (RED): Created a test case simulating a complex 10KB session history that fails mid-stream (after 20 chunks) with ERR_STREAM_PREMATURE_CLOSE. Verified that the system crashed without the fix.
  2. Verification (GREEN): Applied the fix and confirmed that the same test case now automatically triggers a retry and successfully completes the generation.
  3. Regression Guard: Permanently added the reproduction case to the official test suite.

Validation Command:
1 npm test -w @google/gemini-cli-core -- src/core/geminiChat_network_retry.test.ts
Expected Result: All 8 network-related retry tests (including the new regression test) pass successfully.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • [ x ] Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • [ x ] Validated on required platforms/methods:
    • [ x ] MacOS
      • [ x ] npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

Added ERR_STREAM_PREMATURE_CLOSE to RETRYABLE_NETWORK_CODES to automatically
recover from premature stream closures during long generation.

* test(core): add regression test for ERR_STREAM_PREMATURE_CLOSE with large content
@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 14, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@RbBtSn0w RbBtSn0w marked this pull request as ready for review April 14, 2026 04:57
@RbBtSn0w RbBtSn0w requested a review from a team as a code owner April 14, 2026 04:57
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the reliability of streaming interactions by allowing the system to automatically recover from premature stream closures. By classifying this specific Node.js error as a retryable event, the application can now gracefully re-initiate requests that fail due to unstable network conditions or long-lived HTTP/2 streams, ensuring a smoother user experience for large-payload sessions.

Highlights

  • Error Handling Improvement: Added ERR_STREAM_PREMATURE_CLOSE to the list of retryable network codes to ensure robust recovery during long-form content generation.
  • Regression Testing: Implemented a new test case in geminiChat_network_retry.test.ts that simulates premature stream closure and verifies the automatic retry mechanism.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds ERR_STREAM_PREMATURE_CLOSE to the list of retryable network error codes in the core package. It also includes a new unit test to verify that the chat system correctly handles and retries when a stream closes prematurely during content generation. I have no feedback to provide.

@gemini-cli gemini-cli bot added the status/need-issue Pull requests that need to have an associated issue. label Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status/need-issue Pull requests that need to have an associated issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant