From 083fa715bfde55686f2b8a282a8961c71e65b362 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 1 May 2026 18:48:24 -0700 Subject: [PATCH] Scope VCS process lifetimes with a helper - Replace raw spawn access with `withProcess` for scoped use - Ensure child processes are finalized even on early exits - Keep `run` built on the scoped helper for safer cleanup --- apps/server/src/vcs/VcsProcess.ts | 71 ++++++++++++++----------------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/apps/server/src/vcs/VcsProcess.ts b/apps/server/src/vcs/VcsProcess.ts index e16e75be30..33e03a2551 100644 --- a/apps/server/src/vcs/VcsProcess.ts +++ b/apps/server/src/vcs/VcsProcess.ts @@ -1,14 +1,4 @@ -import { - Duration, - Context, - Effect, - Layer, - Option, - PlatformError, - Sink, - Stream, - type Scope, -} from "effect"; +import { Duration, Context, Effect, Layer, Option, PlatformError, Sink, Stream } from "effect"; import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; import { @@ -55,9 +45,10 @@ export interface VcsProcessHandle { } export interface VcsProcessShape { - readonly spawn: ( + readonly withProcess: ( input: VcsProcessInput, - ) => Effect.Effect; + use: (handle: VcsProcessHandle) => Effect.Effect, + ) => Effect.Effect; readonly run: (input: VcsProcessInput) => Effect.Effect; } @@ -161,6 +152,7 @@ export const make = Effect.fn("makeVcsProcess")(function* () { }), ), ); + yield* Effect.addFinalizer(() => child.kill().pipe(Effect.ignore)); const mapStreamError = (streamName: "stdout" | "stderr") => Stream.mapError((cause: PlatformError.PlatformError) => @@ -187,36 +179,39 @@ export const make = Effect.fn("makeVcsProcess")(function* () { } satisfies VcsProcessHandle; }); + const withProcess: VcsProcessShape["withProcess"] = (input, use) => + Effect.scoped(spawn(input).pipe(Effect.flatMap(use))); + const run = Effect.fn("VcsProcess.run")(function* (input: VcsProcessInput) { const timeoutMs = input.timeoutMs ?? DEFAULT_TIMEOUT_MS; const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES; const label = commandLabel(input.command, input.args); const runProcess = Effect.gen(function* () { - const child = yield* spawn(input); - - const [stdout, stderr, exitCode] = yield* Effect.all( - [ - collectText({ - operation: input.operation, - command: label, - cwd: input.cwd, - stream: child.stdout, - maxOutputBytes, - truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes ?? false, - }), - collectText({ - operation: input.operation, - command: label, - cwd: input.cwd, - stream: child.stderr, - maxOutputBytes, - truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes ?? false, - }), - child.exitCode, - input.stdin === undefined ? Effect.void : child.writeStdin(input.stdin), - ], - { concurrency: "unbounded" }, + const [stdout, stderr, exitCode] = yield* withProcess(input, (child) => + Effect.all( + [ + collectText({ + operation: input.operation, + command: label, + cwd: input.cwd, + stream: child.stdout, + maxOutputBytes, + truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes ?? false, + }), + collectText({ + operation: input.operation, + command: label, + cwd: input.cwd, + stream: child.stderr, + maxOutputBytes, + truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes ?? false, + }), + child.exitCode, + input.stdin === undefined ? Effect.void : child.writeStdin(input.stdin), + ], + { concurrency: "unbounded" }, + ), ).pipe(Effect.map(([stdout, stderr, exitCode]) => [stdout, stderr, exitCode] as const)); if (!input.allowNonZeroExit && exitCode !== 0) { @@ -258,7 +253,7 @@ export const make = Effect.fn("makeVcsProcess")(function* () { ); }); - return VcsProcess.of({ spawn, run }); + return VcsProcess.of({ withProcess, run }); }); export const layer = Layer.effect(VcsProcess, make());