From b9197b7e4c61d049ba73d5c0397bd5768885e33d Mon Sep 17 00:00:00 2001 From: Ben Davis Date: Fri, 26 Jun 2026 11:23:47 -0700 Subject: [PATCH] fix(server): bound explicit child process kills --- .../cli/boundedChildProcessSpawner.test.ts | 36 +++++++++++ .../src/cli/boundedChildProcessSpawner.ts | 61 +++++++++++++------ 2 files changed, 80 insertions(+), 17 deletions(-) diff --git a/apps/server/src/cli/boundedChildProcessSpawner.test.ts b/apps/server/src/cli/boundedChildProcessSpawner.test.ts index b382dc14a4..051f9b14a2 100644 --- a/apps/server/src/cli/boundedChildProcessSpawner.test.ts +++ b/apps/server/src/cli/boundedChildProcessSpawner.test.ts @@ -152,6 +152,42 @@ it.effect("preserves unref as an opt-out from scope-owned termination", () => }), ); +it.effect("bounds explicit handle kill calls and escalates to SIGKILL", () => + Effect.gen(function* () { + const exited = yield* Deferred.make(); + const signals: Array = []; + let running = true; + const handle = makeHandle({ + pid: 44, + exitCode: Deferred.await(exited), + isRunning: Effect.sync(() => running), + kill: ({ killSignal } = {}) => + Effect.sync(() => { + if (killSignal !== "SIGTERM" && killSignal !== "SIGKILL") return; + signals.push(killSignal); + if (killSignal === "SIGKILL") running = false; + }).pipe(Effect.andThen(Deferred.await(exited)), Effect.asVoid), + }); + const spawner = make( + ChildProcessSpawner.make(() => Effect.succeed(handle)), + { + termGraceMs: 0, + killGraceMs: 0, + }, + ); + const callerScope = yield* Scope.make(); + const spawned = yield* spawner + .spawn(ChildProcess.make("unused")) + .pipe(Effect.provideService(Scope.Scope, callerScope)); + + yield* spawned.kill({ forceKillAfter: 0 }); + + expect(signals).toEqual(["SIGTERM", "SIGKILL"]); + yield* Deferred.succeed(exited, ChildProcessSpawner.ExitCode(137)); + yield* Scope.close(callerScope, Exit.void); + }), +); + it.effect("does not wait for the delegate's unbounded finalizer", () => Effect.gen(function* () { const exited = yield* Deferred.make(); diff --git a/apps/server/src/cli/boundedChildProcessSpawner.ts b/apps/server/src/cli/boundedChildProcessSpawner.ts index 8fa934ed46..ad61531f6b 100644 --- a/apps/server/src/cli/boundedChildProcessSpawner.ts +++ b/apps/server/src/cli/boundedChildProcessSpawner.ts @@ -1,3 +1,4 @@ +import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Layer from "effect/Layer"; @@ -60,32 +61,58 @@ export const make = ( ); const sendSignal = Effect.fn("BoundedChildProcessSpawner.sendSignal")(function* ( handle: ChildProcessSpawner.ChildProcessHandle, - signal: ChildProcessShutdownSignal, + signal: ChildProcess.Signal, ) { yield* handle .kill({ killSignal: signal }) .pipe(Effect.ignore, Effect.forkDetach({ startImmediately: true })); }); + const kill = Effect.fn("BoundedChildProcessSpawner.kill")(function* ( + handle: ChildProcessSpawner.ChildProcessHandle, + killOptions?: ChildProcess.KillOptions, + ) { + if (!(yield* isStillRunning(handle))) return; + + const initialSignal = killOptions?.killSignal ?? "SIGTERM"; + const initialGraceMs = + initialSignal === "SIGKILL" + ? killGraceMs + : Math.min( + termGraceMs, + normalizedDelay( + killOptions?.forceKillAfter === undefined + ? undefined + : Duration.toMillis(killOptions.forceKillAfter), + termGraceMs, + ), + ); + yield* sendSignal(handle, initialSignal); + const stoppedAfterInitialSignal = yield* waitUntilStopped( + handle, + initialGraceMs, + pollIntervalMs, + ); + if (stoppedAfterInitialSignal) return; + + if (initialSignal !== "SIGKILL") { + yield* sendSignal(handle, "SIGKILL"); + if (yield* waitUntilStopped(handle, killGraceMs, pollIntervalMs)) return; + } + + yield* Effect.logWarning("Child process did not stop after SIGKILL grace period", { + pid: Number(handle.pid), + killGraceMs, + }); + }); + const shutdown = Effect.fn("BoundedChildProcessSpawner.shutdown")(function* ( handle: ChildProcessSpawner.ChildProcessHandle, childScope: Scope.Closeable, isReferenced: () => boolean, ) { - if (isReferenced() && (yield* isStillRunning(handle))) { - yield* sendSignal(handle, "SIGTERM"); - const stoppedAfterTerm = yield* waitUntilStopped(handle, termGraceMs, pollIntervalMs); - - if (!stoppedAfterTerm) { - yield* sendSignal(handle, "SIGKILL"); - const stoppedAfterKill = yield* waitUntilStopped(handle, killGraceMs, pollIntervalMs); - if (!stoppedAfterKill) { - yield* Effect.logWarning("Child process did not stop after SIGKILL grace period", { - pid: Number(handle.pid), - killGraceMs, - }); - } - } + if (isReferenced()) { + yield* kill(handle); } // The platform spawner's own finalizer can wait forever for an exit event. @@ -113,7 +140,7 @@ export const make = ( pid: delegateHandle.pid, exitCode: delegateHandle.exitCode, isRunning: delegateHandle.isRunning, - kill: delegateHandle.kill, + kill: (killOptions) => kill(delegateHandle, killOptions), stdin: delegateHandle.stdin, stdout: delegateHandle.stdout, stderr: delegateHandle.stderr, @@ -137,7 +164,7 @@ export const make = ( ), ), }); - yield* Effect.addFinalizer(() => shutdown(handle, childScope, () => referenced)); + yield* Effect.addFinalizer(() => shutdown(delegateHandle, childScope, () => referenced)); yield* handle.exitCode.pipe( Effect.exit, Effect.andThen(Scope.close(childScope, Exit.void)),