Skip to content

Commit bbc99df

Browse files
PR Batch Testerclaude
andcommitted
fix(desktop): a stuck quit no longer leaves the app unkillable
Once a quit starts, the app sets `quitting`, hides its windows and waits for shutdown. If that shutdown never finishes, every later Quit and SIGTERM was swallowed, so only SIGKILL could end the app. Now a Quit or SIGTERM that arrives after shutdown has been pending for 10 seconds exits the app. A repeat inside that window is ignored as before, so a double Cmd-Q during a normal quit cannot orphan the backend. Refs #23843 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2c8bdf6 commit bbc99df

2 files changed

Lines changed: 105 additions & 18 deletions

File tree

‎apps/desktop/src/app/DesktopLifecycle.test.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Deferred from "effect/Deferred";
33
import * as Effect from "effect/Effect";
44
import * as Layer from "effect/Layer";
55
import * as Ref from "effect/Ref";
6+
import * as TestClock from "effect/testing/TestClock";
67

78
import type * as Electron from "electron";
89

@@ -18,6 +19,7 @@ import * as DesktopWindow from "../window/DesktopWindow.ts";
1819
function layerElectronApp(
1920
appListeners: Map<string, (...args: readonly unknown[]) => void>,
2021
quit: Effect.Effect<void> = Effect.void,
22+
exit: (code: number) => Effect.Effect<void> = () => Effect.void,
2123
) {
2224
const registerListener = (eventName: string, listener: (...args: readonly unknown[]) => void) =>
2325
Effect.acquireRelease(
@@ -37,7 +39,7 @@ function layerElectronApp(
3739
whenReady: Effect.void,
3840
requestSingleInstanceLock: Effect.succeed(true),
3941
quit,
40-
exit: () => Effect.void,
42+
exit,
4143
relaunch: () => Effect.void,
4244
setPath: () => Effect.void,
4345
setName: () => Effect.void,
@@ -221,6 +223,54 @@ describe("DesktopLifecycle", () => {
221223
}),
222224
);
223225

226+
it.effect("exits on a repeated quit only once shutdown has stalled", () =>
227+
Effect.gen(function* () {
228+
const appListeners = new Map<string, (...args: readonly unknown[]) => void>();
229+
const exitCodes: number[] = [];
230+
const exit = (code: number) =>
231+
Effect.sync(() => {
232+
exitCodes.push(code);
233+
});
234+
const layerEnvironment = Layer.succeed(DesktopEnvironment.DesktopEnvironment, {
235+
platform: "darwin",
236+
isDevelopment: false,
237+
} as DesktopEnvironment.DesktopEnvironment["Service"]);
238+
const layer = DesktopLifecycle.layer.pipe(
239+
Layer.provideMerge(layerElectronApp(appListeners, Effect.void, exit)),
240+
Layer.provideMerge(layerElectronTheme),
241+
Layer.provideMerge(layerElectronWindow()),
242+
Layer.provideMerge(layerDesktopWindow()),
243+
Layer.provideMerge(layerEnvironment),
244+
// Nothing marks this shutdown complete, so it stays pending.
245+
Layer.provideMerge(DesktopShutdown.layer),
246+
Layer.provideMerge(DesktopState.layer),
247+
);
248+
249+
yield* Effect.scoped(
250+
Effect.gen(function* () {
251+
const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
252+
yield* lifecycle.register;
253+
254+
let prevented = 0;
255+
const event = {
256+
preventDefault: () => {
257+
prevented += 1;
258+
},
259+
} as Electron.Event;
260+
appListeners.get("before-quit")?.(event);
261+
appListeners.get("before-quit")?.(event);
262+
assert.deepEqual(exitCodes, [], "a quick repeat must not cut a normal shutdown short");
263+
264+
yield* TestClock.adjust("10 seconds");
265+
appListeners.get("before-quit")?.(event);
266+
267+
assert.deepEqual(exitCodes, [0]);
268+
assert.equal(prevented, 3);
269+
}),
270+
).pipe(Effect.provide(layer));
271+
}),
272+
);
273+
224274
it.effect("ignores app activation while quitting", () =>
225275
Effect.gen(function* () {
226276
const appListeners = new Map<string, (...args: readonly unknown[]) => void>();

‎apps/desktop/src/app/DesktopLifecycle.ts‎

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import * as Clock from "effect/Clock";
12
import * as Context from "effect/Context";
3+
import * as Duration from "effect/Duration";
24
import * as Effect from "effect/Effect";
35
import * as Layer from "effect/Layer";
46
import * as Ref from "effect/Ref";
@@ -94,15 +96,46 @@ const requestDesktopShutdownAndWait = Effect.fn("desktop.lifecycle.requestShutdo
9496
},
9597
);
9698

99+
interface QuitState {
100+
phase: "running" | "waiting" | "allowed";
101+
waitingSince: number;
102+
}
103+
104+
// Longer than a backend's stop budget (SIGTERM, 5s grace, SIGKILL, 1s wait),
105+
// so a repeated Cmd-Q during a normal quit cannot orphan the backend.
106+
const SHUTDOWN_STALL_THRESHOLD = Duration.seconds(10);
107+
108+
const startWaitingForShutdown = (quit: QuitState) =>
109+
Effect.map(Clock.currentTimeMillis, (now) => {
110+
quit.phase = "waiting";
111+
quit.waitingSince = now;
112+
});
113+
114+
// While a quit waits on shutdown, `quitting` makes activation open nothing. If
115+
// that shutdown stalls, a repeated Quit or SIGTERM is the user's only way out
116+
// short of SIGKILL.
117+
const exitIfShutdownStalled = Effect.fn("desktop.lifecycle.exitIfShutdownStalled")(function* (
118+
quit: QuitState,
119+
source: string,
120+
) {
121+
const pendingMs = (yield* Clock.currentTimeMillis) - quit.waitingSince;
122+
if (pendingMs < Duration.toMillis(SHUTDOWN_STALL_THRESHOLD)) {
123+
yield* logLifecycleInfo("quit already in progress", { source, pendingMs });
124+
return;
125+
}
126+
yield* logLifecycleError("desktop shutdown stalled; exiting without it", { source, pendingMs });
127+
const electronApp = yield* ElectronApp.ElectronApp;
128+
yield* electronApp.exit(0);
129+
});
130+
97131
function handleBeforeQuit(
98132
event: Electron.Event,
99133
runEffect: <A, E>(
100134
effect: Effect.Effect<A, E, DesktopLifecycleRegistrationServices>,
101135
) => Promise<A>,
102-
allowQuit: () => boolean,
103-
markQuitAllowed: () => void,
136+
quit: QuitState,
104137
): void {
105-
if (allowQuit()) {
138+
if (quit.phase === "allowed") {
106139
void runEffect(
107140
Effect.gen(function* () {
108141
const state = yield* DesktopState.DesktopState;
@@ -114,10 +147,15 @@ function handleBeforeQuit(
114147
}
115148

116149
event.preventDefault();
150+
if (quit.phase === "waiting") {
151+
void runEffect(exitIfShutdownStalled(quit, "before-quit"));
152+
return;
153+
}
117154
void runEffect(
118155
Effect.gen(function* () {
119156
const state = yield* DesktopState.DesktopState;
120157
const electronWindow = yield* ElectronWindow.ElectronWindow;
158+
yield* startWaitingForShutdown(quit);
121159
yield* Ref.set(state.quitting, true);
122160
yield* logLifecycleInfo("before-quit received");
123161
yield* requestDesktopShutdownAndWait(
@@ -129,7 +167,7 @@ function handleBeforeQuit(
129167
);
130168
}).pipe(Effect.withSpan("desktop.lifecycle.beforeQuit")),
131169
).finally(() => {
132-
markQuitAllowed();
170+
quit.phase = "allowed";
133171
void runEffect(
134172
Effect.gen(function* () {
135173
const electronApp = yield* ElectronApp.ElectronApp;
@@ -144,16 +182,23 @@ function quitFromSignal(
144182
runEffect: <A, E>(
145183
effect: Effect.Effect<A, E, DesktopLifecycleRegistrationServices>,
146184
) => Promise<A>,
185+
quit: QuitState,
147186
): void {
187+
if (quit.phase === "waiting") {
188+
void runEffect(exitIfShutdownStalled(quit, signal));
189+
return;
190+
}
148191
void runEffect(
149192
Effect.gen(function* () {
150193
yield* Effect.annotateCurrentSpan({ signal });
151194
const electronApp = yield* ElectronApp.ElectronApp;
152195
const state = yield* DesktopState.DesktopState;
153196
const wasQuitting = yield* Ref.getAndSet(state.quitting, true);
154197
if (wasQuitting) return;
198+
yield* startWaitingForShutdown(quit);
155199
yield* logLifecycleInfo("process signal received", { signal });
156200
yield* requestDesktopShutdownAndWait();
201+
quit.phase = "allowed";
157202
yield* electronApp.quit;
158203
}).pipe(Effect.withSpan("desktop.lifecycle.processSignal")),
159204
);
@@ -196,8 +241,7 @@ export const make = DesktopLifecycle.of({
196241
const environment = yield* DesktopEnvironment.DesktopEnvironment;
197242
const context = yield* Effect.context<DesktopLifecycleRegistrationServices>();
198243
const runEffect = Effect.runPromiseWith(context);
199-
let quitAllowed = false;
200-
let updaterQuitAllowed = false;
244+
const quit: QuitState = { phase: "running", waitingSince: 0 };
201245
yield* electronTheme.onUpdated(() => {
202246
void runEffect(
203247
desktopWindow.syncAppearance.pipe(Effect.withSpan("desktop.lifecycle.themeUpdated")),
@@ -207,7 +251,7 @@ export const make = DesktopLifecycle.of({
207251
// Electron's updater owns the remaining quit/install/relaunch sequence.
208252
// Cancelling the following app "before-quit" event breaks that sequence,
209253
// most visibly on macOS where the native updater performs the relaunch.
210-
updaterQuitAllowed = true;
254+
quit.phase = "allowed";
211255
// This event is synchronous and the updater's quit proceeds as soon as
212256
// the listener returns, so a forked destroyAll would race the quit
213257
// and windows could still be open when the process exits (visible on
@@ -223,14 +267,7 @@ export const make = DesktopLifecycle.of({
223267
);
224268
});
225269
yield* electronApp.on("before-quit", (event: Electron.Event) => {
226-
handleBeforeQuit(
227-
event,
228-
runEffect,
229-
() => quitAllowed || updaterQuitAllowed,
230-
() => {
231-
quitAllowed = true;
232-
},
233-
);
270+
handleBeforeQuit(event, runEffect, quit);
234271
});
235272
yield* electronApp.on("activate", () => {
236273
void runEffect(
@@ -255,10 +292,10 @@ export const make = DesktopLifecycle.of({
255292

256293
if (environment.platform !== "win32") {
257294
yield* addScopedListener(process, "SIGINT", () => {
258-
quitFromSignal("SIGINT", runEffect);
295+
quitFromSignal("SIGINT", runEffect, quit);
259296
});
260297
yield* addScopedListener(process, "SIGTERM", () => {
261-
quitFromSignal("SIGTERM", runEffect);
298+
quitFromSignal("SIGTERM", runEffect, quit);
262299
});
263300
}
264301
}).pipe(Effect.withSpan("desktop.lifecycle.register")),

0 commit comments

Comments
 (0)