fix(cli): prevent orphaned processes from consuming 100% CPU#15889
fix(cli): prevent orphaned processes from consuming 100% CPU#15889JuanCS-Dev wants to merge 1 commit intogoogle-gemini:mainfrom
Conversation
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
Summary of ChangesHello @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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| 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); | ||
| } |
There was a problem hiding this comment.
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);
}
|
Closing as this PR is redundant as the proposed changes (signal handlers and TTY check) are already implemented in |
Summary
A
gemini-cliprocess becomes orphaned when the parent terminal is closed, enters a CPU spin loop, and runs indefinitely consuming 100% CPU.Changes
SIGHUP,SIGTERM, andSIGINTto ensure graceful shutdown.setupTtyCheckfor interactive sessions to detect lost controlling terminals as a defense-in-depth measure.runExitCleanup()is called on signal-induced exits.Fixes #15874