Skip to content

fix(core): avoid error handling on cancelled requests to prevent crash#6039

Merged
bulkypanda merged 4 commits intogoogle-gemini:mainfrom
pwrwpw:fix/5935
Aug 21, 2025
Merged

fix(core): avoid error handling on cancelled requests to prevent crash#6039
bulkypanda merged 4 commits intogoogle-gemini:mainfrom
pwrwpw:fix/5935

Conversation

@pwrwpw
Copy link
Contributor

@pwrwpw pwrwpw commented Aug 12, 2025

TLDR

Fix crash caused by accessing error properties on cancelled requests.
Checks for request cancellation early and gracefully handles it to prevent runtime errors.

Dive Deeper

When a user cancels a request (e.g., by pressing ESC), the error handling code attempts to read properties from an undefined error object, causing a crash.
This change moves the cancellation check before any error processing, so if the request is aborted, the code emits a cancellation event and exits cleanly without throwing exceptions or processing undefined errors.

Reviewer Test Plan

  • Send requests and cancel them using ESC abort signals.

  • Verify that no unnecessary exception handling occurs upon cancellation.

  • Verify that the UserCancelled event is emitted appropriately on cancellation.

  • Ensure that normal errors (e.g., UnauthorizedError) are still handled correctly.

Testing Matrix

🍏 🪟 🐧
npm run
npx
Docker
Podman - -
Seatbelt - -

Linked issues / bugs

Fixes #5935

@pwrwpw pwrwpw requested a review from a team as a code owner August 12, 2025 09:47
@google-cla
Copy link

google-cla bot commented Aug 12, 2025

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.

Copy link
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.

Summary of Changes

Hello @pwrwpw, 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 addresses a critical crash that occurred when users cancelled ongoing requests. The fix reorders the error handling logic to first check if a request has been aborted. By doing so, the system can gracefully handle cancellations, preventing attempts to access undefined error properties and ensuring a clean exit. This change maintains the correct handling of other error types, such as unauthorized access, while significantly improving the stability of request cancellation.

Highlights

  • Crash Fix for Cancelled Requests: The core issue addressed is a crash that occurs when a user cancels a request, leading to attempts to access properties on an undefined error object within the error handling flow.
  • Prioritized Cancellation Handling: The solution involves reordering the error handling logic to check for request cancellation (signal.aborted) before attempting to process any potential errors. This ensures that if a request is aborted, the system gracefully exits and emits a UserCancelled event without triggering further errors.
  • Preservation of Existing Error Handling: The change ensures that while cancelled requests are handled gracefully, the system still correctly processes and throws UnauthorizedError instances and reports other general errors as intended.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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.

Copy link
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 crash that occurs when a request is cancelled. The fix involves moving the cancellation check to the beginning of the catch block, ensuring that cancelled requests are handled gracefully before any other error processing occurs. This prevents potential runtime errors from attempting to access properties on a malformed error object. The change is logical, robust, and correctly prioritizes user-initiated cancellations. I approve this change.

Copy link
Contributor

@jacob314 jacob314 left a comment

Choose a reason for hiding this comment

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

lgtm
Thanks for the pr. If you can follow up with a adding a quick test to ensure we don't regress that would be helpful to make sure we don't regress and reintroduce a bug.

@jacob314 jacob314 enabled auto-merge August 20, 2025 00:44
auto-merge was automatically disabled August 20, 2025 02:53

Head branch was pushed to by a user without write access

@pwrwpw
Copy link
Contributor Author

pwrwpw commented Aug 20, 2025

/gemini

@gemini-code-assist
Copy link
Contributor

Hello @pwrwpw, I've received your request for a code review. Here's my assessment of your pull request:

Overall Review

This pull request effectively addresses a critical crash scenario by reordering the cancellation check within the Turn class's error handling logic. The change ensures that if a request is aborted, the system gracefully handles the cancellation and avoids attempting to process potentially malformed error objects, which previously led to a crash.

Detailed Review

packages/core/src/core/turn.ts

  • Change: The if (signal.aborted) check has been moved to occur before the toFriendlyError(e) call.
  • Impact: This is the core of the fix. By checking for cancellation first, the code prevents toFriendlyError from being called with a malformed error object when the request has been aborted. This directly resolves the crash described in the PR description.
  • Correctness: critical - This change is essential for the stability and correctness of the application, preventing a runtime crash.

packages/core/src/core/turn.test.ts

  • New Test Case: A new test should not crash when cancelled request has malformed error (reproduces 84d87c2a bug) has been added.
  • Test Effectiveness: This test is excellent. It precisely mocks the sendMessageStream to simulate both cancellation and the malformed error condition, verifying that the UserCancelled event is emitted and that reportError is not called. This provides strong assurance that the fix works as intended and prevents regressions.
  • Maintainability: high - Adding a specific test case for a previously identified bug significantly improves the maintainability and robustness of the codebase.

Conclusion

This is a well-executed fix for a significant issue. The code change is minimal but highly effective, and the accompanying test case thoroughly validates the solution. The pull request is ready to merge from a code quality and correctness perspective.

Great work, @pwrwpw!

@pwrwpw
Copy link
Contributor Author

pwrwpw commented Aug 20, 2025

/gemini review

Copy link
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 crash that occurs when a request is cancelled. The fix correctly prioritizes checking for request cancellation before attempting to process any associated error, which could be malformed and cause a runtime error. The change is logical, well-contained, and is accompanied by a specific test case that validates the fix by simulating the exact crash scenario. The implementation is sound and effectively resolves the reported issue.

@bulkypanda bulkypanda added this pull request to the merge queue Aug 21, 2025
Merged via the queue into google-gemini:main with commit f8f79bf Aug 21, 2025
18 checks passed
@pwrwpw pwrwpw deleted the fix/5935 branch August 21, 2025 09:25
thacio added a commit to thacio/auditaria that referenced this pull request Aug 21, 2025
silviojr pushed a commit that referenced this pull request Aug 21, 2025
acoliver referenced this pull request in vybestack/llxprt-code Sep 11, 2025
…h (#6039)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
involvex pushed a commit to involvex/gemini-cli that referenced this pull request Sep 11, 2025
reconsumeralization pushed a commit to reconsumeralization/gemini-cli that referenced this pull request Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Error: Cannot read properties of undefined (reading 'error')]

3 participants