From 2884dea195edb524b80087e7b55de0c6584316a5 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 31 Mar 2023 14:21:07 -0400 Subject: [PATCH] Fix app crash on quit (#7407) (#7452) * Fix app crash on quit (#7407) * Fix app crash on quit Signed-off-by: Sebastian Malton * Back out disabling extensions on quit Signed-off-by: Sebastian Malton --------- Signed-off-by: Sebastian Malton * Fixup cherry-pick Signed-off-by: Sebastian Malton --------- Signed-off-by: Sebastian Malton --- ...efore-closing-of-application.injectable.ts | 45 +++++++++++++++---- .../create-lens-window.injectable.ts | 4 +- .../runnable-tokens/phases.ts | 2 +- .../resolve-system-proxy-window.injectable.ts | 4 +- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/packages/core/src/main/electron-app/runnables/setup-runnables-before-closing-of-application.injectable.ts b/packages/core/src/main/electron-app/runnables/setup-runnables-before-closing-of-application.injectable.ts index 0ba1b68137..9843d2c223 100644 --- a/packages/core/src/main/electron-app/runnables/setup-runnables-before-closing-of-application.injectable.ts +++ b/packages/core/src/main/electron-app/runnables/setup-runnables-before-closing-of-application.injectable.ts @@ -8,7 +8,7 @@ import { beforeQuitOfFrontEndInjectionToken, beforeQuitOfBackEndInjectionToken } import electronAppInjectable from "../electron-app.injectable"; import isIntegrationTestingInjectable from "../../../common/vars/is-integration-testing.injectable"; import autoUpdaterInjectable from "../features/auto-updater.injectable"; -import { runManySyncFor } from "@k8slens/run-many"; +import { runManySyncFor, runManyFor } from "@k8slens/run-many"; const setupRunnablesBeforeClosingOfApplicationInjectable = getInjectable({ id: "setup-closing-of-application", @@ -16,8 +16,9 @@ const setupRunnablesBeforeClosingOfApplicationInjectable = getInjectable({ instantiate: (di) => ({ run: () => { const runManySync = runManySyncFor(di); + const runMany = runManyFor(di); const runRunnablesBeforeQuitOfFrontEnd = runManySync(beforeQuitOfFrontEndInjectionToken); - const runRunnablesBeforeQuitOfBackEnd = runManySync(beforeQuitOfBackEndInjectionToken); + const runRunnablesBeforeQuitOfBackEnd = runMany(beforeQuitOfBackEndInjectionToken); const app = di.inject(electronAppInjectable); const isIntegrationTesting = di.inject(isIntegrationTestingInjectable); const autoUpdater = di.inject(autoUpdaterInjectable); @@ -27,17 +28,43 @@ const setupRunnablesBeforeClosingOfApplicationInjectable = getInjectable({ isAutoUpdating = true; }); - app.on("will-quit", (event) => { + app.on("will-quit", () => { runRunnablesBeforeQuitOfFrontEnd(); - const shouldQuitBackEnd = isIntegrationTesting || isAutoUpdating; + let isAsyncQuitting = false; - if (shouldQuitBackEnd) { - runRunnablesBeforeQuitOfBackEnd(); - } else { - // IMPORTANT: This cannot be destructured as it would break binding of "this" for the Electron event + const doAsyncQuit = (event: Electron.Event, exitCode = 0) => { + if (isAsyncQuitting) { + return; + } + + isAsyncQuitting = true; + + void (async () => { + try { + await runRunnablesBeforeQuitOfBackEnd(); + } catch (error) { + console.error("A beforeQuitOfBackEnd failed!!!!", error); + exitCode = 1; + } + + app.exit(exitCode); + })(); + }; + + app.on("will-quit", (event) => { + runRunnablesBeforeQuitOfFrontEnd(); event.preventDefault(); - } + + if (isIntegrationTesting || isAutoUpdating) { + doAsyncQuit(event); + } + }); + + app.on("quit", (event, exitCode) => { + event.preventDefault(); + doAsyncQuit(event, exitCode); + }); }); return undefined; diff --git a/packages/core/src/main/start-main-application/lens-window/application-window/create-lens-window.injectable.ts b/packages/core/src/main/start-main-application/lens-window/application-window/create-lens-window.injectable.ts index e3854f6df6..3d6e636c8a 100644 --- a/packages/core/src/main/start-main-application/lens-window/application-window/create-lens-window.injectable.ts +++ b/packages/core/src/main/start-main-application/lens-window/application-window/create-lens-window.injectable.ts @@ -4,7 +4,7 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import type { ContentSource, ElectronWindowTitleBarStyle } from "./create-electron-window.injectable"; -import createElectronWindowForInjectable from "./create-electron-window.injectable"; +import createElectronWindowInjectable from "./create-electron-window.injectable"; import type { ClusterFrameInfo } from "../../../../common/cluster-frames.injectable"; export interface ElectronWindow { @@ -59,7 +59,7 @@ const createLensWindowInjectable = getInjectable({ id: "create-lens-window", instantiate: (di) => { - const createElectronWindow = di.inject(createElectronWindowForInjectable); + const createElectronWindow = di.inject(createElectronWindowInjectable); return (configuration: LensWindowConfiguration): LensWindow => { let browserWindow: ElectronWindow | undefined; diff --git a/packages/core/src/main/start-main-application/runnable-tokens/phases.ts b/packages/core/src/main/start-main-application/runnable-tokens/phases.ts index 5808062fb2..b5844436e1 100644 --- a/packages/core/src/main/start-main-application/runnable-tokens/phases.ts +++ b/packages/core/src/main/start-main-application/runnable-tokens/phases.ts @@ -10,7 +10,7 @@ export const beforeQuitOfFrontEndInjectionToken = getInjectionToken({ +export const beforeQuitOfBackEndInjectionToken = getInjectionToken({ id: "before-quit-of-back-end", }); diff --git a/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-window.injectable.ts b/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-window.injectable.ts index 9dcfed4edf..91536c8204 100644 --- a/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-window.injectable.ts +++ b/packages/core/src/main/utils/resolve-system-proxy/resolve-system-proxy-window.injectable.ts @@ -12,13 +12,13 @@ const resolveSystemProxyWindowInjectable = getInjectable({ const app = di.inject(electronAppInjectable); await app.whenReady(); - + const window = new BrowserWindow({ show: false, paintWhenInitiallyHidden: false, }); - window.hide(); + window.hide(); return window; },