Skip to content

fix(core): handle AbortError when ESC cancels tool execution#20863

Merged
Adib234 merged 4 commits intogoogle-gemini:mainfrom
PrasannaPal21:fix/20839-abort-error-exc
Mar 10, 2026
Merged

fix(core): handle AbortError when ESC cancels tool execution#20863
Adib234 merged 4 commits intogoogle-gemini:mainfrom
PrasannaPal21:fix/20839-abort-error-exc

Conversation

@PrasannaPal21
Copy link
Contributor

When a user presses ESC to cancel an in-flight tool call (e.g. google_web_search), the underlying request can reject with AbortError. Treat AbortError as a normal cancellation so the CLI returns to the prompt without surfacing a raw stack trace.

Fixes #20839

Summary

  • Handle AbortError during tool execution as a normal cancellation so the CLI shows a clean “Request cancelled” message and returns to the prompt without a stack trace.

Details

  • Treat AbortError thrown during tool execution as cancellation (instead of an unhandled exception/error result).
  • Ensure google_web_search returns a clean cancelled result when the abort is caught within the tool.

Related Issues

Fixes #20839

How to Validate

  1. Build + start the CLI from source:
    • npm run build --workspace=@google/gemini-cli-devtools
    • npm run build
    • npm run start
  2. Trigger a web search (anything that invokes google_web_search).
  3. While the search is in progress, press ESC to cancel.
  4. Confirm:
    • The CLI shows a cancellation message (e.g. “Request cancelled.”).
    • No raw AbortError stack trace is printed.
    • The CLI returns to the prompt and continues normally.

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

@PrasannaPal21 PrasannaPal21 requested a review from a team as a code owner March 2, 2026 18:31
@gemini-code-assist
Copy link
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 user experience by gracefully handling AbortError exceptions that can occur when a user cancels an in-flight tool execution, such as a web search. Instead of surfacing raw stack traces, the system now recognizes these errors as cancellations, providing clean, user-friendly messages and returning control to the CLI prompt, thus improving the overall stability and polish of the command-line interface.

Highlights

  • Graceful AbortError Handling: The system now explicitly checks for AbortError during tool execution and treats it as a normal cancellation event, preventing raw stack traces from being displayed to the user.
  • Improved User Experience for Cancellations: When a tool execution is cancelled, either by user action (ESC key) or an AbortError, the CLI will now display a clean 'Operation cancelled.' or 'User cancelled tool execution.' message and return to the prompt.
  • Specific Web Search Cancellation Message: The google_web_search tool has been updated to return a specific 'Web search was cancelled.' message when an AbortError occurs during its execution, ensuring consistent and clear feedback.
Changelog
  • packages/core/src/scheduler/tool-executor.ts
    • Updated error handling logic to explicitly identify and process AbortError instances as cancellations.
    • Modified cancellation message generation to differentiate between generic user cancellations and AbortError-driven cancellations.
  • packages/core/src/tools/web-search.ts
    • Implemented specific AbortError handling within the WebSearchToolInvocation to return a tailored cancellation result.
Activity
  • The pull request was opened by PrasannaPal21 to address an issue where pressing ESC to cancel a tool call resulted in a raw AbortError stack trace.
  • The author provided a detailed summary, specific details, related issues, and validation steps for the changes.
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.

@google-cla
Copy link

google-cla bot commented Mar 2, 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.

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 correctly handles AbortError during tool execution, treating it as a cancellation to prevent unhandled exception stack traces from being shown to the user. The changes are implemented in the central ToolExecutor for generic handling, and specifically within the google_web_search tool for a more tailored cancellation message. The implementation is sound, but there is a small area for improvement regarding code duplication, as noted in the specific comment, which also aligns with best practices for consistent cancellation handling.

Comment on lines +157 to +159
const isAbortError =
executionError instanceof Error &&
executionError.name === 'AbortError';
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This logic for identifying an AbortError is also used in packages/core/src/tools/web-search.ts. To improve maintainability and promote code reuse, consider extracting this check into a shared utility function (e.g., in packages/core/src/utils/errors.ts). This would prevent potential inconsistencies if the definition of an abort error needs to be updated in the future.

References
  1. Asynchronous operations that can be cancelled by the user should accept and propagate an AbortSignal to ensure cancellability and prevent dangling processes or network requests. Consistent identification of AbortError is crucial for robust cancellation handling across the repository.
  2. Asynchronous operations waiting for user input via the MessageBus should rely on the provided AbortSignal for cancellation, rather than implementing a separate timeout, to maintain consistency with existing patterns. Centralizing AbortError identification supports consistent cancellation logic.

@gemini-cli gemini-cli bot added area/core Issues related to User Interface, OS Support, Core Functionality 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 2, 2026
@Adib234 Adib234 self-assigned this Mar 6, 2026
@Adib234
Copy link
Contributor

Adib234 commented Mar 6, 2026

Can we add some tests for this and resolve the merge conflicts?

@PrasannaPal21
Copy link
Contributor Author

PrasannaPal21 commented Mar 6, 2026

Can we add some tests for this and resolve the merge conflicts?

Yes, on it

edit: @Adib234 Ive added the tests and resolved the merge conflicts, please take a look..

When a user presses ESC to cancel an in-flight tool call (e.g. google_web_search),
the underlying request can reject with AbortError. Treat AbortError as a normal
cancellation so the CLI returns to the prompt without surfacing a raw stack trace.

Fixes google-gemini#20839
Add a shared isAbortError() helper in utils/errors and use it in the tool
executor and google_web_search to avoid duplicated AbortError checks.
@PrasannaPal21 PrasannaPal21 force-pushed the fix/20839-abort-error-exc branch from a579599 to 5605526 Compare March 10, 2026 15:34
@PrasannaPal21
Copy link
Contributor Author

hey @Adib234, fixed the linting error, can you run the ci again?

@Adib234 Adib234 added this pull request to the merge queue Mar 10, 2026
Merged via the queue into google-gemini:main with commit 0b78de9 Mar 10, 2026
27 checks passed
JaisalJain pushed a commit to JaisalJain/gemini-cli that referenced this pull request Mar 11, 2026
kunal-10-cloud pushed a commit to kunal-10-cloud/gemini-cli that referenced this pull request Mar 12, 2026
liamhelmer pushed a commit to badal-io/gemini-cli that referenced this pull request Mar 12, 2026
yashodipmore pushed a commit to yashodipmore/geemi-cli that referenced this pull request Mar 21, 2026
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.

AbortError stack trace displayed when pressing ESC during google_web_search

2 participants