fix: prevent orphaned gateway processes on Windows causing port conflcts and UI freeze (#570)

This commit is contained in:
paisley
2026-03-18 14:34:03 +08:00
committed by GitHub
Unverified
parent 1eda50ef44
commit 2dc1070213
2 changed files with 52 additions and 16 deletions

View File

@@ -459,15 +459,30 @@ if (gotTheLock) {
}
});
app.on('before-quit', () => {
let gatewayCleanedUp = false;
app.on('before-quit', (event) => {
setQuitting();
hostEventBus.closeAll();
hostApiServer?.close();
// Fire-and-forget: do not await gatewayManager.stop() here.
// Awaiting inside before-quit can stall Electron's quit sequence.
void gatewayManager.stop().catch((err) => {
logger.warn('gatewayManager.stop() error during quit:', err);
});
// On first before-quit, block the quit so we can await gateway cleanup.
// On Windows, fire-and-forget leaves orphaned Python/uv processes that
// hold port 18789, causing port conflicts on next launch.
if (!gatewayCleanedUp) {
gatewayCleanedUp = true;
event.preventDefault();
hostEventBus.closeAll();
hostApiServer?.close();
const stopPromise = gatewayManager.stop().catch((err) => {
logger.warn('gatewayManager.stop() error during quit:', err);
});
const timeoutPromise = new Promise<void>((resolve) => setTimeout(resolve, 5000));
void Promise.race([stopPromise, timeoutPromise]).then(() => {
app.exit(0);
});
}
});
}