Skip to content

Commit 91e08f6

Browse files
PR Batch Testerclaude
andcommitted
fix(desktop): time a stalled quit on the monotonic clock
Review follow-up. A wall-clock jump could force an early exit or keep ignoring Quit for longer, so measure the pending shutdown with Clock.monotonicTimeNanos. Name startWaitingForShutdown with Effect.fn, and test a repeat just under the 10 second threshold. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bbc99df commit 91e08f6

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,19 @@ describe("DesktopLifecycle", () => {
259259
} as Electron.Event;
260260
appListeners.get("before-quit")?.(event);
261261
appListeners.get("before-quit")?.(event);
262-
assert.deepEqual(exitCodes, [], "a quick repeat must not cut a normal shutdown short");
262+
yield* TestClock.adjust("9999 millis");
263+
appListeners.get("before-quit")?.(event);
264+
assert.deepEqual(
265+
exitCodes,
266+
[],
267+
"a repeat before the threshold must not cut shutdown short",
268+
);
263269

264-
yield* TestClock.adjust("10 seconds");
270+
yield* TestClock.adjust("1 millis");
265271
appListeners.get("before-quit")?.(event);
266272

267273
assert.deepEqual(exitCodes, [0]);
268-
assert.equal(prevented, 3);
274+
assert.equal(prevented, 4);
269275
}),
270276
).pipe(Effect.provide(layer));
271277
}),

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,20 @@ const requestDesktopShutdownAndWait = Effect.fn("desktop.lifecycle.requestShutdo
9898

9999
interface QuitState {
100100
phase: "running" | "waiting" | "allowed";
101-
waitingSince: number;
101+
waitingSinceNanos: bigint;
102102
}
103103

104104
// 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.
105+
// so a double Cmd-Q at the start of a normal quit cannot cut the backend stop
106+
// short and orphan it.
106107
const SHUTDOWN_STALL_THRESHOLD = Duration.seconds(10);
107108

108-
const startWaitingForShutdown = (quit: QuitState) =>
109-
Effect.map(Clock.currentTimeMillis, (now) => {
110-
quit.phase = "waiting";
111-
quit.waitingSince = now;
112-
});
109+
const startWaitingForShutdown = Effect.fn("desktop.lifecycle.startWaitingForShutdown")(function* (
110+
quit: QuitState,
111+
) {
112+
quit.phase = "waiting";
113+
quit.waitingSinceNanos = yield* Clock.monotonicTimeNanos;
114+
});
113115

114116
// While a quit waits on shutdown, `quitting` makes activation open nothing. If
115117
// that shutdown stalls, a repeated Quit or SIGTERM is the user's only way out
@@ -118,7 +120,9 @@ const exitIfShutdownStalled = Effect.fn("desktop.lifecycle.exitIfShutdownStalled
118120
quit: QuitState,
119121
source: string,
120122
) {
121-
const pendingMs = (yield* Clock.currentTimeMillis) - quit.waitingSince;
123+
const pendingMs = Number(
124+
((yield* Clock.monotonicTimeNanos) - quit.waitingSinceNanos) / 1_000_000n,
125+
);
122126
if (pendingMs < Duration.toMillis(SHUTDOWN_STALL_THRESHOLD)) {
123127
yield* logLifecycleInfo("quit already in progress", { source, pendingMs });
124128
return;
@@ -241,7 +245,7 @@ export const make = DesktopLifecycle.of({
241245
const environment = yield* DesktopEnvironment.DesktopEnvironment;
242246
const context = yield* Effect.context<DesktopLifecycleRegistrationServices>();
243247
const runEffect = Effect.runPromiseWith(context);
244-
const quit: QuitState = { phase: "running", waitingSince: 0 };
248+
const quit: QuitState = { phase: "running", waitingSinceNanos: 0n };
245249
yield* electronTheme.onUpdated(() => {
246250
void runEffect(
247251
desktopWindow.syncAppearance.pipe(Effect.withSpan("desktop.lifecycle.themeUpdated")),

0 commit comments

Comments
 (0)