Skip to content

fix(core): remove synchronous I/O from shell tool critical path#28397

Open
Daksh7785 wants to merge 2 commits into
google-gemini:mainfrom
Daksh7785:fix/ui-stutter-sync-io
Open

fix(core): remove synchronous I/O from shell tool critical path#28397
Daksh7785 wants to merge 2 commits into
google-gemini:mainfrom
Daksh7785:fix/ui-stutter-sync-io

Conversation

@Daksh7785

Copy link
Copy Markdown

Description

This PR replaces blocking synchronous filesystem operations (fs.mkdtempSync, fs.existsSync, fs.statSync) in packages/core/src/tools/shell.ts with their asynchronous counterparts from node:fs/promises.

Problem

The React Ink terminal UI in the CLI was stuttering and freezing because the single-threaded Node.js event loop was being blocked by synchronous file system operations executed just before spawning processes.

Solution

  • Replaced fs.mkdtempSync with await fsPromises.mkdtemp.
  • Refactored the permission-checking loops to use await Promise.all alongside fsPromises.access and fsPromises.stat to ensure non-blocking execution.
  • Removed unused node:fs imports.

Fixes #28395

This fixes the UI stutter issue google-gemini#28395 where React Ink terminal frames were being paused due to synchronous fs calls like mkdtempSync and existsSync.
@Daksh7785 Daksh7785 requested a review from a team as a code owner July 13, 2026 18:54
@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 performance issues in the CLI's terminal UI by eliminating blocking synchronous filesystem calls. By migrating these operations to asynchronous promises, the application avoids stuttering and freezing caused by event loop contention during process spawning and permission validation.

Highlights

  • Non-blocking I/O: Replaced synchronous filesystem operations (fs.mkdtempSync, fs.existsSync, fs.statSync) with asynchronous versions from node:fs/promises to prevent event loop blocking.
  • Concurrency Improvements: Refactored permission-checking loops to utilize Promise.all, allowing for parallel asynchronous execution when processing sandbox file paths.
  • Code Cleanup: Removed unused imports of the synchronous node:fs module.
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.

@github-actions github-actions Bot added the size/m A medium sized PR label Jul 13, 2026
@github-actions

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 84
  • Additions: +48
  • Deletions: -36
  • Files changed: 1

@google-cla

google-cla Bot commented Jul 13, 2026

Copy link
Copy Markdown

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.

@github-actions

Copy link
Copy Markdown

🛑 Action Required: Evaluation Approval

Steering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged.

Maintainers:

  1. Go to the Workflow Run Summary.
  2. Click the yellow 'Review deployments' button.
  3. Select the 'eval-gate' environment and click 'Approve'.

Once approved, the evaluation results will be posted here automatically.

@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 refactors the shell tool implementation in packages/core/src/tools/shell.ts to use asynchronous file system operations (fsPromises) instead of synchronous ones, and parallelizes the sandbox denial path resolution using Promise.all. However, a high-severity infinite loop vulnerability was identified in the path traversal logic on Windows, where reaching a root directory (e.g., C:\) can cause an infinite loop because path.dirname returns the same path. A code suggestion has been provided to safely break the loop when the parent directory equals the current path.

Comment on lines +933 to +954
while (currentPath.length > 1) {
const exists = await fsPromises
.access(currentPath)
.then(() => true)
.catch(() => false);
if (exists) {
const mode = this.context.config.getApprovalMode();
const isReadonlyMode =
this.context.config.sandboxPolicyManager.getModeConfig(
mode,
)?.readonly ?? false;
const isAllowed =
this.context.config.isPathAllowed(currentPath);

if (!isAllowed || isReadonlyMode) {
writePaths.add(currentPath);
readPaths.add(currentPath);
}
break;
}
currentPath = path.dirname(currentPath);
}

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.

security-high high

An infinite loop / Denial of Service (DoS) vulnerability exists in the sandbox denial path resolution logic on Windows. When traversing up the directory tree using path.dirname(currentPath), if currentPath resolves to a non-existent root path (e.g., C:\ or a non-existent drive like Z:\) or an inaccessible UNC path, fsPromises.access will fail and path.dirname will return the same root path. Since the length of the root path is greater than 1, the loop condition while (currentPath.length > 1) remains true, causing an infinite loop that consumes 100% CPU, freezes the application, and hangs the shell tool execution indefinitely.

To remediate this, check if the parent directory returned by path.dirname(currentPath) is equal to currentPath (indicating that the root directory has been reached and no further traversal is possible) and break the loop.

                  while (currentPath.length > 1) {
                    const exists = await fsPromises
                      .access(currentPath)
                      .then(() => true)
                      .catch(() => false);
                    if (exists) {
                      const mode = this.context.config.getApprovalMode();
                      const isReadonlyMode =
                        this.context.config.sandboxPolicyManager.getModeConfig(
                          mode,
                        )?.readonly ?? false;
                      const isAllowed =
                        this.context.config.isPathAllowed(currentPath);

                      if (!isAllowed || isReadonlyMode) {
                        writePaths.add(currentPath);
                        readPaths.add(currentPath);
                      }
                      break;
                    }
                    const parent = path.dirname(currentPath);
                    if (parent === currentPath) {
                      break;
                    }
                    currentPath = parent;
                  }

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality labels Jul 13, 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 priority/p2 Important but can be addressed in a future release. size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blocking Synchronous I/O on the Main Thread Causes UI Stutter

1 participant