fix(desktop): shut down cleanly with workspace terminals (#3268)
Workspace terminal shutdown only signaled an npm wrapper, leaving Electron alive with broken output pipes. Make the signal-handling runner the terminal-owned process and route external Electron termination through the existing quit lifecycle.
This commit is contained in:
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vitest";
|
||||
import { DEFAULT_DESKTOP_SETTINGS } from "../settings/desktop-settings";
|
||||
import {
|
||||
createQuitLifecycle,
|
||||
registerExternalQuitSignals,
|
||||
shouldStopDesktopManagedDaemonOnQuit,
|
||||
stopDesktopManagedDaemonOnQuitIfNeeded,
|
||||
} from "./quit-lifecycle";
|
||||
@@ -29,6 +30,25 @@ function waitForQuitLifecycle(): Promise<void> {
|
||||
}
|
||||
|
||||
describe("quit-lifecycle", () => {
|
||||
it("turns external termination signals into one Electron quit", () => {
|
||||
const listeners = new Map<NodeJS.Signals, () => void>();
|
||||
const quits: string[] = [];
|
||||
|
||||
registerExternalQuitSignals({
|
||||
signals: {
|
||||
on: (signal, listener) => {
|
||||
listeners.set(signal, listener);
|
||||
},
|
||||
},
|
||||
quit: () => quits.push("quit"),
|
||||
});
|
||||
|
||||
expect(Array.from(listeners.keys())).toEqual(["SIGHUP", "SIGINT", "SIGTERM"]);
|
||||
listeners.get("SIGTERM")?.();
|
||||
listeners.get("SIGHUP")?.();
|
||||
expect(quits).toEqual(["quit"]);
|
||||
});
|
||||
|
||||
it("stops by default and only keeps running when keepRunningAfterQuit is enabled", () => {
|
||||
expect(shouldStopDesktopManagedDaemonOnQuit(SETTINGS_STOP_ON_QUIT)).toBe(true);
|
||||
expect(shouldStopDesktopManagedDaemonOnQuit(SETTINGS_KEEP_RUNNING)).toBe(false);
|
||||
|
||||
@@ -14,6 +14,10 @@ interface BeforeQuitApp {
|
||||
exit(code: number): void;
|
||||
}
|
||||
|
||||
interface ExternalQuitSignalSource {
|
||||
on(signal: NodeJS.Signals, listener: () => void): unknown;
|
||||
}
|
||||
|
||||
interface QuitLifecycle {
|
||||
handleBeforeQuit(event: BeforeQuitEvent): void;
|
||||
handleBeforeQuitForUpdate(): void;
|
||||
@@ -31,6 +35,23 @@ export interface StopOnQuitDeps {
|
||||
showShutdownFeedback: () => void;
|
||||
}
|
||||
|
||||
export function registerExternalQuitSignals({
|
||||
signals,
|
||||
quit,
|
||||
}: {
|
||||
signals: ExternalQuitSignalSource;
|
||||
quit: () => void;
|
||||
}): void {
|
||||
let quitRequested = false;
|
||||
for (const signal of ["SIGHUP", "SIGINT", "SIGTERM"] satisfies NodeJS.Signals[]) {
|
||||
signals.on(signal, () => {
|
||||
if (quitRequested) return;
|
||||
quitRequested = true;
|
||||
quit();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function shouldStopDesktopManagedDaemonOnQuit(settings: QuitLifecycleSettings): boolean {
|
||||
return !settings.daemon.keepRunningAfterQuit;
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ import {
|
||||
} from "./daemon/daemon-manager.js";
|
||||
import {
|
||||
createQuitLifecycle,
|
||||
registerExternalQuitSignals,
|
||||
stopDesktopManagedDaemonOnQuitIfNeeded,
|
||||
} from "./daemon/quit-lifecycle.js";
|
||||
import { runDesktopStartup } from "./desktop-startup.js";
|
||||
@@ -1081,6 +1082,7 @@ const quitLifecycle = createQuitLifecycle({
|
||||
// electron-updater forwards this event through Electron's built-in autoUpdater.
|
||||
electronAutoUpdater.on("before-quit-for-update", quitLifecycle.handleBeforeQuitForUpdate);
|
||||
app.on("before-quit", quitLifecycle.handleBeforeQuit);
|
||||
registerExternalQuitSignals({ signals: process, quit: () => app.quit() });
|
||||
|
||||
app.on("window-all-closed", () => {
|
||||
if (process.platform !== "darwin") {
|
||||
|
||||
Reference in New Issue
Block a user