diff --git a/packages/cli/src/utils/cleanup.ts b/packages/cli/src/utils/cleanup.ts index da9ff927ef..eaed9e861c 100644 --- a/packages/cli/src/utils/cleanup.ts +++ b/packages/cli/src/utils/cleanup.ts @@ -55,6 +55,10 @@ export function registerTelemetryConfig(config: Config) { } export async function runExitCleanup() { + // drain stdin to prevent printing garbage on exit + // https://github.com/google-gemini/gemini-cli/issues/1680 + await drainStdin(); + runSyncCleanup(); for (const fn of cleanupFunctions) { try { @@ -84,6 +88,18 @@ export async function runExitCleanup() { } } +async function drainStdin() { + if (!process.stdin?.isTTY) return; + // Resume stdin and attach a no-op listener to drain the buffer. + // We use removeAllListeners to ensure we don't trigger other handlers. + process.stdin + .resume() + .removeAllListeners('data') + .on('data', () => {}); + // Give it a moment to flush the OS buffer. + await new Promise((resolve) => setTimeout(resolve, 50)); +} + export async function cleanupCheckpoints() { const storage = new Storage(process.cwd()); const tempDir = storage.getProjectTempDir();