Skip to content

Commit 64512be

Browse files
authored
fix: auto-update inconsistency state (#574)
* fix: auto-update inconsistency state * enable prerelease config * remove prerelease config
1 parent c090631 commit 64512be

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

main/src/main.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ autoUpdater.on('update-downloaded', (_, __, releaseName) => {
115115

116116
if (returnValue.response === 0) {
117117
log.info('🎯 User clicked: Restart')
118+
isUpdateInProgress = true
118119

119120
log.info('🛑 Removing quit listeners to avoid interference')
120121
app.removeAllListeners('before-quit')
@@ -144,12 +145,14 @@ autoUpdater.on('update-downloaded', (_, __, releaseName) => {
144145
log.info('🚀 All cleaned up, calling autoUpdater.quitAndInstall()...')
145146
autoUpdater.quitAndInstall()
146147
} catch (error) {
148+
isUpdateInProgress = false
147149
log.error('❌ Error during graceful shutdown:', error)
148150
tray?.destroy()
149151
app.relaunch()
150152
app.quit()
151153
}
152154
} else {
155+
isUpdateInProgress = false
153156
log.info('⏰ User chose Later - showing toast notification')
154157
if (mainWindow) {
155158
mainWindow.webContents.send('update-downloaded')
@@ -169,7 +172,7 @@ autoUpdater.checkForUpdates()
169172

170173
let tray: Tray | null = null
171174
let isQuitting = false
172-
175+
let isUpdateInProgress = false
173176
let tearingDown = false
174177

175178
/** Hold the quit, run teardown, then really exit. */
@@ -333,6 +336,7 @@ function createWindow() {
333336
let mainWindow: BrowserWindow | null = null
334337

335338
app.whenReady().then(async () => {
339+
isUpdateInProgress = false
336340
// Initialize tray first
337341
try {
338342
tray = initTray({ toolHiveIsRunning: false }) // Start with false, will update after ToolHive starts
@@ -517,6 +521,7 @@ ipcMain.handle('install-update-and-restart', async () => {
517521
// This will prevent the graceful shutdown process
518522
isQuitting = true
519523
tearingDown = true
524+
isUpdateInProgress = true
520525

521526
// Stop ToolHive and servers immediately without graceful shutdown
522527
try {
@@ -541,6 +546,11 @@ ipcMain.handle('install-update-and-restart', async () => {
541546
return { success: true }
542547
})
543548

549+
ipcMain.handle('is-update-in-progress', () => {
550+
log.debug(`[is-update-in-progress]: ${isUpdateInProgress}`)
551+
return isUpdateInProgress
552+
})
553+
544554
ipcMain.handle('is-release-build', () => {
545555
return isOfficialReleaseBuild()
546556
})

main/src/toolhive-manager.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ export function getToolhivePort(): number | undefined {
3434
}
3535

3636
export function isToolhiveRunning(): boolean {
37-
return !!toolhiveProcess && !toolhiveProcess.killed
37+
const isRunning = !!toolhiveProcess && !toolhiveProcess.killed
38+
log.debug(
39+
`[isToolhiveRunning] Process exists: ${!!toolhiveProcess}, Killed: ${toolhiveProcess?.killed}, Port: ${toolhivePort}, Result: ${isRunning}`
40+
)
41+
return isRunning
3842
}
3943

4044
function findFreePort(): Promise<number> {
@@ -54,6 +58,10 @@ function findFreePort(): Promise<number> {
5458
}
5559

5660
export async function startToolhive(tray?: Tray): Promise<void> {
61+
log.info(
62+
`[startToolhive] Starting - Current state: isRunning=${isToolhiveRunning()}, isRestarting=${isRestarting}`
63+
)
64+
5765
if (!existsSync(binPath)) {
5866
log.error(`ToolHive binary not found at: ${binPath}`)
5967
return
@@ -71,6 +79,8 @@ export async function startToolhive(tray?: Tray): Promise<void> {
7179
}
7280
)
7381

82+
log.info(`[startToolhive] Process spawned with PID: ${toolhiveProcess.pid}`)
83+
7484
if (tray) {
7585
updateTrayStatus(tray, !!toolhiveProcess)
7686
}
@@ -125,10 +135,16 @@ export async function restartToolhive(tray?: Tray): Promise<void> {
125135
}
126136

127137
export function stopToolhive(): void {
138+
log.info(
139+
`[stopToolhive] Current state: isRunning=${isToolhiveRunning()}, PID: ${toolhiveProcess?.pid}`
140+
)
128141
if (toolhiveProcess && !toolhiveProcess.killed) {
129142
log.info('Stopping ToolHive process...')
130143
toolhiveProcess.kill()
131144
toolhiveProcess = undefined
145+
log.info(`[stopToolhive] Process stopped and reset`)
146+
} else {
147+
log.info(`[stopToolhive] No process to stop`)
132148
}
133149
}
134150

preload/src/preload.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
1515
showApp: () => ipcRenderer.invoke('show-app'),
1616
hideApp: () => ipcRenderer.invoke('hide-app'),
1717
quitApp: () => ipcRenderer.invoke('quit-app'),
18+
isUpdateInProgress: () => ipcRenderer.invoke('is-update-in-progress'),
1819

1920
// App version
2021
getAppVersion: () => ipcRenderer.invoke('get-app-version'),
@@ -100,6 +101,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
100101
export interface ElectronAPI {
101102
getAppVersion: () => Promise<string>
102103
isReleaseBuild: () => Promise<boolean>
104+
isUpdateInProgress: () => Promise<boolean>
103105
getAutoLaunchStatus: () => Promise<boolean>
104106
setAutoLaunch: (enabled: boolean) => Promise<boolean>
105107
showApp: () => Promise<void>

renderer/src/routes/__root.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,27 @@ export const Route = createRootRouteWithContext<{
7474
log.error(error)
7575
},
7676
beforeLoad: async ({ context: { queryClient } }) => {
77+
let isUpdateInProgress = false
78+
79+
try {
80+
isUpdateInProgress = await window.electronAPI.isUpdateInProgress()
81+
log.info(`[beforeLoad] Update in progress: ${isUpdateInProgress}`)
82+
} catch (e) {
83+
log.debug(`[beforeLoad] Update check API not available: ${e}`)
84+
}
85+
86+
if (isUpdateInProgress) {
87+
log.info(`[beforeLoad] Skipping health API check - update in progress`)
88+
return
89+
}
90+
7791
await queryClient.ensureQueryData({
7892
queryKey: ['is-toolhive-running'],
7993
queryFn: async () => {
8094
const res = await window.electronAPI.isToolhiveRunning()
8195
const appVersion = await window.electronAPI.getAppVersion()
8296
if (!res) {
83-
log.error('Error ToolHive is not running')
97+
log.error('ToolHive is not running')
8498
}
8599
log.info(`ToolHive version ${appVersion} is running`)
86100
return res

0 commit comments

Comments
 (0)