Skip to content

Commit 4c27e7f

Browse files
authored
electron: more robust sidecar kill handling (#18742)
1 parent 0f5626d commit 4c27e7f

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

packages/desktop-electron/src/main/cli.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type CommandEvent =
3535
export type SqliteMigrationProgress = { type: "InProgress"; value: number } | { type: "Done" }
3636

3737
export type CommandChild = {
38+
pid: number | undefined
3839
kill: () => void
3940
}
4041

@@ -191,7 +192,7 @@ export function spawnCommand(args: string, extraEnv: Record<string, string>) {
191192
treeKill(child.pid)
192193
}
193194

194-
return { events, child: { kill }, exit }
195+
return { events, child: { pid: child.pid, kill }, exit }
195196
}
196197

197198
function handleSqliteProgress(events: EventEmitter, line: string) {

packages/desktop-electron/src/main/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ function setupApp() {
8181
killSidecar()
8282
})
8383

84+
app.on("will-quit", () => {
85+
killSidecar()
86+
})
87+
88+
for (const signal of ["SIGINT", "SIGTERM"] as const) {
89+
process.on(signal, () => {
90+
killSidecar()
91+
app.exit(0)
92+
})
93+
}
94+
8495
void app.whenReady().then(async () => {
8596
// migrate()
8697
app.setAsDefaultProtocolClient("opencode")
@@ -234,8 +245,15 @@ registerIpcHandlers({
234245

235246
function killSidecar() {
236247
if (!sidecar) return
248+
const pid = sidecar.pid
237249
sidecar.kill()
238250
sidecar = null
251+
// tree-kill is async; also send process group signal as immediate fallback
252+
if (pid && process.platform !== "win32") {
253+
try {
254+
process.kill(-pid, "SIGTERM")
255+
} catch {}
256+
}
239257
}
240258

241259
function ensureLoopbackNoProxy() {

0 commit comments

Comments
 (0)