From 32ebbe1a4c19c25a0e368a9b929d5847e3b3ebf1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 8 May 2026 01:45:38 -0700 Subject: [PATCH] Force app-server processes to exit promptly on teardown - set a 2s force-kill timeout for Codex app-server child processes - make scoped teardown interruption-safe so finalizers still run - add regression coverage for interrupted finalization --- .../provider/Layers/CodexSessionRuntime.ts | 2 ++ .../Layers/scopedSafeTeardown.test.ts | 21 +++++++++++- .../src/provider/Layers/scopedSafeTeardown.ts | 32 +++++++++++-------- .../effect-codex-app-server/src/client.ts | 6 +++- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/apps/server/src/provider/Layers/CodexSessionRuntime.ts b/apps/server/src/provider/Layers/CodexSessionRuntime.ts index 802a2e808a..6ec46e9e44 100644 --- a/apps/server/src/provider/Layers/CodexSessionRuntime.ts +++ b/apps/server/src/provider/Layers/CodexSessionRuntime.ts @@ -42,6 +42,7 @@ const BENIGN_ERROR_LOG_SNIPPETS = [ "state db missing rollout path for thread", "state db record_discrepancy: find_thread_path_by_id_str_in_subdir, falling_back", ]; +const CODEX_APP_SERVER_FORCE_KILL_AFTER = "2 seconds" as const; const RECOVERABLE_THREAD_RESUME_ERROR_SNIPPETS = [ "not found", "missing thread", @@ -702,6 +703,7 @@ export const makeCodexSessionRuntime = ( ChildProcess.make(options.binaryPath, ["app-server"], { cwd: options.cwd, env, + forceKillAfter: CODEX_APP_SERVER_FORCE_KILL_AFTER, shell: process.platform === "win32", }), ) diff --git a/apps/server/src/provider/Layers/scopedSafeTeardown.test.ts b/apps/server/src/provider/Layers/scopedSafeTeardown.test.ts index ebbc379b7d..255d194dfd 100644 --- a/apps/server/src/provider/Layers/scopedSafeTeardown.test.ts +++ b/apps/server/src/provider/Layers/scopedSafeTeardown.test.ts @@ -1,5 +1,5 @@ import { it } from "@effect/vitest"; -import { Cause, Effect, Exit } from "effect"; +import { Cause, Deferred, Effect, Exit, Fiber } from "effect"; import { describe, expect } from "vitest"; import { scopedSafeTeardown } from "./scopedSafeTeardown.ts"; @@ -91,4 +91,23 @@ describe("scopedSafeTeardown", () => { } }), ); + + it.effect("runs finalizers when the body is interrupted", () => + Effect.gen(function* () { + const started = yield* Deferred.make(); + const finalized = yield* Deferred.make(); + const wrapped = Effect.gen(function* () { + yield* Effect.addFinalizer(() => + Deferred.succeed(finalized, undefined).pipe(Effect.asVoid), + ); + yield* Deferred.succeed(started, undefined); + return yield* Effect.never; + }).pipe(scopedSafeTeardown("test")); + + const fiber = yield* wrapped.pipe(Effect.forkChild); + yield* Deferred.await(started); + yield* Fiber.interrupt(fiber); + yield* Deferred.await(finalized); + }), + ); }); diff --git a/apps/server/src/provider/Layers/scopedSafeTeardown.ts b/apps/server/src/provider/Layers/scopedSafeTeardown.ts index 688374590e..455479cbb0 100644 --- a/apps/server/src/provider/Layers/scopedSafeTeardown.ts +++ b/apps/server/src/provider/Layers/scopedSafeTeardown.ts @@ -23,9 +23,11 @@ * 1. Make a fresh scope manually. * 2. Run the body against that scope, capturing its Exit via * `Effect.exit`. - * 3. Close the scope, catching any cause (typed failure *or* defect) + * 3. Run the body inside an interruptible region, but capture the Exit from + * success, failure, defect, or interruption. + * 4. Close the scope, catching any cause (typed failure *or* defect) * with a log. - * 4. Replay the captured Exit so typed body failures still surface and + * 5. Replay the captured Exit so typed body failures still surface and * successes still return their value. * * The helper deliberately logs teardown causes at `Warning` level — @@ -34,7 +36,7 @@ * * @module provider/Layers/scopedSafeTeardown */ -import { Effect, Exit, Scope } from "effect"; +import { Effect, Scope } from "effect"; /** * Run `effect` with a freshly made `Scope.Scope`, guaranteeing that @@ -49,13 +51,17 @@ import { Effect, Exit, Scope } from "effect"; export const scopedSafeTeardown = (label: string) => (effect: Effect.Effect): Effect.Effect> => - Effect.gen(function* () { - const scope = yield* Scope.make(); - const bodyExit = yield* effect.pipe(Effect.provideService(Scope.Scope, scope), Effect.exit); - yield* Scope.close(scope, Exit.void).pipe( - Effect.catchCause((cause) => - Effect.logWarning(`${label} teardown errored; preserving body result`, cause), - ), - ); - return yield* bodyExit; - }) as Effect.Effect>; + Effect.uninterruptibleMask((restore) => + Effect.gen(function* () { + const scope = yield* Scope.make(); + const bodyExit = yield* restore( + effect.pipe(Effect.provideService(Scope.Scope, scope)), + ).pipe(Effect.exit); + yield* Scope.close(scope, bodyExit).pipe( + Effect.catchCause((cause) => + Effect.logWarning(`${label} teardown errored; preserving body result`, cause), + ), + ); + return yield* bodyExit; + }), + ) as Effect.Effect>; diff --git a/packages/effect-codex-app-server/src/client.ts b/packages/effect-codex-app-server/src/client.ts index 9821fef8d8..e8817cb17d 100644 --- a/packages/effect-codex-app-server/src/client.ts +++ b/packages/effect-codex-app-server/src/client.ts @@ -17,6 +17,8 @@ import { } from "./_internal/shared.ts"; import { makeChildStdio, makeTerminationError } from "./_internal/stdio.ts"; +const DEFAULT_APP_SERVER_FORCE_KILL_AFTER = "2 seconds" as const; + export interface CodexAppServerClientOptions { readonly logIncoming?: boolean; readonly logOutgoing?: boolean; @@ -279,6 +281,7 @@ export const layerCommand = ( const command = ChildProcess.make(options.command, [...(options.args ?? [])], { ...(options.cwd ? { cwd: options.cwd } : {}), ...(options.env ? { env: { ...process.env, ...options.env } } : {}), + forceKillAfter: DEFAULT_APP_SERVER_FORCE_KILL_AFTER, shell: process.platform === "win32", }); return yield* spawner.spawn(command).pipe( @@ -291,7 +294,8 @@ export const layerCommand = ( ), ); }), - (handle) => handle.kill().pipe(Effect.orDie), + (handle) => + handle.kill({ forceKillAfter: DEFAULT_APP_SERVER_FORCE_KILL_AFTER }).pipe(Effect.orDie), ).pipe( Effect.flatMap((handle) => make(makeChildStdio(handle), options, makeTerminationError(handle)),