fix: simplify Electron single instance enforcement
- Remove complex file-based locking that caused crashes - Use only Electron's built-in requestSingleInstanceLock() API - Second instances exit immediately with clear messaging - Existing instance focuses and shows user-friendly dialog - Prevents database conflicts and resource contention - Update documentation with simplified approach Fixes crashes when multiple instances run simultaneously.
This commit is contained in:
@@ -10,24 +10,11 @@ import { join } from 'path';
|
||||
|
||||
import { ElectronCapacitorApp, setupContentSecurityPolicy, setupReloadWatcher } from './setup';
|
||||
|
||||
// Single instance enforcement
|
||||
// Use Electron's built-in single instance lock
|
||||
const gotTheLock = app.requestSingleInstanceLock();
|
||||
|
||||
if (!gotTheLock) {
|
||||
console.log('[Electron] Another instance is already running. Exiting...');
|
||||
|
||||
// Show user-friendly dialog before quitting
|
||||
dialog.showMessageBox({
|
||||
type: 'info',
|
||||
title: 'TimeSafari Already Running',
|
||||
message: 'TimeSafari is already running in another window.',
|
||||
detail: 'Please use the existing TimeSafari window instead of opening a new one.',
|
||||
buttons: ['OK']
|
||||
}).then(() => {
|
||||
app.quit();
|
||||
});
|
||||
|
||||
// Exit immediately
|
||||
console.log('[Electron] Another instance is already running. Exiting immediately...');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
@@ -100,7 +87,7 @@ autoUpdater.on('error', (error) => {
|
||||
// Initialize our app, build windows, and load content.
|
||||
await myCapacitorApp.init();
|
||||
|
||||
// Handle second instance launch (focus existing window)
|
||||
// Handle second instance launch (focus existing window and show dialog)
|
||||
app.on('second-instance', (event, commandLine, workingDirectory) => {
|
||||
console.log('[Electron] Second instance attempted to launch');
|
||||
|
||||
@@ -111,6 +98,17 @@ autoUpdater.on('error', (error) => {
|
||||
mainWindow.restore();
|
||||
}
|
||||
mainWindow.focus();
|
||||
|
||||
// Show user-friendly dialog to inform about single instance
|
||||
dialog.showMessageBox(mainWindow, {
|
||||
type: 'info',
|
||||
title: 'TimeSafari Already Running',
|
||||
message: 'TimeSafari is already running in another window.',
|
||||
detail: 'Please use this existing TimeSafari window instead of opening a new one.',
|
||||
buttons: ['OK']
|
||||
}).catch((error) => {
|
||||
console.error('[Electron] Error showing dialog:', error);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user