Skip to content

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

Fix app initialization timing issues with Electron 36+ (#142)

Gabriel Stein65c46a4e60bb
diff --git a/main.js b/main.js
index cd8979e05..fc6d91e0f 100644
--- a/main.js
+++ b/main.js
@@ -294,7 +294,15 @@ if (gotSingleInstanceLock) {
});
app.whenReady().then(() => {
- startApp();
+ startApp().catch((error) => {
+ console.error("CRITICAL: Failed to start app:", error);
+ // Show an error dialog before crashing
+ dialog.showErrorBox(
+ "OpenWhispr Startup Error",
+ `Failed to start the application:\n\n${error.message}\n\nPlease report this issue.`
+ );
+ app.exit(1);
+ });
});
app.on("window-all-closed", () => {
diff --git a/src/helpers/devServerManager.js b/src/helpers/devServerManager.js
index e6268a4c6..670d70b39 100644
--- a/src/helpers/devServerManager.js
+++ b/src/helpers/devServerManager.js
@@ -42,12 +42,35 @@ class DevServerManager {
if (process.env.NODE_ENV === "development") {
return isControlPanel ? "http://localhost:5174/?panel=true" : "http://localhost:5174/";
} else {
- const path = require("path");
- const htmlPath = path.join(__dirname, "..", "..", "src", "dist", "index.html");
- const url = isControlPanel ? `file://${htmlPath}?panel=true` : `file://${htmlPath}`;
- return url;
+ // For production, return null - caller should use loadFile() instead
+ return null;
}
}
+
+ /**
+ * Get the path to the index.html file for production builds.
+ * In Electron 36+, loadFile() is preferred over loadURL() with file:// protocol.
+ * @param {boolean} isControlPanel - Whether this is for the control panel
+ * @returns {{ path: string, query: object } | null} - Path info for loadFile() or null for dev
+ */
+ static getAppFilePath(isControlPanel = false) {
+ if (process.env.NODE_ENV === "development") {
+ return null; // Use getAppUrl() for dev server
+ }
+
+ const path = require("path");
+ const { app } = require("electron");
+
+ // In packaged app, files are relative to app.getAppPath()
+ // which points to the app.asar or app directory
+ const appPath = app.getAppPath();
+ const htmlPath = path.join(appPath, "src", "dist", "index.html");
+
+ return {
+ path: htmlPath,
+ query: isControlPanel ? { panel: "true" } : {},
+ };
+ }
}
module.exports = DevServerManager;
diff --git a/src/helpers/llamaCppInstaller.js b/src/helpers/llamaCppInstaller.js
index 21c59ae50..9789ab887 100644
--- a/src/helpers/llamaCppInstaller.js
+++ b/src/helpers/llamaCppInstaller.js
@@ -12,13 +12,34 @@ const { promises: fsPromises } = require("fs");
*/
class LlamaCppInstaller {
constructor() {
- this.installDir = path.join(app.getPath("userData"), "llama-cpp");
+ // IMPORTANT: Do NOT call app.getPath() here!
+ // It can hang or fail before app.whenReady() in Electron 36+.
+ this.installDir = null;
this.binPath = null;
this.platform = process.platform;
this.arch = process.arch;
+ this._initialized = false;
+ }
+
+ /**
+ * Ensures the installer is initialized. Safe to call multiple times.
+ */
+ ensureInitialized() {
+ if (this._initialized) return;
+
+ if (!app.isReady()) {
+ throw new Error(
+ "LlamaCppInstaller cannot be initialized before app.whenReady(). " +
+ "This is a programming error."
+ );
+ }
+
+ this.installDir = path.join(app.getPath("userData"), "llama-cpp");
+ this._initialized = true;
}
async ensureInstallDir() {
+ this.ensureInitialized();
await fsPromises.mkdir(this.installDir, { recursive: true });
}
@@ -28,6 +49,7 @@ class LlamaCppInstaller {
}
getInstalledBinaryPath() {
+ this.ensureInitialized();
return path.join(this.installDir, this.getBinaryName());
}
diff --git a/src/helpers/modelManagerBridge.js b/src/helpers/modelManagerBridge.js
index 3264f42e4..d5ed39fd1 100644
--- a/src/helpers/modelManagerBridge.js
+++ b/src/helpers/modelManagerBridge.js
@@ -32,22 +32,52 @@ class ModelNotFoundError extends ModelError {
class ModelManager {
constructor() {
- this.modelsDir = this.getModelsDir();
+ this.modelsDir = null;
this.downloadProgress = new Map();
this.activeDownloads = new Map();
this.activeRequests = new Map(); // Track HTTP requests for cancellation
this.serverManager = new LlamaServerManager();
this.currentServerModelId = null;
+ this._initialized = false;
+
+ // IMPORTANT: Do NOT call app.getPath() here!
+ // It can hang or fail before app.whenReady() in Electron 36+.
+ // Initialization will happen on first use via ensureInitialized().
+ }
+
+ /**
+ * Ensures the manager is initialized. Safe to call multiple times.
+ * This must be called before any operation that requires modelsDir.
+ */
+ ensureInitialized() {
+ if (this._initialized) return;
+
+ // Check if app is ready before accessing app.getPath()
+ if (!app.isReady()) {
+ throw new Error(
+ "ModelManager cannot be initialized before app.whenReady(). " +
+ "This is a programming error - ensure ModelManager methods are only called after app is ready."
+ );
+ }
+
+ this.modelsDir = this.getModelsDir();
+ this._initialized = true;
+ // Don't await - let this run in background
this.ensureModelsDirExists();
}
getModelsDir() {
- const homeDir = app.getPath("home");
+ const os = require("os");
+ // Use os.homedir() as fallback if app.getPath fails
+ const homeDir = app.isReady() ? app.getPath("home") : os.homedir();
return path.join(homeDir, ".cache", "openwhispr", "models");
}
async ensureModelsDirExists() {
try {
+ if (!this.modelsDir) {
+ this.ensureInitialized();
+ }
await fsPromises.mkdir(this.modelsDir, { recursive: true });
} catch (error) {
console.error("Failed to create models directory:", error);
@@ -65,6 +95,7 @@ class ModelManager {
}
async getAllModels() {
+ this.ensureInitialized();
try {
const models = [];
@@ -95,6 +126,7 @@ class ModelManager {
}
async isModelDownloaded(modelId) {
+ this.ensureInitialized();
const modelInfo = this.findModelById(modelId);
if (!modelInfo) return false;
@@ -131,6 +163,7 @@ class ModelManager {
}
async downloadModel(modelId, onProgress) {
+ this.ensureInitialized();
const modelInfo = this.findModelById(modelId);
if (!modelInfo) {
throw new ModelNotFoundError(modelId);
@@ -378,6 +411,7 @@ class ModelManager {
}
async deleteModel(modelId) {
+ this.ensureInitialized();
const modelInfo = this.findModelById(modelId);
if (!modelInfo) {
throw new ModelNotFoundError(modelId);
@@ -391,6 +425,7 @@ class ModelManager {
}
async deleteAllModels() {
+ this.ensureInitialized();
try {
if (fsPromises.rm) {
await fsPromises.rm(this.modelsDir, { recursive: true, force: true });
@@ -419,6 +454,7 @@ class ModelManager {
}
async runInference(modelId, prompt, options = {}) {
+ this.ensureInitialized();
const startTime = Date.now();
debugLogger.logReasoning("INFERENCE_START", {
modelId,
@@ -526,6 +562,7 @@ class ModelManager {
async prewarmServer(modelId) {
if (!modelId) return false;
+ this.ensureInitialized();
const modelInfo = this.findModelById(modelId);
if (!modelInfo) return false;
diff --git a/src/helpers/windowConfig.js b/src/helpers/windowConfig.js
index 1f2e4fcab..392651cec 100644
--- a/src/helpers/windowConfig.js
+++ b/src/helpers/windowConfig.js
@@ -9,7 +9,6 @@ const MAIN_WINDOW_CONFIG = {
preload: path.join(__dirname, "..", "..", "preload.js"),
nodeIntegration: false,
contextIsolation: true,
- enableRemoteModule: false,
sandbox: true,
},
frame: false,
@@ -34,7 +33,6 @@ const CONTROL_PANEL_CONFIG = {
preload: path.join(__dirname, "..", "..", "preload.js"),
nodeIntegration: false,
contextIsolation: true,
- enableRemoteModule: false,
sandbox: false,
webSecurity: false,
spellcheck: false,
diff --git a/src/helpers/windowManager.js b/src/helpers/windowManager.js
index 5431448d5..51da8d010 100644
--- a/src/helpers/windowManager.js
+++ b/src/helpers/windowManager.js
@@ -93,14 +93,20 @@ class WindowManager {
}
async loadMainWindow() {
- const appUrl = DevServerManager.getAppUrl(false);
if (process.env.NODE_ENV === "development") {
+ const appUrl = DevServerManager.getAppUrl(false);
const isReady = await DevServerManager.waitForDevServer();
if (!isReady) {
// Dev server not ready, continue anyway
}
+ this.mainWindow.loadURL(appUrl);
+ } else {
+ // Production: use loadFile() for better compatibility with Electron 36+
+ const fileInfo = DevServerManager.getAppFilePath(false);
+ if (fileInfo) {
+ this.mainWindow.loadFile(fileInfo.path, { query: fileInfo.query });
+ }
}
- this.mainWindow.loadURL(appUrl);
}
createHotkeyCallback() {
@@ -267,14 +273,20 @@ class WindowManager {
}
async loadControlPanel() {
- const appUrl = DevServerManager.getAppUrl(true);
if (process.env.NODE_ENV === "development") {
+ const appUrl = DevServerManager.getAppUrl(true);
const isReady = await DevServerManager.waitForDevServer();
if (!isReady) {
console.error("Dev server not ready for control panel, loading anyway...");
}
+ this.controlPanelWindow.loadURL(appUrl);
+ } else {
+ // Production: use loadFile() for better compatibility with Electron 36+
+ const fileInfo = DevServerManager.getAppFilePath(true);
+ if (fileInfo) {
+ this.controlPanelWindow.loadFile(fileInfo.path, { query: fileInfo.query });
+ }
}
- this.controlPanelWindow.loadURL(appUrl);
}
showDictationPanel(options = {}) {