Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(captions): stop whisper helper on app quit

Vitali Gusatinsky3a35d4d7f25c
diff --git a/electron/main.ts b/electron/main.ts
index 60313e3ed..b53f9107e 100644
--- a/electron/main.ts
+++ b/electron/main.ts
@@ -38,7 +38,7 @@ import {
} from "./install-channel";
import { getSelectedDesktopSource, registerIpcHandlers } from "./ipc/handlers";
import { installMainProcessErrorGuards } from "./main-process-errors";
-import { registerSttIpc } from "./stt";
+import { registerSttIpc, shutdownStt } from "./stt";
import { checkLatestRelease } from "./update-checker";
import {
createCountdownOverlayWindow,
@@ -684,10 +684,31 @@ app.on("activate", () => {
}
});
-app.on("before-quit", () => {
+let sttShutdownPromise: Promise<void> | null = null;
+let sttShutdownFinished = false;
+
+// Electron does not wait for an async event listener. Hold the first quit long
+// enough to terminate the long-lived Whisper helper, then re-enter app.quit()
+// with a guard so the second before-quit event can proceed normally. Without
+// this, a normal Cmd+Q orphaned the helper under launchd with the model and GPU
+// resources still resident after every OpenScreen window had gone away.
+app.on("before-quit", (event) => {
// A check started seconds ago must not settle after the app is gone and try to open a
- // dialog on a quitting app.
+ // dialog on a quitting app. Aborting on the FIRST quit is deliberate even though that
+ // quit is deferred below: the user asked to leave, and a check they can re-run from the
+ // tray is not worth holding the helper's teardown behind.
updateCheckAbort?.abort();
+ if (sttShutdownFinished) return;
+ event.preventDefault();
+ if (sttShutdownPromise) return;
+ sttShutdownPromise = shutdownStt()
+ .catch((error) => {
+ console.error("[stt] Failed to stop whisper helper during app quit:", error);
+ })
+ .finally(() => {
+ sttShutdownFinished = true;
+ app.quit();
+ });
});
app.on("will-quit", () => {
diff --git a/electron/stt/index.test.ts b/electron/stt/index.test.ts
index ad0eaf976..335df794b 100644
--- a/electron/stt/index.test.ts
+++ b/electron/stt/index.test.ts
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { planChunks } from "./chunking";
-import { _resetSttManagerForTests, SttManager } from "./index";
+import { _resetSttManagerForTests, getSttManager, SttManager, shutdownStt } from "./index";
import type { SttStatusEvent, SttTranscribeResponse } from "./transcriptionContract";
// We swap the long-lived modules for fakes so the manager's `init()` and
@@ -201,6 +201,16 @@ describe("SttManager", () => {
expect(fakeWhisperServer.stop).toHaveBeenCalledOnce();
});
+ it("shutdownStt() stops and releases the singleton exactly once", async () => {
+ const mgr = getSttManager();
+ await mgr.init({ modelsBaseDir: "/tmp/fake-stt-models" });
+
+ await shutdownStt();
+ await shutdownStt();
+
+ expect(fakeWhisperServer.stop).toHaveBeenCalledOnce();
+ });
+
it("retries setup after a failed one instead of caching the rejection", async () => {
// First run downloads a 253 MB model. Caching a rejected `prepare()` meant
// one dropped connection failed every later transcription in the session —
diff --git a/electron/stt/index.ts b/electron/stt/index.ts
index 398ed3097..ee6be4664 100644
--- a/electron/stt/index.ts
+++ b/electron/stt/index.ts
@@ -20,7 +20,7 @@ import { WhisperServerManager } from "./whisperServer";
* (`chunking.ts`) and runs each through whisper-stt-server's HTTP
* `/inference`, which returns both phrase- and word-level segments in one
* pass (see whisperServer.ts). Word timestamps come from whisper.cpp's
- * native DTW token timestamps (`t_dtw`, SMALL aheads preset,
+ * native DTW token timestamps (`t_dtw`, LARGE_V3_TURBO aheads preset,
* `flash_attn = false`), see
* technical-documentation/architecture/transcription-and-captions.md § Decision rationale.
* 3. `shutdown()` tears down on app quit.
@@ -310,6 +310,18 @@ export function getSttManager(): SttManager {
return singleton;
}
+/**
+ * Stop and release the lazy singleton without creating one just to quit.
+ *
+ * Electron's GUI lifecycle awaits this from a guarded `before-quit` handler;
+ * clearing the slot first also makes repeated quit events idempotent.
+ */
+export async function shutdownStt(): Promise<void> {
+ const manager = singleton;
+ singleton = null;
+ await manager?.shutdown();
+}
+
/** Reset the singleton — for tests. */
export function _resetSttManagerForTests(): void {
singleton = null;
diff --git a/electron/stt/whisperServer.ts b/electron/stt/whisperServer.ts
index 12313df94..dd04d144a 100644
--- a/electron/stt/whisperServer.ts
+++ b/electron/stt/whisperServer.ts
@@ -42,7 +42,7 @@ const REQUEST_TIMEOUT_MS = 280_000;
* and the renderer doesn't move.
*
* Word timestamps come from whisper.cpp's native DTW token timestamps
- * (`t_dtw`, SMALL aheads preset, `flash_attn = false` so DTW is actually
+ * (`t_dtw`, LARGE_V3_TURBO aheads preset, `flash_attn = false` so DTW is actually
* computed). The helper returns them already absolute, so no segment-offset
* arithmetic is required.
*
@@ -52,7 +52,7 @@ const REQUEST_TIMEOUT_MS = 280_000;
*/
export interface WhisperServerStartOptions {
- /** Absolute path to the GGML model file (e.g. ggml-small-q8_0.bin). */
+ /** Absolute path to the GGML model file (e.g. ggml-large-v3-turbo-q5_0.bin). */
modelPath: string;
/** Externally-resolved binary path (skips gpuDetector on startup); null = auto. */
binaryPath?: string | null;