This repository was archived by the owner on Jun 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
FEAT-177: fix CMD+Q not killing the process #48
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| export interface ShutdownDeps { | ||
| updateCheckTimer: NodeJS.Timeout | null; | ||
| clearUpdateCheckTimer: () => void; | ||
| cloudSocket: { stop: () => void }; | ||
| commandExecutor: { dispose: () => void }; | ||
| server: { stop: () => Promise<void> }; | ||
| desktopWindow: { dispose: () => void }; | ||
| tray: { dispose: () => void }; | ||
| } | ||
|
|
||
| export type ShutdownResult = "clean" | "timed_out" | "failed"; | ||
|
|
||
| export async function runShutdownSequence( | ||
| deps: ShutdownDeps, | ||
| options?: { timeoutMs?: number; setTimeoutFn?: typeof setTimeout } | ||
| ): Promise<ShutdownResult> { | ||
| const timeoutMs = options?.timeoutMs ?? 5000; | ||
| const setTimeoutFn = options?.setTimeoutFn ?? setTimeout; | ||
|
|
||
| let timer: ReturnType<typeof setTimeout> | null = null; | ||
|
|
||
| const cleanup = async (): Promise<"clean"> => { | ||
| deps.clearUpdateCheckTimer(); | ||
| deps.cloudSocket.stop(); | ||
| deps.commandExecutor.dispose(); | ||
| await deps.server.stop(); | ||
| deps.desktopWindow.dispose(); | ||
| deps.tray.dispose(); | ||
| return "clean"; | ||
| }; | ||
|
|
||
| const timeout = new Promise<"timed_out">((resolve) => { | ||
| timer = setTimeoutFn(() => resolve("timed_out"), timeoutMs); | ||
| }); | ||
|
|
||
| try { | ||
| const result = await Promise.race([cleanup(), timeout]); | ||
| return result; | ||
| } catch { | ||
| return "failed"; | ||
| } finally { | ||
| if (timer != null) { | ||
| clearTimeout(timer); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { describe, test } from "node:test"; | ||
| import { | ||
| runShutdownSequence, | ||
| type ShutdownDeps, | ||
| } from "../src/main/shutdown.js"; | ||
|
|
||
| /** Build stub deps that record call order. */ | ||
| function makeStubDeps(overrides?: Partial<ShutdownDeps>) { | ||
| const calls: string[] = []; | ||
| const deps: ShutdownDeps = { | ||
| updateCheckTimer: null, | ||
| clearUpdateCheckTimer: () => { | ||
| calls.push("clearUpdateCheckTimer"); | ||
| }, | ||
| cloudSocket: { | ||
| stop: () => { | ||
| calls.push("cloudSocket.stop"); | ||
| }, | ||
| }, | ||
| commandExecutor: { | ||
| dispose: () => { | ||
| calls.push("commandExecutor.dispose"); | ||
| }, | ||
| }, | ||
| server: { | ||
| stop: async () => { | ||
| calls.push("server.stop"); | ||
| }, | ||
| }, | ||
| desktopWindow: { | ||
| dispose: () => { | ||
| calls.push("desktopWindow.dispose"); | ||
| }, | ||
| }, | ||
| tray: { | ||
| dispose: () => { | ||
| calls.push("tray.dispose"); | ||
| }, | ||
| }, | ||
| ...overrides, | ||
| }; | ||
| return { deps, calls }; | ||
| } | ||
|
|
||
| describe("runShutdownSequence", () => { | ||
| test("clean path: all deps succeed, cleanup steps called in order", async () => { | ||
| const { deps, calls } = makeStubDeps(); | ||
|
|
||
| const result = await runShutdownSequence(deps); | ||
|
|
||
| assert.equal(result, "clean"); | ||
| assert.deepEqual(calls, [ | ||
| "clearUpdateCheckTimer", | ||
| "cloudSocket.stop", | ||
| "commandExecutor.dispose", | ||
| "server.stop", | ||
| "desktopWindow.dispose", | ||
| "tray.dispose", | ||
| ]); | ||
| }); | ||
|
|
||
| test("timeout path: result is 'timed_out' when server.stop never resolves", async () => { | ||
| const { deps } = makeStubDeps({ | ||
| server: { | ||
| stop: () => new Promise<void>(() => {}), // never resolves | ||
| }, | ||
| }); | ||
|
|
||
| // Stub setTimeoutFn that fires the callback immediately | ||
| const stubSetTimeout = ((cb: () => void) => { | ||
| cb(); | ||
| return 999 as unknown as ReturnType<typeof setTimeout>; | ||
| }) as unknown as typeof setTimeout; | ||
|
|
||
| const result = await runShutdownSequence(deps, { | ||
| setTimeoutFn: stubSetTimeout, | ||
| }); | ||
|
|
||
| assert.equal(result, "timed_out"); | ||
| }); | ||
|
|
||
| test("failed path: server.stop rejects with an error", async () => { | ||
| const { deps } = makeStubDeps({ | ||
| server: { | ||
| stop: () => Promise.reject(new Error("stop failed")), | ||
| }, | ||
| }); | ||
|
|
||
| // Use a setTimeoutFn that never fires so timeout doesn't win | ||
| const neverTimeout = (() => | ||
| 42 as unknown as ReturnType<typeof setTimeout>) as unknown as typeof setTimeout; | ||
|
|
||
| const result = await runShutdownSequence(deps, { | ||
| setTimeoutFn: neverTimeout, | ||
| }); | ||
|
|
||
| assert.equal(result, "failed"); | ||
| }); | ||
|
|
||
| test("timer is cleared after cleanup resolves (no leaked handles)", async () => { | ||
| const { deps } = makeStubDeps(); | ||
|
|
||
| let capturedTimerId: ReturnType<typeof setTimeout> | null = null; | ||
| let clearTimeoutCalledWith: unknown = null; | ||
|
|
||
| // Monkey-patch clearTimeout to observe the call | ||
| const origClearTimeout = globalThis.clearTimeout; | ||
| globalThis.clearTimeout = ((id: unknown) => { | ||
| clearTimeoutCalledWith = id; | ||
| origClearTimeout(id as ReturnType<typeof setTimeout>); | ||
| }) as typeof clearTimeout; | ||
|
|
||
| try { | ||
| // Use a real-ish setTimeoutFn that returns a recognizable timer id | ||
| const stubSetTimeout = ((_cb: () => void, _ms: number) => { | ||
| const id = origClearTimeout.bind( | ||
| null | ||
| ) as unknown as ReturnType<typeof setTimeout>; | ||
| capturedTimerId = 12345 as unknown as ReturnType<typeof setTimeout>; | ||
| return capturedTimerId; | ||
| }) as unknown as typeof setTimeout; | ||
|
|
||
| const result = await runShutdownSequence(deps, { | ||
| setTimeoutFn: stubSetTimeout, | ||
| }); | ||
|
|
||
| assert.equal(result, "clean"); | ||
| assert.equal( | ||
| clearTimeoutCalledWith, | ||
| capturedTimerId, | ||
| "clearTimeout should be called with the timer id returned by setTimeoutFn" | ||
| ); | ||
| } finally { | ||
| globalThis.clearTimeout = origClearTimeout; | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.