GeezerVison is showing github.com — fix(util): kill spawned process groups when opencode exits (#59411) · swxtchio/swx-opencode-archive@e2bc79d
This repository was archived by the owner on Nov 7, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/cli/src/shutdown-deadline.ts +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,14 +36,20 @@ export const install = Effect.fnUntraced(function* (input: Duration.Input = DEAD | 36 | 36 | started = true | 37 | 37 | fork(expire(signal)) | 38 | 38 | } | | 39 | + // Closing the terminal sends SIGHUP, whose default action exits without teardown. Treat it as SIGTERM, but only | | 40 | + // with a terminal: nohup and the background service have none, and a listener would override nohup's ignore. | | 41 | + const onHangup = () => process.kill(process.pid, "SIGTERM") | | 42 | + const hangup = process.stdin.isTTY || process.stdout.isTTY | 39 | 43 | const off = () => { | 40 | 44 | process.off("SIGTERM", onSignal) | 41 | 45 | process.off("SIGINT", onSignal) | | 46 | + process.off("SIGHUP", onHangup) | 42 | 47 | } | 43 | 48 | yield* Effect.acquireRelease( | 44 | 49 | Effect.sync(() => { | 45 | 50 | process.on("SIGTERM", onSignal) | 46 | 51 | process.on("SIGINT", onSignal) | | 52 | + if (hangup) process.on("SIGHUP", onHangup) | 47 | 53 | }), | 48 | 54 | // Once shutdown has started, keep listening: this release can run while a later finalizer is still stuck. | 49 | 55 | () => | |
Collapse file Copy file name to clipboardExpand all lines: packages/util/src/cross-spawn-spawner.ts +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,6 +23,19 @@ import { Teardown } from "./teardown.js" | 23 | 23 | /** Closing a child's scope escalates to SIGKILL after this, unless the command sets its own `forceKillAfter`. */ | 24 | 24 | export const RELEASE_FORCE_KILL_AFTER = "3 seconds" | 25 | 25 |
| | 26 | +// Process groups of running children. Exiting without stopping them (a shutdown deadline, a second signal, a | | 27 | +// crash) would orphan them, so a synchronous exit hook kills whatever is left. | | 28 | +const groups = new Set<number>() | | 29 | +let hooked = false | | 30 | +const killGroups = () => | | 31 | + groups.forEach((pid) => { | | 32 | + try { | | 33 | + globalThis.process.kill(-pid, "SIGKILL") | | 34 | + } catch { | | 35 | + // Already gone. | | 36 | + } | | 37 | + }) | | 38 | + | 26 | 39 | const toError = (err: unknown): Error => (err instanceof globalThis.Error ? err : new globalThis.Error(String(err))) | 27 | 40 |
| 28 | 41 | const toTag = (err: NodeJS.ErrnoException): PlatformError.SystemErrorTag => { | @@ -297,6 +310,7 @@ const makeCrossSpawnSpawner = Effect.gen(function* () { | 297 | 310 | resume(Effect.fail(toPlatformError("spawn", err, command))) | 298 | 311 | }) | 299 | 312 | proc.on("exit", (...args) => { | | 313 | + groups.delete(proc.pid!) | 300 | 314 | exit = args | 301 | 315 | Deferred.doneUnsafe(exited, Exit.succeed(args)) | 302 | 316 | }) | @@ -308,6 +322,11 @@ const makeCrossSpawnSpawner = Effect.gen(function* () { | 308 | 322 | Deferred.doneUnsafe(closed, Exit.succeed(exit ?? args)) | 309 | 323 | }) | 310 | 324 | proc.on("spawn", () => { | | 325 | + if (opts.detached && globalThis.process.platform !== "win32") { | | 326 | + if (!hooked) globalThis.process.once("exit", killGroups) | | 327 | + hooked = true | | 328 | + groups.add(proc.pid!) | | 329 | + } | 311 | 330 | settle = Teardown.track( | 312 | 331 | `child ${proc.pid} running: ${[command.command, ...command.args].join(" ").slice(0, 200)}`, | 313 | 332 | ) | |
|