Skip to content

fix(cli): prevent orphaned processes from consuming 100% CPU#15889

Closed
JuanCS-Dev wants to merge 1 commit intogoogle-gemini:mainfrom
JuanCS-Dev:fix/orphaned-process-cpu-spin-15874
Closed

fix(cli): prevent orphaned processes from consuming 100% CPU#15889
JuanCS-Dev wants to merge 1 commit intogoogle-gemini:mainfrom
JuanCS-Dev:fix/orphaned-process-cpu-spin-15874

Conversation

@JuanCS-Dev
Copy link
Copy Markdown
Contributor

Summary

A gemini-cli process becomes orphaned when the parent terminal is closed, enters a CPU spin loop, and runs indefinitely consuming 100% CPU.

Changes

  • Added signal handlers for SIGHUP, SIGTERM, and SIGINT to ensure graceful shutdown.
  • Implemented setupTtyCheck for interactive sessions to detect lost controlling terminals as a defense-in-depth measure.
  • Ensured runExitCleanup() is called on signal-induced exits.
  • Cleaned up redundant signal handlers in the main interactive loop.

Fixes #15874

Added SIGHUP, SIGTERM, and SIGINT signal handlers to ensure graceful
shutdown and resource cleanup. Added a periodic TTY check for
interactive sessions as a defense-in-depth measure against revoked
terminal file descriptors causing infinite spin loops.

Fixes google-gemini#15874
@JuanCS-Dev JuanCS-Dev requested a review from a team as a code owner January 4, 2026 17:33
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @JuanCS-Dev, 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 issue where the gemini-cli process could become an orphaned, CPU-intensive runaway process if its parent terminal was closed unexpectedly. The changes introduce robust signal handling and a proactive terminal check to ensure that the CLI application terminates cleanly and efficiently, thereby preventing resource exhaustion and improving overall system stability.

Highlights

  • Graceful Shutdown: Implemented signal handlers for SIGHUP, SIGTERM, and SIGINT to ensure the CLI process shuts down gracefully, preventing orphaned processes.
  • TTY Check: Added a periodic TTY check (setupTtyCheck) for interactive sessions to detect if the controlling terminal has been lost, serving as a defense-in-depth mechanism against CPU-spinning orphans.
  • Cleanup Consistency: Ensured that runExitCleanup() is consistently called when the process exits due to a signal.
  • Refactored Signal Handlers: Cleaned up redundant signal handlers in the main interactive loop by consolidating process.stdin.setRawMode cleanup into a generic registerSyncCleanup.
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.

Copy link
Copy Markdown
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 introduces important fixes to prevent orphaned processes from consuming high CPU by adding signal handlers for graceful shutdown and a periodic TTY check. The changes are well-structured, centralizing cleanup logic. I've identified a potential race condition in the new TTY check mechanism and provided a suggestion to resolve it. Overall, this is a solid improvement for the CLI's robustness.

Comment on lines +206 to +218
if (
!process.stdin.isTTY &&
!process.stdout.isTTY &&
!process.env['SANDBOX']
) {
debugLogger.warn('Lost controlling terminal, exiting...');
try {
await runExitCleanup();
} catch (_) {
// Ignore errors during cleanup
}
process.exit(ExitCodes.SUCCESS);
}
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.

high

There's a potential race condition here. If runExitCleanup() takes longer than the 5-second interval, the setInterval callback could fire again, leading to concurrent shutdown attempts. This can cause unpredictable behavior, especially if cleanup functions are not idempotent.

To prevent this, you should clear the interval as soon as the shutdown process is initiated.

    if (
      !process.stdin.isTTY &&
      !process.stdout.isTTY &&
      !process.env['SANDBOX']
    ) {
      clearInterval(ttyCheckInterval);
      debugLogger.warn('Lost controlling terminal, exiting...');
      try {
        await runExitCleanup();
      } catch (_) {
        // Ignore errors during cleanup
      }
      process.exit(ExitCodes.SUCCESS);
    }

@gemini-cli gemini-cli bot added status/need-issue Pull requests that need to have an associated issue. and removed status/need-issue Pull requests that need to have an associated issue. labels Jan 7, 2026
@gemini-cli gemini-cli bot added the area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt label Jan 10, 2026
@gemini-cli gemini-cli bot added the priority/p2 Important but can be addressed in a future release. label Jan 19, 2026
@jacob314 jacob314 added the help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! label Jan 22, 2026
@scidomino
Copy link
Copy Markdown
Collaborator

Closing as this PR is redundant as the proposed changes (signal handlers and TTY check) are already implemented in packages/cli/src/utils/cleanup.ts

@scidomino scidomino closed this Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt 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.

[BUG] Orphaned gemini-cli process consumes 100% CPU after terminal closure (47+ days)

4 participants