Skip to content

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

fix(transcription): scale local transcription timeouts with audio duration (#1104)

Alcahest Malus Dienheim40b907f381e4
diff --git a/src/helpers/diarization.js b/src/helpers/diarization.js
index 6af52c5e7..1bdc2fcd6 100644
--- a/src/helpers/diarization.js
+++ b/src/helpers/diarization.js
@@ -16,6 +16,10 @@ const {
transcriptsLooselyOverlap,
buildMergedCandidates,
} = require("./transcriptText");
+const {
+ computeTranscriptionTimeoutMs,
+ PCM16_MONO_16K_BYTES_PER_SECOND,
+} = require("./transcriptionTimeout");
const DIARIZATION_TIMEOUT_MS = 3600000; // 60 minutes
const POST_MERGE_CONTEXT_WINDOW_MS = 6000;
@@ -318,6 +322,17 @@ class DiarizationManager {
wavPath,
});
+ // Scale with the recording length, but never below the 60-minute floor.
+ let timeoutMs = DIARIZATION_TIMEOUT_MS;
+ try {
+ timeoutMs = Math.max(
+ DIARIZATION_TIMEOUT_MS,
+ computeTranscriptionTimeoutMs(fs.statSync(wavPath).size / PCM16_MONO_16K_BYTES_PER_SECOND)
+ );
+ } catch {
+ // Unreadable WAV: keep the flat cap.
+ }
+
return new Promise((resolve) => {
let stdout = "";
let stderr = "";
@@ -341,10 +356,10 @@ class DiarizationManager {
};
const timeout = setTimeout(() => {
- debugLogger.warn("Diarization timed out", { timeoutMs: DIARIZATION_TIMEOUT_MS });
+ debugLogger.warn("Diarization timed out", { timeoutMs });
gracefulStopProcess(proc);
resolve([]);
- }, DIARIZATION_TIMEOUT_MS);
+ }, timeoutMs);
proc.stdout.on("data", (data) => {
stdout += data.toString();
diff --git a/src/helpers/parakeetWsServer.js b/src/helpers/parakeetWsServer.js
index 9b7de4e42..7fec46d73 100644
--- a/src/helpers/parakeetWsServer.js
+++ b/src/helpers/parakeetWsServer.js
@@ -13,14 +13,20 @@ const { getSafeTempDir } = require("./safeTempDir");
const sidecarPidFile = require("./sidecarPidFile");
const { parseOfflineMessage, createOnlineAccumulator } = require("./parakeetWsResult");
const { pcm16ToFloat32 } = require("../utils/audioUtils");
+const {
+ computeTranscriptionTimeoutMs,
+ TRANSCRIPTION_TIMEOUT_FLOOR_MS,
+} = require("./transcriptionTimeout");
const PORT_RANGE_START = 6006;
const PORT_RANGE_END = 6029;
const STARTUP_TIMEOUT_MS = 60000;
const HEALTH_CHECK_INTERVAL_MS = 5000;
-const TRANSCRIPTION_TIMEOUT_MS = 300000;
-const ONLINE_CHUNK_BYTES = 8000 * 4;
-const FLOAT32_BYTES_PER_SECOND = 16000 * 4;
+const FLOAT32_BYTES_PER_SAMPLE = 4;
+const ONLINE_CHUNK_BYTES = 8000 * FLOAT32_BYTES_PER_SAMPLE;
+const FLOAT32_BYTES_PER_SECOND = 16000 * FLOAT32_BYTES_PER_SAMPLE;
+// Streaming decodes as the audio drains, so its hard cap is tighter than the offline budget.
+const ONLINE_TIMEOUT_PER_AUDIO_SECOND_MS = 2000;
// After "Done" is sent, give up only after this long without any result message.
const ONLINE_FINISH_IDLE_TIMEOUT_MS = 10000;
// Must cover the model's 560ms chunk so the flush decodes the final words.
@@ -260,6 +266,10 @@ class ParakeetWsServer {
}
_transcribeOffline(samplesBuffer, sampleRate) {
+ const timeoutMs = computeTranscriptionTimeoutMs(
+ samplesBuffer.length / FLOAT32_BYTES_PER_SAMPLE / sampleRate
+ );
+
return new Promise((resolve, reject) => {
const startTime = Date.now();
let result = "";
@@ -269,7 +279,7 @@ class ParakeetWsServer {
ws.close();
} catch {}
reject(new Error("parakeet-ws transcription timed out"));
- }, TRANSCRIPTION_TIMEOUT_MS);
+ }, timeoutMs);
const ws = new WebSocket(`ws://127.0.0.1:${this.port}`);
@@ -353,7 +363,7 @@ class ParakeetWsServer {
timedOut = true;
stream.abort();
},
- Math.max(TRANSCRIPTION_TIMEOUT_MS, audioSeconds * 2000)
+ Math.max(TRANSCRIPTION_TIMEOUT_FLOOR_MS, audioSeconds * ONLINE_TIMEOUT_PER_AUDIO_SECOND_MS)
);
try {
const idleTimeoutMs = Math.max(ONLINE_FINISH_IDLE_TIMEOUT_MS, audioSeconds * 500);
diff --git a/src/helpers/transcriptionTimeout.js b/src/helpers/transcriptionTimeout.js
new file mode 100644
index 000000000..9524ebad8
--- /dev/null
+++ b/src/helpers/transcriptionTimeout.js
@@ -0,0 +1,32 @@
+// Floor keeps short dictations on the same 5-minute budget as before.
+const TRANSCRIPTION_TIMEOUT_FLOOR_MS = 300000;
+// 10x real-time budget covers big models on slow CPUs.
+const TRANSCRIPTION_TIMEOUT_PER_AUDIO_SECOND_MS = 10000;
+// Flat 60-minute cap when duration is unknown; a hung server must still fail.
+const UNKNOWN_DURATION_TIMEOUT_MS = 3600000;
+// 24-hour ceiling keeps the scaled value well under the 32-bit setTimeout limit.
+const TRANSCRIPTION_TIMEOUT_CEILING_MS = 86400000;
+// Every local engine is fed 16 kHz mono s16le, so byte length is a duration estimate.
+const PCM16_MONO_16K_BYTES_PER_SECOND = 16000 * 2;
+
+function computeTranscriptionTimeoutMs(estimatedDurationSeconds) {
+ if (!Number.isFinite(estimatedDurationSeconds) || estimatedDurationSeconds <= 0) {
+ return UNKNOWN_DURATION_TIMEOUT_MS;
+ }
+ return Math.min(
+ TRANSCRIPTION_TIMEOUT_CEILING_MS,
+ Math.max(
+ TRANSCRIPTION_TIMEOUT_FLOOR_MS,
+ Math.ceil(estimatedDurationSeconds * TRANSCRIPTION_TIMEOUT_PER_AUDIO_SECOND_MS)
+ )
+ );
+}
+
+module.exports = {
+ TRANSCRIPTION_TIMEOUT_FLOOR_MS,
+ TRANSCRIPTION_TIMEOUT_PER_AUDIO_SECOND_MS,
+ UNKNOWN_DURATION_TIMEOUT_MS,
+ TRANSCRIPTION_TIMEOUT_CEILING_MS,
+ PCM16_MONO_16K_BYTES_PER_SECOND,
+ computeTranscriptionTimeoutMs,
+};
diff --git a/src/helpers/whisperServer.js b/src/helpers/whisperServer.js
index caa67c354..cf2817c1b 100644
--- a/src/helpers/whisperServer.js
+++ b/src/helpers/whisperServer.js
@@ -12,6 +12,10 @@ const { getSafeTempDir } = require("./safeTempDir");
const { convertToWav } = require("./ffmpegUtils");
const sidecarPidFile = require("./sidecarPidFile");
const { sanitizeWhisperVadConfig, DEFAULT_WHISPER_VAD_CONFIG } = require("./whisperVadConfig");
+const {
+ computeTranscriptionTimeoutMs,
+ PCM16_MONO_16K_BYTES_PER_SECOND,
+} = require("./transcriptionTimeout");
const PORT_RANGE_START = 8178;
const PORT_RANGE_END = 8199;
@@ -778,6 +782,9 @@ class WhisperServerManager extends EventEmitter {
}
_postInference(body, boundary) {
+ // Multipart boilerplate adds under a kilobyte, so body length tracks audio length.
+ const timeoutMs = computeTranscriptionTimeoutMs(body.length / PCM16_MONO_16K_BYTES_PER_SECOND);
+
return new Promise((resolve, reject) => {
const startTime = Date.now();
@@ -791,7 +798,7 @@ class WhisperServerManager extends EventEmitter {
"Content-Type": `multipart/form-data; boundary=${boundary}`,
"Content-Length": body.length,
},
- timeout: 300000,
+ timeout: timeoutMs,
},
(res) => {
let data = "";
diff --git a/test/helpers/transcriptionTimeout.test.js b/test/helpers/transcriptionTimeout.test.js
new file mode 100644
index 000000000..6edba559e
--- /dev/null
+++ b/test/helpers/transcriptionTimeout.test.js
@@ -0,0 +1,59 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+
+const {
+ TRANSCRIPTION_TIMEOUT_FLOOR_MS,
+ TRANSCRIPTION_TIMEOUT_PER_AUDIO_SECOND_MS,
+ UNKNOWN_DURATION_TIMEOUT_MS,
+ TRANSCRIPTION_TIMEOUT_CEILING_MS,
+ PCM16_MONO_16K_BYTES_PER_SECOND,
+ computeTranscriptionTimeoutMs,
+} = require("../../src/helpers/transcriptionTimeout");
+
+test("long audio scales past the old 5-minute limit", () => {
+ const timeoutMs = computeTranscriptionTimeoutMs(3600);
+ assert.ok(timeoutMs > TRANSCRIPTION_TIMEOUT_FLOOR_MS);
+ assert.equal(timeoutMs, Math.ceil(3600 * TRANSCRIPTION_TIMEOUT_PER_AUDIO_SECOND_MS));
+});
+
+test("short audio keeps the exact 5-minute floor", () => {
+ assert.equal(computeTranscriptionTimeoutMs(30), TRANSCRIPTION_TIMEOUT_FLOOR_MS);
+ assert.equal(computeTranscriptionTimeoutMs(0.5), TRANSCRIPTION_TIMEOUT_FLOOR_MS);
+ assert.equal(TRANSCRIPTION_TIMEOUT_FLOOR_MS, 300000);
+});
+
+test("audio just past the floor breakpoint scales by the formula", () => {
+ assert.equal(
+ computeTranscriptionTimeoutMs(30.1),
+ Math.ceil(30.1 * TRANSCRIPTION_TIMEOUT_PER_AUDIO_SECOND_MS)
+ );
+});
+
+test("WAV byte length converts to the right duration", () => {
+ const tenMinutesOf16kMonoS16le = 600 * 32000;
+ assert.equal(
+ computeTranscriptionTimeoutMs(tenMinutesOf16kMonoS16le / PCM16_MONO_16K_BYTES_PER_SECOND),
+ 600 * TRANSCRIPTION_TIMEOUT_PER_AUDIO_SECOND_MS
+ );
+});
+
+test("absurd durations are clamped under the 32-bit setTimeout limit", () => {
+ assert.equal(computeTranscriptionTimeoutMs(1e9), TRANSCRIPTION_TIMEOUT_CEILING_MS);
+ assert.ok(TRANSCRIPTION_TIMEOUT_CEILING_MS <= 2147483647);
+});
+
+test("unknown or invalid durations fall back to the flat cap", () => {
+ assert.equal(computeTranscriptionTimeoutMs(undefined), UNKNOWN_DURATION_TIMEOUT_MS);
+ assert.equal(computeTranscriptionTimeoutMs(NaN), UNKNOWN_DURATION_TIMEOUT_MS);
+ assert.equal(computeTranscriptionTimeoutMs(0), UNKNOWN_DURATION_TIMEOUT_MS);
+ assert.equal(computeTranscriptionTimeoutMs(-5), UNKNOWN_DURATION_TIMEOUT_MS);
+ assert.equal(computeTranscriptionTimeoutMs(Infinity), UNKNOWN_DURATION_TIMEOUT_MS);
+});
+
+test("result is always a finite number", () => {
+ for (const input of [undefined, NaN, 0, -1, 0.5, 30, 3600, 1e9, Infinity]) {
+ const timeoutMs = computeTranscriptionTimeoutMs(input);
+ assert.equal(typeof timeoutMs, "number");
+ assert.ok(Number.isFinite(timeoutMs));
+ }
+});