From a9b5c693ca7a89c2e48f6dad66420f69894c084b Mon Sep 17 00:00:00 2001 From: euxaristia <25621994+euxaristia@users.noreply.github.com> Date: Thu, 9 Apr 2026 14:27:08 -0400 Subject: [PATCH] fix(cli): suppress unhandled AbortError logs during request cancellation (#22621) --- packages/cli/src/gemini.test.tsx | 19 +++++++++++++++++++ packages/cli/src/gemini.tsx | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/cli/src/gemini.test.tsx b/packages/cli/src/gemini.test.tsx index 611850bd4a..7712d39bb2 100644 --- a/packages/cli/src/gemini.test.tsx +++ b/packages/cli/src/gemini.test.tsx @@ -304,6 +304,25 @@ describe('gemini.tsx main function', () => { vi.restoreAllMocks(); }); + it('should suppress AbortError and not open debug console', async () => { + const debugLoggerErrorSpy = vi.spyOn(debugLogger, 'error'); + const debugLoggerLogSpy = vi.spyOn(debugLogger, 'log'); + const abortError = new DOMException( + 'The operation was aborted.', + 'AbortError', + ); + + setupUnhandledRejectionHandler(); + process.emit('unhandledRejection', abortError, Promise.resolve()); + + await new Promise(process.nextTick); + + expect(debugLoggerErrorSpy).not.toHaveBeenCalled(); + expect(debugLoggerLogSpy).toHaveBeenCalledWith( + expect.stringContaining('Suppressed unhandled AbortError'), + ); + }); + it('should log unhandled promise rejections and open debug console on first error', async () => { const processExitSpy = vi .spyOn(process, 'exit') diff --git a/packages/cli/src/gemini.tsx b/packages/cli/src/gemini.tsx index 166ee0e7eb..eedfcc950a 100644 --- a/packages/cli/src/gemini.tsx +++ b/packages/cli/src/gemini.tsx @@ -164,6 +164,14 @@ export function getNodeMemoryArgs(isDebugMode: boolean): string[] { export function setupUnhandledRejectionHandler() { let unhandledRejectionOccurred = false; process.on('unhandledRejection', (reason, _promise) => { + // AbortError is expected when the user cancels a request (e.g. pressing ESC). + // It may surface as an unhandled rejection due to async timing in the + // streaming pipeline, but it is not a bug. + if (reason instanceof Error && reason.name === 'AbortError') { + debugLogger.log(`Suppressed unhandled AbortError: ${reason.message}`); + return; + } + const errorMessage = `========================================= This is an unexpected error. Please file a bug report using the /bug tool. CRITICAL: Unhandled Promise Rejection!