Skip to content

fix(core): prevent unhandled AbortError crash during stream loop detection#21123

Merged
ruomengz merged 13 commits intogoogle-gemini:mainfrom
7hokerz:fix/unhandled-abort-error-crash
Mar 5, 2026
Merged

fix(core): prevent unhandled AbortError crash during stream loop detection#21123
ruomengz merged 13 commits intogoogle-gemini:mainfrom
7hokerz:fix/unhandled-abort-error-crash

Conversation

@7hokerz
Copy link
Copy Markdown
Contributor

@7hokerz 7hokerz commented Mar 4, 2026

Summary

This PR resolves a critical bug where the CLI crashes with an unhandled AbortError during stream processing.

Details

The crash occurred in processTurn because controller.abort() was being invoked directly within the for await (const event of resultStream) iteration upon detecting a stream loop. This caused the underlying active stream to immediately throw an AbortError. Lacking a try...catch block, this resulted in an unhandled promise rejection that abruptly terminated the Node.js process.

To resolve this structurally, a loopDetectedAbort boolean flag was introduced. The logic now sets this flag and uses break to safely exit the asynchronous iteration first. The controller.abort() method is then called after the loop has gracefully terminated, preventing the exception from leaking into the active stream processing state.

Related Issues

Fixed #21122

How to Validate

  1. Run Unit Tests: Execute the test suite containing the newly added test cases covering the loop detection scenario.
    # Example command
    npm test -w @google/gemini-cli-core -- src/core/client.test.ts

Expected Result: Tests should pass successfully without triggering any unhandled AbortError rejections.

Pre-Merge Checklist

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

@7hokerz 7hokerz requested a review from a team as a code owner March 4, 2026 17:18
@gemini-cli gemini-cli bot added the area/core Issues related to User Interface, OS Support, Core Functionality label Mar 4, 2026
@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 resolves a critical bug in the CLI that caused unhandled AbortError crashes during stream processing when a loop was detected. The fix introduces a flag to handle the abort signal more gracefully, preventing the error from crashing the application. A new unit test has been added to validate the fix.

Highlights

  • Bug Fix: Addresses a critical issue where the CLI was crashing due to an unhandled AbortError during stream processing when a loop was detected.
  • Structural Solution: Introduces a loopDetectedAbort flag to ensure the asynchronous iteration is exited gracefully before calling controller.abort(), preventing the exception from leaking.
  • Test Coverage: Includes a new unit test to specifically cover the loop detection scenario and ensure no unhandled AbortError rejections occur.
Changelog
  • packages/core/src/core/client.test.ts
    • Added a unit test to verify that the stream aborts cleanly and returns a Turn instance when a loop is detected, without causing unhandled promise rejections.
  • packages/core/src/core/client.ts
    • Introduced a loopDetectedAbort flag to handle stream loop detection and prevent unhandled AbortErrors.
Activity
  • The PR introduces a loopDetectedAbort flag to prevent unhandled AbortErrors during stream processing.
  • A new unit test was added to specifically validate the fix for the loop detection scenario.
  • The changes ensure that the abort signal is handled gracefully, preventing application crashes.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

@gsquared94
Copy link
Copy Markdown
Contributor

/gemini review

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 addresses a crash caused by an unhandled AbortError during stream processing when a loop is detected. The fix defers aborting the stream's controller until after the stream iteration loop is exited, preventing the error. A new unit test is added to verify this behavior and prevent regressions. The changes are confined to packages/core/src/core/client.ts and its corresponding test file.

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 addresses a critical unhandled AbortError crash that occurs during stream processing when a loop is detected. The fix correctly defers the call to controller.abort() until after the stream's for await loop has been safely exited using a break statement, preventing the unhandled promise rejection. The changes are sound, and the new unit test effectively validates that the fix works as expected without introducing regressions.

@gemini-cli gemini-cli bot added priority/p2 Important but can be addressed in a future release. help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Mar 4, 2026
@ruomengz
Copy link
Copy Markdown
Contributor

ruomengz commented Mar 4, 2026

Thanks for the fix! It looks good, but there is a merge conflict in packages/core/src/core/client.ts. Could you please sync with main and resolve that? I've approved the workflows so we can see the CI results once the conflict is cleared.

@7hokerz 7hokerz marked this pull request as draft March 5, 2026 00:11
@7hokerz 7hokerz marked this pull request as ready for review March 5, 2026 00:36
@7hokerz
Copy link
Copy Markdown
Contributor Author

7hokerz commented Mar 5, 2026

@ruomengz, I've resolved the conflicts and restructured the code to align with the updated architecture.

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 addresses a crash that occurred when an AbortError was thrown from within an active asynchronous stream iteration. The fix involves deferring the abort() call until after the stream's for await...of loop has terminated. This is achieved by setting a flag within the loop and then acting on it after the loop breaks. This pattern is also applied to the loop recovery logic, which could also trigger an abort. A new unit test has been added to verify that the stream now completes gracefully under these conditions.

@7hokerz
Copy link
Copy Markdown
Contributor Author

7hokerz commented Mar 5, 2026

@ruomengz , Could you please run CI?

@ruomengz ruomengz enabled auto-merge March 5, 2026 14:43
@7hokerz
Copy link
Copy Markdown
Contributor Author

7hokerz commented Mar 5, 2026

@ruomengz , A particular test fails due to a timeout, what should I do?

@ruomengz ruomengz added this pull request to the merge queue Mar 5, 2026
Merged via the queue into google-gemini:main with commit a830858 Mar 5, 2026
62 of 67 checks passed
@7hokerz 7hokerz deleted the fix/unhandled-abort-error-crash branch March 6, 2026 04:54
struckoff pushed a commit to struckoff/gemini-cli that referenced this pull request Mar 6, 2026
…ction (google-gemini#21123)

Co-authored-by: Gaurav <39389231+gsquared94@users.noreply.github.com>
Co-authored-by: ruomeng <ruomeng@google.com>
kunal-10-cloud pushed a commit to kunal-10-cloud/gemini-cli that referenced this pull request Mar 12, 2026
…ction (google-gemini#21123)

Co-authored-by: Gaurav <39389231+gsquared94@users.noreply.github.com>
Co-authored-by: ruomeng <ruomeng@google.com>
liamhelmer pushed a commit to badal-io/gemini-cli that referenced this pull request Mar 12, 2026
…ction (google-gemini#21123)

Co-authored-by: Gaurav <39389231+gsquared94@users.noreply.github.com>
Co-authored-by: ruomeng <ruomeng@google.com>
yashodipmore pushed a commit to yashodipmore/geemi-cli that referenced this pull request Mar 21, 2026
…ction (google-gemini#21123)

Co-authored-by: Gaurav <39389231+gsquared94@users.noreply.github.com>
Co-authored-by: ruomeng <ruomeng@google.com>
SUNDRAM07 pushed a commit to SUNDRAM07/gemini-cli that referenced this pull request Mar 30, 2026
…ction (google-gemini#21123)

Co-authored-by: Gaurav <39389231+gsquared94@users.noreply.github.com>
Co-authored-by: ruomeng <ruomeng@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unhandled AbortError crash during stream generation in processTurn

3 participants