Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(meetings): wait for an in-flight local chunk before stopping (#2803)

Chad Piha43db446e1cb9
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 51dd583ee..2aaa6cd3e 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -8062,7 +8062,10 @@ class IPCHandlers {
let meetingLocalProvider = null;
let meetingLocalModel = null;
let meetingLocalLanguage = null;
- let meetingLocalTranscribing = false;
+ // The pass that is transcribing buffered audio, if any. Stop and rollback
+ // await it: a pass that finished after the reset would push its finals into
+ // the next session and send them to no window.
+ let meetingLocalTranscribePromise = null;
let meetingPendingMicChunks = [];
let meetingPendingMicFinals = [];
let meetingPendingMicFinalTimer = null;
@@ -8607,15 +8610,25 @@ class IPCHandlers {
}
};
- const transcribeAllLocalBuffers = async () => {
- if (meetingLocalTranscribing) return;
- meetingLocalTranscribing = true;
- try {
- await transcribeLocalMeetingChunk("system");
- await transcribeLocalMeetingChunk("mic");
- } finally {
- meetingLocalTranscribing = false;
+ const transcribeAllLocalBuffers = () => {
+ if (meetingLocalTranscribePromise) return meetingLocalTranscribePromise;
+ meetingLocalTranscribePromise = (async () => {
+ try {
+ await transcribeLocalMeetingChunk("system");
+ await transcribeLocalMeetingChunk("mic");
+ } finally {
+ meetingLocalTranscribePromise = null;
+ }
+ })();
+ return meetingLocalTranscribePromise;
+ };
+
+ const settleLocalMeetingTranscription = async () => {
+ if (meetingLocalTimer) {
+ clearInterval(meetingLocalTimer);
+ meetingLocalTimer = null;
}
+ await meetingLocalTranscribePromise?.catch(() => {});
};
const dropMeetingMicDiarizationCapture = () => {
@@ -8668,7 +8681,6 @@ class IPCHandlers {
meetingLocalProvider = null;
meetingLocalModel = null;
meetingLocalLanguage = null;
- meetingLocalTranscribing = false;
meetingPendingMicChunks = [];
resetPendingMicFinals();
meetingAecEnabled = false;
@@ -8910,8 +8922,10 @@ class IPCHandlers {
}
await stopMeetingAec();
await stopLiveSpeakerIdentification().catch(() => {});
- resetMeetingLocalState();
+ // Late finals from either path must land before the reset clears them.
+ await settleLocalMeetingTranscription();
await disconnectMeetingStreaming().catch(() => {});
+ resetMeetingLocalState();
this.activeMeetingSpeakerConfig = null;
};
@@ -9195,6 +9209,9 @@ class IPCHandlers {
// scoped stop/auto-end matching.
const recordingSessionId = options.sessionId;
meetingStartedAt = Date.now();
+ // Stop captures and clears the segments, but a stop that fails partway
+ // (the audio tap won't stop) never gets there.
+ meetingDiarizationSegments = [];
meetingConnectionOptions = options;
meetingConnectionWin = BrowserWindow.fromWebContents(event.sender);
meetingReconnectCount = 0;
@@ -9645,11 +9662,8 @@ class IPCHandlers {
const diarizationWin = meetingLocalWin || this.windowManager.controlPanelWindow;
if (meetingLocalMode) {
- if (meetingLocalTimer) {
- clearInterval(meetingLocalTimer);
- meetingLocalTimer = null;
- }
try {
+ await settleLocalMeetingTranscription();
await transcribeAllLocalBuffers();
} catch (err) {
debugLogger.error("Local meeting final transcription failed", { error: err.message });
diff --git a/test/helpers/meetingLocalStopRace.test.js b/test/helpers/meetingLocalStopRace.test.js
new file mode 100644
index 000000000..dfa556be0
--- /dev/null
+++ b/test/helpers/meetingLocalStopRace.test.js
@@ -0,0 +1,266 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const fs = require("node:fs");
+const path = require("node:path");
+const vm = require("node:vm");
+
+const source = fs.readFileSync(path.join(__dirname, "../../src/helpers/ipcHandlers.js"), "utf8");
+
+const sliceBetween = (startMarker, endMarker) => {
+ const start = source.indexOf(startMarker);
+ const end = source.indexOf(endMarker, start);
+ assert.ok(start >= 0 && end > start, `${startMarker} not found in ipcHandlers.js`);
+ return source.slice(start, end);
+};
+
+const deferred = () => {
+ let resolve;
+ const promise = new Promise((done) => (resolve = done));
+ return { promise, resolve };
+};
+
+// Arrays made inside the vm context have another realm's prototype.
+const plain = (value) => JSON.parse(JSON.stringify(value));
+
+const settle = () => new Promise((resolve) => setImmediate(resolve));
+
+// Runs the real local-meeting stop, rollback and start paths over stubbed
+// capture, transcription and diarization so the closure state is observable.
+function harness() {
+ const events = [];
+ const sent = [];
+ const diarizations = [];
+ const chunkGates = [];
+ const lateStreamingFinals = [];
+ const win = {
+ isDestroyed: () => false,
+ webContents: { send: (channel, payload) => sent.push({ channel, payload }) },
+ };
+ const engine = {
+ endRecordingSession: () => undefined,
+ setUserRecording() {},
+ beginRecordingSession: async () => {},
+ };
+ const context = {
+ events,
+ chunkGates,
+ lateStreamingFinals,
+ win,
+ debugLogger: { debug() {}, error() {} },
+ meetingDetectionEngine: engine,
+ windowManager: { controlPanelWindow: null },
+ activeMeetingSpeakerConfig: null,
+ _startOrSkipDiarization: (...args) => diarizations.push(args),
+ _resolveMeetingSpeakerIndexBase: () => 0,
+ _resolveInitialMeetingSpeakerConfig: () => null,
+ BrowserWindow: { fromWebContents: () => win },
+ ALLOWED_MEETING_PROVIDERS: new Set(["local"]),
+ LOCAL_MEETING_CHUNK_INTERVAL_MS: 5000,
+ credentialGeneration: 0,
+ setInterval: () => "local-timer",
+ clearInterval: () => {},
+ };
+ vm.createContext(context);
+ vm.runInContext(
+ `
+ let meetingLocalMode = false;
+ let meetingLocalTimer = null;
+ let meetingLocalWin = null;
+ let meetingLocalBuffers = { mic: [], system: [] };
+ let meetingLocalTranscript = "";
+ let meetingLocalProvider = null;
+ let meetingLocalModel = null;
+ let meetingLocalLanguage = null;
+ let meetingLocalTranscribePromise = null;
+ let meetingDiarizationSegments = [];
+ let meetingNoteId = null;
+ let meetingSpeakerIndexBase = 0;
+ let meetingStartedAt = null;
+ let meetingConnectionOptions = null;
+ let meetingConnectionWin = null;
+ let meetingConnectionKey = null;
+ let meetingReconnectCount = 0;
+ let meetingFatalErrorSent = false;
+ let meetingTranscriptionPreparePromise = null;
+ let meetingTranscriptionStartInProgress = false;
+ let meetingOneOnOneAttendee = null;
+ let meetingOneOnOneProfileBound = false;
+
+ const noop = () => {};
+ const asyncNoop = async () => {};
+ const clearMeetingSystemAudioSilenceTimer = noop;
+ const stopMeetingSystemAudioWatchdog = noop;
+ const armMeetingSystemAudioSilenceTimer = noop;
+ const startMeetingSystemAudioWatchdog = noop;
+ const flushPendingMeetingMicChunks = noop;
+ const flushPendingMicFinals = () => events.push("flush");
+ const stopMeetingAec = asyncNoop;
+ const startMeetingAec = asyncNoop;
+ const stopLiveSpeakerIdentification = async () => null;
+ const startLiveSpeakerIdentification = asyncNoop;
+ const meetingEchoLeakDetector = { reset: noop };
+ const resolveOneOnOneAttendeeForNote = () => null;
+ const isMeetingStreamingConnected = () => false;
+ const getMeetingConnectionKey = () => "local";
+ const getMeetingSystemAudioPlan = async () => ({
+ mode: "unsupported",
+ strategy: "unsupported",
+ });
+ const startMeetingSystemAudio = async (_event, systemAudioMode, systemAudioStrategy) => ({
+ systemAudioMode,
+ systemAudioStrategy,
+ });
+ const toPolicyFailure = (error) => ({ success: false, error: error.message });
+ const buildOrderedTranscriptText = (segments) =>
+ segments.map((segment) => segment.text).join(" ").trim();
+
+ // Mirrors the real chunk: a final lands in the segment list and goes to
+ // the meeting window, whichever session those belong to by then.
+ const transcribeLocalMeetingChunk = async (source) => {
+ const index = chunkGates.findIndex((gate) => gate.source === source);
+ if (index < 0) return;
+ const [gate] = chunkGates.splice(index, 1);
+ await gate.promise;
+ events.push("chunk-done:" + source);
+ meetingDiarizationSegments.push({ text: gate.text, source, timestamp: 1 });
+ if (meetingLocalWin && !meetingLocalWin.isDestroyed()) {
+ meetingLocalWin.webContents.send("meeting-transcription-segment", {
+ text: gate.text,
+ source,
+ type: "final",
+ });
+ }
+ };
+
+ const disconnectMeetingStreaming = async () => {
+ events.push("disconnect");
+ for (const text of lateStreamingFinals) {
+ meetingDiarizationSegments.push({ text, source: "system", timestamp: 2 });
+ }
+ return [{ text: "" }, { text: "" }];
+ };
+
+ const captureMeetingDiarizationState = async () => {
+ events.push("capture");
+ const diarizationSegments = meetingDiarizationSegments;
+ meetingDiarizationSegments = [];
+ return {
+ diarizationPcmPath: null,
+ diarizationSegments,
+ diarizationStartedAt: null,
+ diarizedSource: null,
+ };
+ };
+
+ const resetMeetingLocalState = () => {
+ events.push("reset");
+ meetingLocalTimer = null;
+ meetingLocalMode = false;
+ meetingLocalWin = null;
+ meetingDiarizationSegments = [];
+ meetingLocalTranscript = "";
+ };
+
+ ${sliceBetween("const transcribeAllLocalBuffers =", "const dropMeetingMicDiarizationCapture =")}
+ ${sliceBetween("const rollbackMeetingTranscriptionStart =", "const setupDictationCallbacks =")}
+ ${sliceBetween("const startMeetingTranscription =", "const sendMeetingAudio =")}
+ ${sliceBetween("const stopMeetingTranscription =", "const meetingTranscriptionLifecycle =")}
+
+ globalThis.api = {
+ beginLocalSession() {
+ meetingLocalMode = true;
+ meetingLocalTimer = "local-timer";
+ meetingLocalWin = win;
+ },
+ timerTick: () => transcribeAllLocalBuffers(),
+ stop: (sessionId) => stopMeetingTranscription(sessionId),
+ rollback: () => rollbackMeetingTranscriptionStart(),
+ start: (options) => startMeetingTranscription({ sender: {} }, options),
+ get segments() {
+ return meetingDiarizationSegments;
+ },
+ set segments(value) {
+ meetingDiarizationSegments = value;
+ },
+ };
+ `,
+ context
+ );
+
+ const holdChunk = (text, source = "system") => {
+ const gate = { ...deferred(), text, source };
+ chunkGates.push(gate);
+ return gate;
+ };
+
+ return { api: context.api, events, sent, diarizations, holdChunk, lateStreamingFinals };
+}
+
+test("stop waits for a timer pass still transcribing before it captures the session", async () => {
+ const { api, events, sent, diarizations, holdChunk } = harness();
+ api.beginLocalSession();
+ const gate = holdChunk("said just before stop");
+
+ void api.timerTick();
+ const stopping = api.stop("session-1");
+ await settle();
+
+ assert.deepEqual(events, [], "stop must not capture while the pass is in flight");
+
+ gate.resolve();
+ const result = await stopping;
+
+ assert.deepEqual(events, ["chunk-done:system", "flush", "capture", "reset"]);
+ assert.equal(result.success, true);
+ assert.equal(result.transcript, "said just before stop");
+ assert.deepEqual(plain(diarizations[0][3].map((segment) => segment.text)), [
+ "said just before stop",
+ ]);
+ assert.equal(sent.length, 1, "the final is sent before the reset clears the window");
+ assert.deepEqual(plain(api.segments), [], "nothing is left for the next session");
+});
+
+test("stop still transcribes what was buffered after the in-flight pass", async () => {
+ const { api, events, holdChunk } = harness();
+ api.beginLocalSession();
+ const inFlight = holdChunk("timer pass");
+
+ void api.timerTick();
+ const stopping = api.stop("session-1");
+ await settle();
+ // The final pass, queued behind the in-flight one.
+ const finalPass = holdChunk("final pass");
+ inFlight.resolve();
+ await settle();
+ finalPass.resolve();
+ const result = await stopping;
+
+ assert.deepEqual(events, ["chunk-done:system", "chunk-done:system", "flush", "capture", "reset"]);
+ assert.equal(result.transcript, "timer pass final pass");
+});
+
+test("rollback lets late finals land before it resets the session", async () => {
+ const { api, events, holdChunk, lateStreamingFinals } = harness();
+ api.beginLocalSession();
+ const gate = holdChunk("local final");
+ lateStreamingFinals.push("streaming final");
+
+ void api.timerTick();
+ const rollingBack = api.rollback();
+ await settle();
+ gate.resolve();
+ await rollingBack;
+
+ assert.deepEqual(events, ["chunk-done:system", "disconnect", "reset"]);
+ assert.deepEqual(plain(api.segments), []);
+});
+
+test("a new session starts with no segments from an earlier one", async () => {
+ const { api } = harness();
+ api.segments = [{ text: "stale final", source: "mic", timestamp: 1 }];
+
+ const result = await api.start({ provider: "local", sessionId: "session-2" });
+
+ assert.equal(result.success, true);
+ assert.deepEqual(plain(api.segments), []);
+});