electron: more robust sidecar kill handling (#18742)

This commit is contained in:
Brendan Allan 2026-03-23 16:44:23 +08:00 committed by GitHub
parent 0f5626d2e4
commit 4c27e7fc64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 1 deletions

View File

@ -35,6 +35,7 @@ export type CommandEvent =
export type SqliteMigrationProgress = { type: "InProgress"; value: number } | { type: "Done" } export type SqliteMigrationProgress = { type: "InProgress"; value: number } | { type: "Done" }
export type CommandChild = { export type CommandChild = {
pid: number | undefined
kill: () => void kill: () => void
} }
@ -191,7 +192,7 @@ export function spawnCommand(args: string, extraEnv: Record<string, string>) {
treeKill(child.pid) treeKill(child.pid)
} }
return { events, child: { kill }, exit } return { events, child: { pid: child.pid, kill }, exit }
} }
function handleSqliteProgress(events: EventEmitter, line: string) { function handleSqliteProgress(events: EventEmitter, line: string) {

View File

@ -81,6 +81,17 @@ function setupApp() {
killSidecar() killSidecar()
}) })
app.on("will-quit", () => {
killSidecar()
})
for (const signal of ["SIGINT", "SIGTERM"] as const) {
process.on(signal, () => {
killSidecar()
app.exit(0)
})
}
void app.whenReady().then(async () => { void app.whenReady().then(async () => {
// migrate() // migrate()
app.setAsDefaultProtocolClient("opencode") app.setAsDefaultProtocolClient("opencode")
@ -234,8 +245,15 @@ registerIpcHandlers({
function killSidecar() { function killSidecar() {
if (!sidecar) return if (!sidecar) return
const pid = sidecar.pid
sidecar.kill() sidecar.kill()
sidecar = null sidecar = null
// tree-kill is async; also send process group signal as immediate fallback
if (pid && process.platform !== "win32") {
try {
process.kill(-pid, "SIGTERM")
} catch {}
}
} }
function ensureLoopbackNoProxy() { function ensureLoopbackNoProxy() {