Skip to content

Commit f9bb1d3

Browse files
authored
fix: fix restart after update (#458)
1 parent ef2fa52 commit f9bb1d3

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

main/src/main.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ export async function blockQuit(source: string, event?: Electron.Event) {
9393
event.preventDefault()
9494
}
9595

96-
mainWindow?.webContents.send('graceful-exit')
96+
// Only send graceful-exit message if mainWindow is still valid
97+
try {
98+
if (mainWindow && !mainWindow.isDestroyed()) {
99+
mainWindow.webContents.send('graceful-exit')
100+
}
101+
} catch (err) {
102+
log.error('Failed to send graceful-exit message: ', err)
103+
}
97104

98105
try {
99106
const port = getToolhivePort()
@@ -394,6 +401,36 @@ ipcMain.handle('restart-toolhive', async () => {
394401
}
395402
})
396403

404+
ipcMain.handle('install-update-and-restart', () => {
405+
log.info('Installing update and restarting application')
406+
// Set a flag to indicate we're installing an update
407+
// This will prevent the graceful shutdown process
408+
isQuitting = true
409+
tearingDown = true
410+
411+
// Stop ToolHive and servers immediately without graceful shutdown
412+
try {
413+
const port = getToolhivePort()
414+
if (port) {
415+
stopAllServers(binPath, port).catch((err) => {
416+
log.error('Failed to stop servers during update: ', err)
417+
})
418+
}
419+
} catch (err) {
420+
log.error('Failed to get port during update: ', err)
421+
}
422+
423+
// Stop ToolHive
424+
stopToolhive()
425+
426+
// Destroy tray
427+
tray?.destroy()
428+
429+
// Install update and restart
430+
autoUpdater.quitAndInstall()
431+
return { success: true }
432+
})
433+
397434
// Shutdown store IPC handlers
398435
ipcMain.handle('shutdown-store:get-last-servers', () => {
399436
return getLastShutdownServers()

preload/src/preload.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ contextBridge.exposeInMainWorld('electronAPI', {
2727
// ToolHive restart
2828
restartToolhive: () => ipcRenderer.invoke('restart-toolhive'),
2929

30+
// Update installation
31+
installUpdateAndRestart: () =>
32+
ipcRenderer.invoke('install-update-and-restart'),
33+
3034
// Theme management
3135
darkMode: {
3236
toggle: () => ipcRenderer.invoke('dark-mode:toggle'),
@@ -100,6 +104,7 @@ export interface ElectronAPI {
100104
success: boolean
101105
error?: string
102106
}>
107+
installUpdateAndRestart: () => Promise<{ success: boolean }>
103108
darkMode: {
104109
toggle: () => Promise<boolean>
105110
system: () => Promise<boolean>

renderer/src/common/components/layout/top-nav/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export function TopNav(props: HTMLProps<HTMLElement>) {
121121
onClick: async () => {
122122
const confirmed = await confirmQuit()
123123
if (confirmed) {
124-
window.electronAPI.quitApp()
124+
window.electronAPI.installUpdateAndRestart()
125125
}
126126
},
127127
},

0 commit comments

Comments
 (0)