Fix close after renderer crash
This commit is contained in:
@@ -567,6 +567,217 @@ describe('createMainWindow', () => {
|
||||
expect(onQuitAborted).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('allows close after the renderer process is gone', () => {
|
||||
const windowHandlers: Record<string, (...args: any[]) => 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<string, (...args: any[]) => 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<string, (...args: any[]) => 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<string, (...args: any[]) => 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<string, (...args: any[]) => void> = {}
|
||||
const webContents = {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user