From b951d89c6a6b48bd41b3da01b8d747dc25e43d46 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Mon, 9 Jun 2025 19:50:05 +0800 Subject: [PATCH] feat: enhance unresponsive renderer handling and crash reporting (#6995) * feat: enhance unresponsive renderer handling and crash reporting * Added support for collecting JavaScript call stacks from unresponsive renderers. * Updated the Document Policy in the HTML to include JS call stacks in crash reports. * Removed legacy unresponsive logging from WindowService. * feat: improve unresponsive renderer handling and update crash reporting * Added session web request handling to include Document-Policy for JS call stacks in crash reports. * Removed legacy Document-Policy meta tag from HTML. * Enhanced logging for unresponsive renderer call stacks. * fix: remove unused session import in index.ts --------- Co-authored-by: beyondkmp --- src/main/index.ts | 20 ++++++++++++++++++++ src/main/services/WindowService.ts | 6 ------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index e53fbb4b42..3272887aa5 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -34,6 +34,26 @@ if (isWin) { app.commandLine.appendSwitch('wm-window-animations-disabled') } +// Enable features for unresponsive renderer js call stacks +app.commandLine.appendSwitch('enable-features', 'DocumentPolicyIncludeJSCallStacksInCrashReports') +app.on('web-contents-created', (_, webContents) => { + webContents.session.webRequest.onHeadersReceived((details, callback) => { + callback({ + responseHeaders: { + ...details.responseHeaders, + 'Document-Policy': ['include-js-call-stacks-in-crash-reports'] + } + }) + }) + + webContents.on('unresponsive', async () => { + // Interrupt execution and collect call stack from unresponsive renderer + Logger.error('Renderer unresponsive start') + const callStack = await webContents.mainFrame.collectJavaScriptCallStack() + Logger.error('Renderer unresponsive js call stack\n', callStack) + }) +}) + // in production mode, handle uncaught exception and unhandled rejection globally if (!isDev) { // handle uncaught exception diff --git a/src/main/services/WindowService.ts b/src/main/services/WindowService.ts index f5bcfe88f9..3f37d7c406 100644 --- a/src/main/services/WindowService.ts +++ b/src/main/services/WindowService.ts @@ -116,12 +116,6 @@ export class WindowService { app.exit(1) } }) - - mainWindow.webContents.on('unresponsive', () => { - // 在升级到electron 34后,可以获取具体js stack trace,目前只打个日志监控下 - // https://www.electronjs.org/blog/electron-34-0#unresponsive-renderer-javascript-call-stacks - Logger.error('Renderer process unresponsive') - }) } private setupMaximize(mainWindow: BrowserWindow, isMaximized: boolean) {