Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion main/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ app.whenReady().then(async () => {
mainWindow.webContents.once('did-finish-load', () => {
log.info('Main window did-finish-load event triggered')
})

// Windows-specific: Handle system shutdown/restart/logout
if (process.platform === 'win32') {
mainWindow.on('session-end', (event) => {
log.info(
`[session-end] Windows session ending (reasons: ${event.reasons.join(', ')}), forcing cleanup...`
)
stopToolhive()
safeTrayDestroy()
})
}
}
} catch (error) {
log.error('Failed to create main window:', error)
Expand Down Expand Up @@ -346,9 +357,19 @@ app.on('before-quit', async (e) => {
})
app.on('will-quit', (e) => blockQuit('will-quit', e))

app.on('quit', () => {
log.info('[quit event] Ensuring ToolHive cleanup...')
// Only cleanup if not already tearing down to avoid double cleanup
if (!getTearingDownState()) {
stopToolhive()
safeTrayDestroy()
}
})

// Docker / Ctrl-C etc.
;['SIGTERM', 'SIGINT'].forEach((sig) =>
process.on(sig as NodeJS.Signals, async () => {
process.on(sig, async () => {
log.info(`[${sig}] start...`)
if (getTearingDownState()) return
setTearingDownState(true)
setQuittingState(true)
Expand All @@ -366,6 +387,12 @@ app.on('will-quit', (e) => blockQuit('will-quit', e))
})
)

process.on('exit', (code) => {
log.info(`[process exit] code=${code}, ensuring ToolHive is stopped...`)
// Note: Only synchronous operations work here, no async
stopToolhive()
})

ipcMain.handle('dark-mode:toggle', () => {
nativeTheme.themeSource = nativeTheme.shouldUseDarkColors ? 'light' : 'dark'
return nativeTheme.shouldUseDarkColors
Expand Down
39 changes: 34 additions & 5 deletions main/src/toolhive-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ export async function startToolhive(): Promise<void> {
{
stdio: ['ignore', 'ignore', 'pipe'],
detached: false,
// Ensure child process is killed when parent exits
// On Windows, this creates a job object to enforce cleanup
windowsHide: true,
}
)

log.info(`[startToolhive] Process spawned with PID: ${toolhiveProcess.pid}`)

scope.addBreadcrumb({
Expand Down Expand Up @@ -215,12 +217,39 @@ export async function restartToolhive(): Promise<void> {

export function stopToolhive(): void {
if (toolhiveProcess && !toolhiveProcess.killed) {
log.info('Stopping ToolHive process...')
toolhiveProcess.kill()
log.info(`Stopping ToolHive process (PID: ${toolhiveProcess.pid})...`)

try {
const killed = toolhiveProcess.kill('SIGTERM')
log.info(`[stopToolhive] SIGTERM sent, result: ${killed}`)

setTimeout(() => {
if (toolhiveProcess && !toolhiveProcess.killed) {
log.warn(
'[stopToolhive] Process did not exit gracefully, forcing SIGKILL...'
)
try {
toolhiveProcess.kill('SIGKILL')
} catch (err) {
log.error('[stopToolhive] Failed to force kill:', err)
}
}
}, 2000)
} catch (err) {
log.error('[stopToolhive] Error killing process:', err)
try {
toolhiveProcess.kill('SIGKILL')
} catch (forceErr) {
log.error('[stopToolhive] Failed to force kill:', forceErr)
}
}

toolhiveProcess = undefined
log.info(`[stopToolhive] Process stopped and reset`)
log.info(`[stopToolhive] Process cleanup completed`)
} else {
log.info(`[stopToolhive] No process to stop`)
log.info(
`[stopToolhive] No process to stop (process=${!!toolhiveProcess}, killed=${toolhiveProcess?.killed})`
)
}
}

Expand Down
Loading