GeezerVison is showing github.com — electron: more robust sidecar kill handling (#18742) · msis/opencode@4c27e7f
File tree
Expand file treeCollapse file tree
Open diff view settings
packages/desktop-electron/src/main
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: packages/desktop-electron/src/main/cli.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ export type CommandEvent = |
35 | 35 | export type SqliteMigrationProgress = { type: "InProgress"; value: number } | { type: "Done" } |
36 | 36 |
|
37 | 37 | export type CommandChild = { |
| 38 | + pid: number | undefined |
38 | 39 | kill: () => void |
39 | 40 | } |
40 | 41 |
|
@@ -191,7 +192,7 @@ export function spawnCommand(args: string, extraEnv: Record<string, string>) { |
191 | 192 | treeKill(child.pid) |
192 | 193 | } |
193 | 194 |
|
194 | | - return { events, child: { kill }, exit } |
| 195 | + return { events, child: { pid: child.pid, kill }, exit } |
195 | 196 | } |
196 | 197 |
|
197 | 198 | function handleSqliteProgress(events: EventEmitter, line: string) { |
|
Collapse file
Copy file name to clipboardExpand all lines: packages/desktop-electron/src/main/index.ts +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -81,6 +81,17 @@ function setupApp() { |
81 | 81 | killSidecar() |
82 | 82 | }) |
83 | 83 |
|
| 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 | + |
84 | 95 | void app.whenReady().then(async () => { |
85 | 96 | // migrate() |
86 | 97 | app.setAsDefaultProtocolClient("opencode") |
@@ -234,8 +245,15 @@ registerIpcHandlers({ |
234 | 245 |
|
235 | 246 | function killSidecar() { |
236 | 247 | if (!sidecar) return |
| 248 | + const pid = sidecar.pid |
237 | 249 | sidecar.kill() |
238 | 250 | 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 | + } |
239 | 257 | } |
240 | 258 |
|
241 | 259 | function ensureLoopbackNoProxy() { |
|