From 20b4813cf83882c01e0ea83c091ca95376f92614 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 1 Nov 2021 05:32:54 +0100 Subject: [PATCH] fix: crash on close window when webContents destroyed (#31620) --- shell/browser/api/electron_api_browser_window.cc | 2 +- spec-main/api-browser-window-spec.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/shell/browser/api/electron_api_browser_window.cc b/shell/browser/api/electron_api_browser_window.cc index 5434d713535e..eb54e2e6eab9 100644 --- a/shell/browser/api/electron_api_browser_window.cc +++ b/shell/browser/api/electron_api_browser_window.cc @@ -254,7 +254,7 @@ void BrowserWindow::OnCloseButtonClicked(bool* prevent_default) { ScheduleUnresponsiveEvent(5000); // Already closed by renderer. - if (!web_contents()) + if (!web_contents() || !api_web_contents_) return; // Required to make beforeunload handler work. diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 26f89f28f30b..f861708b5968 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -110,6 +110,11 @@ describe('BrowserWindow module', () => { await closed; }); + it('should not crash if called after webContents is destroyed', () => { + w.webContents.destroy(); + w.webContents.on('destroyed', () => w.close()); + }); + it('should emit unload handler', async () => { await w.loadFile(path.join(fixtures, 'api', 'unload.html')); const closed = emittedOnce(w, 'closed');