Skip to content

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

fix: resolve whisper-server crash on Windows with non-ASCII usernames by pre-converting audio to WAV and routing temp files through ASCII-safe directory

Gabriel Stein8fd8a83e7779
diff --git a/src/helpers/llamaServer.js b/src/helpers/llamaServer.js
index d5ddaf169..3728282e9 100644
--- a/src/helpers/llamaServer.js
+++ b/src/helpers/llamaServer.js
@@ -1,11 +1,11 @@
const { spawn } = require("child_process");
const fs = require("fs");
const net = require("net");
-const os = require("os");
const path = require("path");
const http = require("http");
const debugLogger = require("./debugLogger");
const { killProcess } = require("../utils/process");
+const { getSafeTempDir } = require("./safeTempDir");
const PORT_RANGE_START = 8200;
const PORT_RANGE_END = 8220;
@@ -153,7 +153,7 @@ class LlamaServerManager {
this.process = spawn(serverBinary, args, {
stdio: ["ignore", "pipe", "pipe"],
windowsHide: true,
- cwd: os.tmpdir(),
+ cwd: getSafeTempDir(),
env,
});
diff --git a/src/helpers/parakeetServer.js b/src/helpers/parakeetServer.js
index 28a71c10f..f02a53e58 100644
--- a/src/helpers/parakeetServer.js
+++ b/src/helpers/parakeetServer.js
@@ -1,9 +1,9 @@
const fs = require("fs");
-const os = require("os");
const path = require("path");
const debugLogger = require("./debugLogger");
const { getModelsDirForService } = require("./modelDirUtils");
const { getFFmpegPath, isWavFormat, convertToWav, wavToFloat32Samples } = require("./ffmpegUtils");
+const { getSafeTempDir } = require("./safeTempDir");
const ParakeetWsServer = require("./parakeetWsServer");
class ParakeetServerManager {
@@ -54,7 +54,7 @@ class ParakeetServerManager {
);
}
- const tempDir = os.tmpdir();
+ const tempDir = getSafeTempDir();
const timestamp = Date.now();
const tempInputPath = path.join(tempDir, `parakeet-input-${timestamp}.webm`);
const tempWavPath = path.join(tempDir, `parakeet-${timestamp}.wav`);
diff --git a/src/helpers/parakeetWsServer.js b/src/helpers/parakeetWsServer.js
index fd64948f0..1ddeafb4e 100644
--- a/src/helpers/parakeetWsServer.js
+++ b/src/helpers/parakeetWsServer.js
@@ -1,14 +1,15 @@
const { spawn } = require("child_process");
const fs = require("fs");
-const os = require("os");
const path = require("path");
const WebSocket = require("ws");
const debugLogger = require("./debugLogger");
+const os = require("os");
const {
findAvailablePort,
resolveBinaryPath,
gracefulStopProcess,
} = require("../utils/serverUtils");
+const { getSafeTempDir } = require("./safeTempDir");
const PORT_RANGE_START = 6006;
const PORT_RANGE_END = 6029;
@@ -83,7 +84,7 @@ class ParakeetWsServer {
this.process = spawn(wsBinary, args, {
stdio: ["ignore", "pipe", "pipe"],
windowsHide: true,
- cwd: os.tmpdir(),
+ cwd: getSafeTempDir(),
});
let stderrBuffer = "";
diff --git a/src/helpers/safeTempDir.js b/src/helpers/safeTempDir.js
new file mode 100644
index 000000000..72b20d6ab
--- /dev/null
+++ b/src/helpers/safeTempDir.js
@@ -0,0 +1,39 @@
+const os = require("os");
+const fs = require("fs");
+const path = require("path");
+
+let cachedSafeTempDir = null;
+
+// Returns an ASCII-safe temp directory for native binaries on Windows.
+// Falls back to ProgramData when TEMP contains non-ASCII characters.
+function getSafeTempDir() {
+ if (cachedSafeTempDir) return cachedSafeTempDir;
+
+ const systemTemp = os.tmpdir();
+
+ if (process.platform !== "win32" || /^[\x00-\x7F]*$/.test(systemTemp)) {
+ cachedSafeTempDir = systemTemp;
+ return systemTemp;
+ }
+
+ const fallbackBase = process.env.ProgramData || "C:\\ProgramData";
+ const fallback = path.join(fallbackBase, "OpenWhispr", "temp");
+
+ try {
+ fs.mkdirSync(fallback, { recursive: true });
+ cachedSafeTempDir = fallback;
+ return fallback;
+ } catch {
+ const rootFallback = path.join(process.env.SystemDrive || "C:", "OpenWhispr", "temp");
+ try {
+ fs.mkdirSync(rootFallback, { recursive: true });
+ cachedSafeTempDir = rootFallback;
+ return rootFallback;
+ } catch {
+ cachedSafeTempDir = systemTemp;
+ return systemTemp;
+ }
+ }
+}
+
+module.exports = { getSafeTempDir };
diff --git a/src/helpers/whisperServer.js b/src/helpers/whisperServer.js
index 6640d9b4d..b0c4da179 100644
--- a/src/helpers/whisperServer.js
+++ b/src/helpers/whisperServer.js
@@ -1,11 +1,12 @@
const { spawn } = require("child_process");
const fs = require("fs");
const net = require("net");
-const os = require("os");
const path = require("path");
const http = require("http");
const debugLogger = require("./debugLogger");
const { killProcess } = require("../utils/process");
+const { getSafeTempDir } = require("./safeTempDir");
+const { isWavFormat, convertToWav } = require("./ffmpegUtils");
const PORT_RANGE_START = 8178;
const PORT_RANGE_END = 8199;
@@ -210,6 +211,12 @@ class WhisperServerManager {
const spawnEnv = { ...process.env };
const pathSep = process.platform === "win32" ? ";" : ":";
+ if (process.platform === "win32") {
+ const safeTmp = getSafeTempDir();
+ spawnEnv.TEMP = safeTmp;
+ spawnEnv.TMP = safeTmp;
+ }
+
// Add the whisper-server directory to PATH so any companion DLLs are found
const serverBinaryDir = path.dirname(serverBinary);
spawnEnv.PATH = serverBinaryDir + pathSep + (process.env.PATH || "");
@@ -377,32 +384,28 @@ class WhisperServerManager {
});
const { language, initialPrompt } = options;
- const boundary = `----WhisperBoundary${Date.now()}`;
- const parts = [];
- const isWav =
- audioBuffer &&
- audioBuffer.length >= 12 &&
- audioBuffer[0] === 0x52 &&
- audioBuffer[1] === 0x49 &&
- audioBuffer[2] === 0x46 &&
- audioBuffer[3] === 0x46 &&
- audioBuffer[8] === 0x57 &&
- audioBuffer[9] === 0x41 &&
- audioBuffer[10] === 0x56 &&
- audioBuffer[11] === 0x45;
- if (!isWav && !this.canConvert) {
- throw new Error("FFmpeg not found - whisper-server requires WAV input");
+ // Pre-convert to WAV so whisper-server doesn't create temp files internally
+ let finalBuffer = audioBuffer;
+ const isWav = isWavFormat(audioBuffer);
+ if (!isWav) {
+ if (!this.canConvert) {
+ throw new Error("FFmpeg not found - whisper-server requires WAV input");
+ }
+ finalBuffer = await this._convertToWav(audioBuffer);
}
- const fileName = isWav ? "audio.wav" : "audio.webm";
- const contentType = isWav ? "audio/wav" : "audio/webm";
+
+ const boundary = `----WhisperBoundary${Date.now()}`;
+ const parts = [];
+ const fileName = "audio.wav";
+ const contentType = "audio/wav";
parts.push(
`--${boundary}\r\n` +
`Content-Disposition: form-data; name="file"; filename="${fileName}"\r\n` +
`Content-Type: ${contentType}\r\n\r\n`
);
- parts.push(audioBuffer);
+ parts.push(finalBuffer);
parts.push("\r\n");
if (language && language !== "auto") {
@@ -488,6 +491,27 @@ class WhisperServerManager {
});
}
+ async _convertToWav(audioBuffer) {
+ const tempDir = getSafeTempDir();
+ const timestamp = Date.now();
+ const tempInputPath = path.join(tempDir, `whisper-input-${timestamp}.webm`);
+ const tempWavPath = path.join(tempDir, `whisper-output-${timestamp}.wav`);
+
+ try {
+ fs.writeFileSync(tempInputPath, audioBuffer);
+ await convertToWav(tempInputPath, tempWavPath, { sampleRate: 16000, channels: 1 });
+ return fs.readFileSync(tempWavPath);
+ } finally {
+ for (const f of [tempInputPath, tempWavPath]) {
+ try {
+ if (fs.existsSync(f)) fs.unlinkSync(f);
+ } catch {
+ // ignore cleanup errors
+ }
+ }
+ }
+ }
+
async stop() {
this.stopHealthCheck();