diff --git a/apps/x/apps/main/src/browser/view.ts b/apps/x/apps/main/src/browser/view.ts index b540809d..90b7d849 100644 --- a/apps/x/apps/main/src/browser/view.ts +++ b/apps/x/apps/main/src/browser/view.ts @@ -113,7 +113,9 @@ export class BrowserViewManager extends EventEmitter { attach(window: BrowserWindow): void { this.cleanupWindowListeners?.(); + this.cleanupWindowListeners = null; this.window = window; + const hostWebContents = window.webContents; const resetForHostWindowNavigation = () => { // Renderer refreshes do not run React unmount cleanup reliably, so the @@ -132,10 +134,16 @@ export class BrowserViewManager extends EventEmitter { }; const handleClosed = () => { - this.cleanupWindowListeners?.(); + if (this.window !== window) return; + + const tabs = [...this.tabs.values()]; this.cleanupWindowListeners = null; this.window = null; this.browserSession = null; + this.bounds = { x: 0, y: 0, width: 0, height: 0 }; + for (const tab of tabs) { + this.destroyTab(tab); + } this.tabs.clear(); this.tabOrder = []; this.activeTabId = null; @@ -144,14 +152,18 @@ export class BrowserViewManager extends EventEmitter { this.snapshotCache.clear(); }; - window.webContents.on('did-start-loading', handleDidStartLoading); - window.webContents.on('render-process-gone', handleRenderProcessGone); + hostWebContents.on('did-start-loading', handleDidStartLoading); + hostWebContents.on('render-process-gone', handleRenderProcessGone); window.on('closed', handleClosed); this.cleanupWindowListeners = () => { - window.webContents.removeListener('did-start-loading', handleDidStartLoading); - window.webContents.removeListener('render-process-gone', handleRenderProcessGone); - window.removeListener('closed', handleClosed); + if (!hostWebContents.isDestroyed()) { + hostWebContents.removeListener('did-start-loading', handleDidStartLoading); + hostWebContents.removeListener('render-process-gone', handleRenderProcessGone); + } + if (!window.isDestroyed()) { + window.removeListener('closed', handleClosed); + } }; } diff --git a/apps/x/apps/main/src/main.ts b/apps/x/apps/main/src/main.ts index b7d0a491..a6d7b4e0 100644 --- a/apps/x/apps/main/src/main.ts +++ b/apps/x/apps/main/src/main.ts @@ -58,6 +58,7 @@ if (started) app.quit(); // Single-instance lock: route a second launch (e.g. clicking a rowboat:// link) // back into the existing process via the 'second-instance' event. if (!app.requestSingleInstanceLock()) { + console.error('[Main] Another Rowboat instance is already running; exiting this process.'); app.quit(); process.exit(0); }