fix(cli): prevent terminal escape sequences from leaking on exit#22682
fix(cli): prevent terminal escape sequences from leaking on exit#22682mattKorwel merged 7 commits intomainfrom
Conversation
Reorders cleanup sequence to disable terminal modes before draining stdin and increases drain timeout. Also adds fallback mouse tracking disable to synchronous cleanup.
Summary of ChangesHello, 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 terminal escape sequences could leak into the shell prompt after the CLI exits, leading to a messy or unresponsive terminal. The changes improve terminal stability and user experience by refining the exit cleanup process, ensuring all terminal modes are properly reset and input buffers are cleared before the application fully terminates. Highlights
Changelog
Activity
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 correctly addresses an issue where terminal escape sequences could leak on exit by reordering the cleanup operations. Running the async cleanup (including UI unmounting) before draining stdin and disabling raw mode is the right approach. The addition of more comprehensive escape sequences for hard exits is also a good defensive measure. I've found one issue in the new test case where the assertion for the cleanup order seems to be incorrect, which I've detailed in a comment.
|
Size Change: +86 B (0%) Total Size: 26.1 MB
ℹ️ View Unchanged
|
jacob314
left a comment
There was a problem hiding this comment.
Looks great! This is a solid fix for the escape sequences leaking on exit. The reordering of the cleanup sequence and the added mouse disabling sequences to the fallback are very robust.
One minor note: in packages/cli/src/utils/cleanup.ts and the PR description, you reference issue #1680, but the correct issue number for the garbage on exit bug is #16801. Could you update the PR description and the code comment?
Otherwise, the changes look excellent and the test successfully validates the cleanup order.
| disableKittyKeyboardProtocol(); | ||
| disableModifyOtherKeys(); | ||
| disableBracketedPasteMode(); | ||
| disableMouseEvents(); |
There was a problem hiding this comment.
disableMouseEvents is getting called from AppContainer. that is likely not needed if we are doing it here.
as a follow up we should also track whether we have called enableMouseEvents and make disableMouseEvents a no-op if we have not enabled it. unless you are in alternate buffer mode we have not enabled mouse events.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>


Summary
This PR prevents terminal escape sequences (especially mouse tracking events) from leaking into the shell prompt after the Gemini CLI exits.
Details
runExitCleanup: Updated the cleanup sequence inpackages/cli/src/utils/cleanup.tsto executecleanupFunctions(which disable terminal modes and unmount the UI) before drainingstdin. This ensures "in-flight" terminal sequences are cleared while the terminal is still in raw mode.drainStdintimeout from 50ms to 100ms for better reliability on slower terminals or during high-activity sessions.TERMINAL_CLEANUP_SEQUENCEinpackages/cli/src/ui/utils/terminalCapabilityManager.tsto include mouse tracking disable sequences. This provides a fallback if the process exits hard without running the full cleanup.packages/cli/src/utils/cleanup.test.tsto verify the execution order of cleanup functions relative to stdin draining.Related Issues
Fixes #16801 (Partial)
Related to mouse/terminal stability issues in alternate buffer mode.
How to Validate
/exitor/quit.^[[<65;...) appear in the shell prompt after exit.npm test -w @google/gemini-cli -- src/utils/cleanup.test.tsPre-Merge Checklist