fix(server): bound explicit child process kills
This commit is contained in:
1 parent
45d301e835
commit
b9197b7e4c
2 files changed
+80
-17
No files matched your search
@@ -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<ChildProcessSpawner.ExitCode>();
|
||||
const signals: Array<ChildProcessShutdownSignal> = [];
|
||||
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<ChildProcessSpawner.ExitCode>();
|
||||
|
||||
@@ -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)),
|
||||
|
||||
Reference in new issue
Block a user