Compare commits

...
1 Commits
Author SHA1 Message Date
Julius Marminge 32ebbe1a4c 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
2026-05-08 01:45:38 -07:00
4 changed files with 46 additions and 15 deletions

No files matched your search

@@ -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",
}),
)
@@ -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<void>();
const finalized = yield* Deferred.make<void>();
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);
}),
);
});
@@ -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) =>
<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, Exclude<R, Scope.Scope>> =>
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<A, E, Exclude<R, Scope.Scope>>;
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<A, E, Exclude<R, Scope.Scope>>;
@@ -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)),