Skip to content

fix(core): clean up temporary directory when background shell execution exits - #29437

Merged
DavidAPierce merged 4 commits into
google-gemini:mainfrom
jesussamuel-byte:561554629
Sep 25, 2026
Merged

DavidAPierce merged 4 commits into
google-gemini:mainfrom
jesussamuel-byte:561554629

Conversation

@jesussamuel-byte

Copy link
Copy Markdown
Contributor

Ensures temporary directories (gemini-shell-*) created to store background process ID files (bgpids.tmp) during shell execution are transferred to ShellExecutionService when a command runs in the background and automatically removed once the background process completes.

Details

  • Temporary Directory Ownership Transfer: Extended ShellExecutionService.background(pid, sessionId, command, tempDir) and ShellExecutionConfig in packages/core/src/services/shellExecutionService.ts to accept an optional tempDir path and store it alongside background process metadata (BackgroundProcessRecord.tempDir and backgroundTempDirs).
  • Automatic Cleanup on Process Exit: Updated ShellExecutionService.cleanupLogStream(pid) and ShellExecutionService.resetForTest() to remove the associated temporary directory via fsPromises.rm(tempDir, { recursive: true, force: true }) when a background process exits or is terminated.
  • Shell Tool Invocation Lifecycle: Updated ShellToolInvocation.execute in packages/core/src/tools/shell.ts to pass tempDir to ShellExecutionService.background and clean up tempDir in the finally block whenever ownership was not transferred (preserving foreground cleanup and ensuring fast-exiting is_background: true commands that finish before the promotion delay also clean up their temporary directory).
  • Unit & Integration Test Coverage:
    • Updated packages/core/src/tools/shell.test.ts to verify tempDir ownership transfer to ShellExecutionService.background and cleanup when a command completes before the promotion timer elapses.
    • Added a unit test in packages/core/src/services/shellExecutionService.test.ts verifying tempDir storage and deletion on background process exit.
    • Added an integration test in packages/core/src/tools/shellBackgroundTools.integration.test.ts that starts a short-lived background command, waits for it to exit, and verifies the temporary directory is deleted.

Related Issues

Fixes #28392
GH-28392

How to Validate

  1. Run targeted unit and integration tests for ShellTool, ShellExecutionService, and background tools:
    npm test -w @google/gemini-cli-core -- src/tools/shell.test.ts src/services/shellExecutionService.test.ts src/tools/shellBackgroundTools.integration.test.ts
  2. Run full preflight checks (format, lint, and typecheck):
    npm run format && npm run lint:ci && npm run typecheck

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

@jesussamuel-byte
jesussamuel-byte requested review from a team as code owners September 21, 2026 16:26
@github-actions github-actions Bot added the size/m A medium sized PR label Sep 21, 2026
@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

📊 PR Size: size/L

  • Lines changed: 273
  • Additions: +247
  • Deletions: -26
  • Files changed: 5

@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 a resource management issue where temporary directories created for background shell processes were not being cleaned up after the processes finished. By transferring ownership of these directories to the ShellExecutionService, the system now ensures that temporary files are reliably removed regardless of whether the process finishes quickly or runs for an extended period.

Highlights

  • Temporary Directory Lifecycle Management: Implemented a mechanism to transfer ownership of temporary directories from shell tool invocations to the ShellExecutionService, ensuring they are automatically cleaned up when background processes complete.
  • Service Integration: Updated ShellExecutionService to track and store temporary directory paths associated with background processes, enabling reliable deletion upon process exit or service reset.
  • Testing and Validation: Added comprehensive unit and integration tests to verify that temporary directories are correctly transferred, preserved during execution, and deleted upon process termination.
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 automatic cleanup of temporary directories associated with background shell executions by transferring their ownership to ShellExecutionService, which deletes them upon process exit. Feedback on these changes highlights three key improvements: using synchronous fs.rmSync in test teardown contexts to avoid race conditions, adding a defensive guard in ShellExecutionService.background to prevent directory leaks for untracked processes, and wrapping the backgrounding logic in a try-catch block to ensure proper cleanup if backgrounding fails.

Comment thread packages/core/src/services/shellExecutionService.ts
Comment thread packages/core/src/services/shellExecutionService.ts Outdated
Comment thread packages/core/src/tools/shell.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 21, 2026
@github-actions github-actions Bot added the size/l A large sized PR label Sep 21, 2026
@jesussamuel-byte

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 automatic cleanup of temporary directories (tempDir) created during shell execution when a process is moved to the background. Ownership of the temporary directory is transferred from ShellToolInvocation to ShellExecutionService once a process is backgrounded. ShellExecutionService then ensures the directory is deleted when the background process exits, when the service is destroyed, or immediately if the process is untracked or already exited. Corresponding unit and integration tests have been added to verify this behavior. There are no review comments, and I have no additional feedback to provide.

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

🚨 Action Required: Eval Regressions Detected

Model: gemini-3-flash-preview

The following trustworthy evaluations passed on main and in recent Nightly runs, but failed in this PR. These regressions must be addressed before merging.

Test Name Nightly PR Result Status
Agent uses AskUser tool to clarify ambiguous requirements 100% 25% ❌ Regression

The check passed or was cleared for 72 other trustworthy evaluations.

🛠️ Troubleshooting & Fix Instructions

1. Ask Gemini CLI to fix it (Recommended)

Copy and paste this prompt to the agent:

The eval "Agent uses AskUser tool to clarify ambiguous requirements" in evals/ask_user.eval.ts is failing. Investigate and fix it using the behavioral-evals skill.

2. Reproduce Locally

Run the following command to see the failure trajectory:

GEMINI_MODEL=gemini-3-flash-preview npm run test:all_evals -- evals/ask_user.eval.ts --testNamePattern="Agent uses AskUser tool to clarify ambiguous requirements"

3. Manual Fix

See the Fixing Guide for detailed troubleshooting steps.

### 🧠 Model Steering Guidance

This PR modifies files that affect the model's behavior (prompts, tools, or instructions).

  • ⚠️ Consider adding Evals: No behavioral evaluations (evals/*.eval.ts) were added or updated in this PR. Consider adding a test case to verify the new behavior and prevent regressions.
  • 🚀 Maintainer Reminder: Please ensure that these changes do not regress results on benchmark evals before merging.

This is an automated guidance message triggered by steering logic signatures.

@DavidAPierce
DavidAPierce added this pull request to the merge queue Sep 24, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 24, 2026
@DavidAPierce
DavidAPierce added this pull request to the merge queue Sep 25, 2026
Merged via the queue into google-gemini:main with commit 20f7075 Sep 25, 2026
53 of 55 checks passed

This branch was successfully deployed

1 active deployment
eval-gate — ea909e6c Deployed Sep 25, 2026 by DavidAPierce via Evaluate Steering & Regressions #2083
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/l A large sized PR size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Temporary Directory Leak During Background Shell Execution

2 participants