From bdb9a36c5a11c79ab8d5e7fb7410d3a1b9e10bd2 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Fri, 15 May 2026 18:15:57 -0700 Subject: [PATCH] Fix close after renderer crash --- src/main/window/createMainWindow.test.ts | 211 +++++++++++++++++++++++ src/main/window/createMainWindow.ts | 22 ++- 2 files changed, 232 insertions(+), 1 deletion(-) diff --git a/src/main/window/createMainWindow.test.ts b/src/main/window/createMainWindow.test.ts index ffbb85a02..2f184b7f8 100644 --- a/src/main/window/createMainWindow.test.ts +++ b/src/main/window/createMainWindow.test.ts @@ -567,6 +567,217 @@ describe('createMainWindow', () => { expect(onQuitAborted).toHaveBeenCalledTimes(1) }) + it('allows close after the renderer process is gone', () => { + const windowHandlers: Record void> = {} + const webContents = { + on: vi.fn((event, handler) => { + windowHandlers[event] = handler + }), + setZoomLevel: vi.fn(), + setBackgroundThrottling: vi.fn(), + invalidate: vi.fn(), + setWindowOpenHandler: vi.fn(), + send: vi.fn(), + isCrashed: vi.fn(() => false) + } + const browserWindowInstance = { + webContents, + on: vi.fn((event, handler) => { + windowHandlers[event] = handler + }), + isDestroyed: vi.fn(() => false), + isMaximized: vi.fn(() => true), + isFullScreen: vi.fn(() => false), + getSize: vi.fn(() => [1200, 800]), + setSize: vi.fn(), + maximize: vi.fn(), + show: vi.fn(), + loadFile: vi.fn(), + loadURL: vi.fn() + } + const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}) + browserWindowMock.mockImplementation(function () { + return browserWindowInstance + }) + + createMainWindow(null, { getIsQuitting: () => true }) + + windowHandlers['render-process-gone']?.( + {} as never, + { + reason: 'crashed', + exitCode: 5 + } as never + ) + const preventDefault = vi.fn() + windowHandlers.close({ preventDefault } as never) + + expect(preventDefault).not.toHaveBeenCalled() + expect(webContents.send).not.toHaveBeenCalledWith('window:close-requested', { + isQuitting: true + }) + + consoleError.mockRestore() + }) + + it('does not persist pending bounds after bypassing close for a gone renderer', () => { + vi.useFakeTimers() + + const windowHandlers: Record void> = {} + const webContents = { + on: vi.fn((event, handler) => { + windowHandlers[event] = handler + }), + setZoomLevel: vi.fn(), + setBackgroundThrottling: vi.fn(), + invalidate: vi.fn(), + setWindowOpenHandler: vi.fn(), + send: vi.fn(), + isCrashed: vi.fn(() => false) + } + const browserWindowInstance = { + webContents, + on: vi.fn((event, handler) => { + windowHandlers[event] = handler + }), + isDestroyed: vi.fn(() => false), + isMaximized: vi.fn(() => false), + isFullScreen: vi.fn(() => false), + getSize: vi.fn(() => [1200, 800]), + getBounds: vi.fn(() => ({ x: 10, y: 20, width: 1000, height: 700 })), + setSize: vi.fn(), + maximize: vi.fn(), + show: vi.fn(), + loadFile: vi.fn(), + loadURL: vi.fn() + } + const updateUI = vi.fn() + const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}) + browserWindowMock.mockImplementation(function () { + return browserWindowInstance + }) + + createMainWindow({ + getUI: () => ({}), + getSettings: () => ({ windowBackgroundBlur: false }), + updateUI + } as never) + + windowHandlers.resize() + windowHandlers['render-process-gone']?.( + {} as never, + { + reason: 'crashed', + exitCode: 5 + } as never + ) + const preventDefault = vi.fn() + windowHandlers.close({ preventDefault } as never) + vi.advanceTimersByTime(500) + + expect(preventDefault).not.toHaveBeenCalled() + expect(updateUI).not.toHaveBeenCalled() + + consoleError.mockRestore() + }) + + it('resumes close confirmation after a renderer process reloads', () => { + const windowHandlers: Record void> = {} + const webContents = { + on: vi.fn((event, handler) => { + windowHandlers[event] = handler + }), + setZoomLevel: vi.fn(), + setBackgroundThrottling: vi.fn(), + invalidate: vi.fn(), + setWindowOpenHandler: vi.fn(), + send: vi.fn(), + isCrashed: vi.fn(() => false) + } + const browserWindowInstance = { + webContents, + on: vi.fn((event, handler) => { + windowHandlers[event] = handler + }), + isDestroyed: vi.fn(() => false), + isMaximized: vi.fn(() => true), + isFullScreen: vi.fn(() => false), + getSize: vi.fn(() => [1200, 800]), + setSize: vi.fn(), + maximize: vi.fn(), + show: vi.fn(), + loadFile: vi.fn(), + loadURL: vi.fn() + } + const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}) + browserWindowMock.mockImplementation(function () { + return browserWindowInstance + }) + + createMainWindow(null, { getIsQuitting: () => true }) + + windowHandlers['render-process-gone']?.( + {} as never, + { + reason: 'crashed', + exitCode: 5 + } as never + ) + windowHandlers['did-finish-load']?.() + const preventDefault = vi.fn() + windowHandlers.close({ preventDefault } as never) + + expect(preventDefault).toHaveBeenCalledTimes(1) + expect(webContents.send).toHaveBeenCalledWith('window:close-requested', { + isQuitting: true + }) + + consoleError.mockRestore() + }) + + it('allows close when Electron reports a crashed webContents', () => { + const windowHandlers: Record void> = {} + const webContents = { + on: vi.fn((event, handler) => { + windowHandlers[event] = handler + }), + setZoomLevel: vi.fn(), + setBackgroundThrottling: vi.fn(), + invalidate: vi.fn(), + setWindowOpenHandler: vi.fn(), + send: vi.fn(), + isCrashed: vi.fn(() => true) + } + const browserWindowInstance = { + webContents, + on: vi.fn((event, handler) => { + windowHandlers[event] = handler + }), + isDestroyed: vi.fn(() => false), + isMaximized: vi.fn(() => true), + isFullScreen: vi.fn(() => false), + getSize: vi.fn(() => [1200, 800]), + setSize: vi.fn(), + maximize: vi.fn(), + show: vi.fn(), + loadFile: vi.fn(), + loadURL: vi.fn() + } + browserWindowMock.mockImplementation(function () { + return browserWindowInstance + }) + + createMainWindow(null, { getIsQuitting: () => true }) + + const preventDefault = vi.fn() + windowHandlers.close({ preventDefault } as never) + + expect(preventDefault).not.toHaveBeenCalled() + expect(webContents.send).not.toHaveBeenCalledWith('window:close-requested', { + isQuitting: true + }) + }) + it('ignores traffic light sync IPC on non-macOS', () => { const windowHandlers: Record void> = {} const webContents = { diff --git a/src/main/window/createMainWindow.ts b/src/main/window/createMainWindow.ts index 149de5b9e..d0c6f054c 100644 --- a/src/main/window/createMainWindow.ts +++ b/src/main/window/createMainWindow.ts @@ -460,13 +460,21 @@ export function createMainWindow( const resetMarkdownEditorFocus = (): void => { markdownEditorFocused = false } - mainWindow.webContents.on('render-process-gone', resetMarkdownEditorFocus) + let rendererProcessGone = false + mainWindow.webContents.on('render-process-gone', (_event, details) => { + rendererProcessGone = true + resetMarkdownEditorFocus() + console.error('[window] Renderer process gone; close confirmation will be bypassed', details) + }) mainWindow.webContents.on('destroyed', resetMarkdownEditorFocus) mainWindow.webContents.on('did-start-navigation', (_e, _url, _isInPlace, isMainFrame) => { if (isMainFrame) { resetMarkdownEditorFocus() } }) + mainWindow.webContents.on('did-finish-load', () => { + rendererProcessGone = false + }) mainWindow.webContents.on('before-input-event', (event, input) => { if (input.type === 'keyDown' && is.dev && input.code === 'F12') { @@ -623,6 +631,18 @@ export function createMainWindow( } return } + const isRendererCrashed = mainWindow.webContents.isCrashed?.() ?? false + if (rendererProcessGone || isRendererCrashed) { + // Why: after a native renderer crash the renderer cannot answer + // window:close-requested. Let Cmd+Q / OS close complete instead of + // trapping the user in a blank, unquittable window. + windowClosing = true + if (boundsTimer) { + clearTimeout(boundsTimer) + boundsTimer = null + } + return + } e.preventDefault() mainWindow.webContents.send('window:close-requested', { isQuitting: opts?.getIsQuitting?.() ?? false