Skip to content

fix(cli): propagate cancellation into shell command injections - #29459

Closed
FanouZeng-TT wants to merge 2 commits into
google-gemini:mainfrom
FanouZeng-TT:fix-shell-processor-abort-signal
Closed

FanouZeng-TT wants to merge 2 commits into
google-gemini:mainfrom
FanouZeng-TT:fix-shell-processor-abort-signal

Conversation

@FanouZeng-TT

Copy link
Copy Markdown

Summary

!{...} shell injections inside custom commands were executed with a brand-new AbortController().signal, so nothing could ever abort them: the caller's cancellation never reached the subprocess, and no injection had a budget of its own. A custom command containing a hung command (for example !{sleep 600}) therefore blocked the whole prompt pipeline with no way out — even though ShellProcessor already handles aborted results and appends [Shell command '…' aborted], which is what a cancelled command was always meant to produce.

Details

Each injection now runs under the caller's signal combined with a per-command ceiling (AbortSignal.any([timeout, caller]), the pattern already used elsewhere in this repo, e.g. gitUtils.ts):

export const SHELL_INJECTION_TIMEOUT_MS = 60_000;

function getCommandAbortSignal(caller?: AbortSignal): AbortSignal {
  const timeoutSignal = AbortSignal.timeout(SHELL_INJECTION_TIMEOUT_MS);
  return caller ? AbortSignal.any([timeoutSignal, caller]) : timeoutSignal;
}

The caller's signal is threaded through a new optional CommandContext.signal, wired from the non-interactive abort controller in nonInteractiveCliCommands.ts. IPromptProcessor.process is unchanged, so there is no cross-package interface change.

Two deliberate choices worth your review: the 60_000 ceiling matches the existing COMMAND_TIMEOUT_MS used by core's auth value resolver, and a pipeline with no per-command budget is precisely what made the hang unrecoverable — but it is a new bound on injections, so say the word if you would rather keep this PR to signal propagation only, or make the ceiling configurable. The interactive path has no signal to hand yet, so there the ceiling is the only escape; wiring the interactive abort signal through to CommandContext is a natural follow-up and is left out here to keep the change small.

Related Issues

Fixes #29314

How to Validate

Red before the source change (only the new tests added):

npx vitest run src/services/prompt-processors/shellProcessor.test.ts   # 2 failed | 35 passed
  should forward the caller signal to shell execution
    → expected false to be true            # the signal passed to execute never aborts
  should abort a hung command once the injection timeout elapses
    → expected "timeout" to be called with arguments: [ Any<Number> ]
      Number of calls: 0                    # no timeout signal existed at all

Green after:

npx tsc --noEmit                                  # clean (packages/cli)
npx eslint <the four changed files>               # clean
npx vitest run src/services/prompt-processors/shellProcessor.test.ts   # 37 passed

Regression surfaces: src/ui/hooks/slashCommandProcessor.test.tsx, src/nonInteractiveCli.test.ts, src/ui/commands, src/services → 690 passed; src/services/FileCommandLoader.test.ts + src/services/prompt-processors → 118 passed. Manual: a custom command containing !{sleep 600} no longer holds the prompt pipeline open — cancelling (headless) or exceeding the ceiling terminates the command and processing continues with the aborted marker.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed) — not needed, no user-facing docs for this path
  • Added/updated tests (if needed)
  • Noted breaking changes (if any) — none; injections are now bounded at 60s, see Details
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

🤖 Generated with Claude Code

ShellProcessor executed every !{...} injection with a fresh AbortController
signal, so nothing could ever abort a command: the caller's cancellation never
reached the subprocess, and no injection had a budget of its own. A custom
command containing a hung command therefore blocked the whole prompt pipeline
with no way out, even though the processor already handles `aborted` results.

Injunctions now run under the caller's signal (threaded through a new optional
`CommandContext.signal`, wired from the non-interactive abort controller) combined
with a per-command timeout, so cancelling or exceeding the ceiling terminates the
command and the pipeline continues with the existing aborted marker.
@FanouZeng-TT
FanouZeng-TT requested a review from a team as a code owner September 23, 2026 04:09
@github-actions github-actions Bot added the size/m A medium sized PR label Sep 23, 2026
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 212
  • Additions: +201
  • Deletions: -11
  • Files changed: 4

@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 addresses an issue where shell command injections (e.g., !{...}) could hang indefinitely, blocking the prompt pipeline because they were executed with an independent abort controller. By threading the caller's cancellation signal through the command context and enforcing a mandatory 60-second timeout per injection, the system now ensures that long-running or hung commands can be properly terminated, improving overall stability and responsiveness.

Highlights

  • Cancellation Propagation: Introduced an optional AbortSignal to CommandContext to allow the propagation of cancellation requests from the caller down to shell command injections.
  • Execution Timeout: Implemented a 60-second timeout for all shell injections using AbortSignal.timeout to prevent hung commands from blocking the prompt pipeline indefinitely.
  • Signal Composition: Created a helper function getCommandAbortSignal that combines the caller's signal with the new 60-second timeout, ensuring commands are terminated if either condition is met.
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 the 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 counterproductive. 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. ↩

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

Copy link
Copy Markdown
Contributor

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 introduces command cancellation and execution timeouts for shell command injections by propagating an AbortSignal from the CLI command context to the shell execution processor. Feedback on the changes highlights compatibility issues with AbortSignal.any in older Node.js 20.x versions, as well as potential memory and timer leaks from AbortSignal.timeout. It is recommended to refactor the signal creation using standard setTimeout with proper cleanup, and to update the corresponding unit tests to use Vitest's fake timers.

Comment thread packages/cli/src/services/prompt-processors/shellProcessor.ts Outdated
Comment thread packages/cli/src/services/prompt-processors/shellProcessor.test.ts
@gemini-cli gemini-cli Bot added priority/p1 Important and should be addressed in the near term. area/core Issues related to User Interface, OS Support, Core Functionality labels Sep 23, 2026
@gemini-cli

gemini-cli Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'.

This PR will be closed in 7 days if it remains without that designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding.

@gemini-cli

gemini-cli Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding.

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 priority/p1 Important and should be addressed in the near term. size/m A medium sized PR status/pr-nudge-sent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(cli): ShellProcessor ignores abort signal, hung custom command blocks prompt pipeline

1 participant