Skip to content

Commit 1e41e55

Browse files
axisrowclaude
andcommitted
fix: bound turn/interrupt with a timeout so cancellation can't hang the companion
client.request("turn/interrupt", ...) had no timeoutMs, so a broker that accepted the connection and then went silent would block the companion until the host's own SIGKILL, leaving the original write-capable turn running with nobody able to reach it. Pass a 5s timeoutMs (with onTimeout force-closing the connection), mirroring the pattern already used for the initialize handshake. Fixes #47 (finding 2; the cheapest, highest-value item from the proposed fix list). Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_013hBHjrDA8DzzY2SEq4ieiS
1 parent fa3c630 commit 1e41e55

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎plugins/codex/scripts/lib/codex.mjs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ const REVIEW_THREAD_PREFIX = "Codex Companion Review";
5252
const EXTERNAL_AGENT_IMPORT_COMPLETED = "externalAgentConfig/import/completed";
5353
const EXTERNAL_AGENT_IMPORT_TIMEOUT_MS = 2 * 60 * 1000;
5454

55+
// Bound turn/interrupt itself (#47 finding 2). It is awaited inside
56+
// captureTurn's catch, in a window that by construction has only the
57+
// wall-clock ceiling's cleanup margin left before the host's own SIGKILL
58+
// (see DEFAULT_HARD_WALL_CLOCK_CEILING_MS below). Without a deadline here, a
59+
// broker that accepts the connection and then goes silent blocks the
60+
// companion until the host kills it, and the original turn keeps running
61+
// with nobody able to reach it.
62+
const TURN_INTERRUPT_TIMEOUT_MS = 5000;
63+
5564
// Hard upper bound on Codex agent inactivity within a single turn. Without
5665
// this, the completion await at the end of captureTurn is unbounded: it is
5766
// resolved ONLY by completeTurn() and is never rejected on a stalled/dead
@@ -1203,7 +1212,13 @@ export async function interruptAppServerTurn(cwd, { threadId, turnId }) {
12031212
reuseExistingBroker: true,
12041213
allowBusyStaleBroker: true
12051214
});
1206-
await client.request("turn/interrupt", { threadId, turnId });
1215+
await client.request("turn/interrupt", { threadId, turnId }, {
1216+
timeoutMs: TURN_INTERRUPT_TIMEOUT_MS,
1217+
// A wedged peer that accepted the connection but goes silent must not
1218+
// linger past the timeout: force-close so callers aren't blocked
1219+
// beyond TURN_INTERRUPT_TIMEOUT_MS regardless of transport.
1220+
onTimeout: () => client?.close().catch(() => {})
1221+
});
12071222
return {
12081223
attempted: true,
12091224
interrupted: true,

‎tests/fake-codex-fixture.mjs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,12 +828,14 @@ rl.on("line", (line) => {
828828
setTimeout(tick, gapMs);
829829
};
830830
setTimeout(tick, gapMs);
831-
} else if (BEHAVIOR === "long-progress-hits-wall-clock-ceiling") {
831+
} else if (BEHAVIOR === "long-progress-hits-wall-clock-ceiling" || BEHAVIOR === "silent-turn-interrupt") {
832832
// Continuously-progressing turn: one item/started, then a
833833
// commandExecution outputDelta every 500ms for ~20s. Every delta
834834
// resets the idle deadline, so the idle budget alone never fires --
835835
// only the separate hard wall-clock ceiling can stop this turn.
836-
// Registered as interruptible so turn/interrupt is observable.
836+
// Registered as interruptible so turn/interrupt is observable. When
837+
// BEHAVIOR is "silent-turn-interrupt" the turn/interrupt handler
838+
// above never responds, exercising #47's turn/interrupt timeout.
837839
send({ method: "turn/started", params: { threadId: thread.id, turn: buildTurn(turnId) } });
838840
send({
839841
method: "item/started",
@@ -930,6 +932,12 @@ rl.on("line", (line) => {
930932
turnId: message.params.turnId
931933
};
932934
saveState(state);
935+
// #47 finding 2 fixture: accept the connection and the request, but
936+
// never respond, so callers verify turn/interrupt is bounded by its
937+
// own timeout instead of hanging until the process is killed.
938+
if (BEHAVIOR === "silent-turn-interrupt") {
939+
break;
940+
}
933941
const pending = interruptibleTurns.get(message.params.turnId);
934942
if (pending) {
935943
clearTimeout(pending.timer);

‎tests/runtime.test.mjs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,6 +3357,39 @@ test("a foreground turn's wall-clock ceiling stays below the host's Bash SIGKILL
33573357
assert.ok(fixtureState.lastInterrupt, "foreground timeout must still interrupt the turn");
33583358
});
33593359

3360+
test("turn/interrupt itself is bounded: a broker that accepts it but never answers does not hang the companion (#47)", () => {
3361+
const repo = makeTempDir();
3362+
const binDir = makeTempDir();
3363+
installFakeCodex(binDir, "silent-turn-interrupt");
3364+
initGitRepo(repo);
3365+
fs.writeFileSync(path.join(repo, "README.md"), "hello\n");
3366+
run("git", ["add", "README.md"], { cwd: repo });
3367+
run("git", ["commit", "-m", "init"], { cwd: repo });
3368+
3369+
// Same continuously-progressing fixture as the wall-clock-ceiling tests, but
3370+
// the fixture's turn/interrupt handler accepts the request and never
3371+
// responds. Before the #47 fix, client.request("turn/interrupt", ...) had
3372+
// no timeoutMs, so captureTurn's catch would block on it indefinitely --
3373+
// bounded in practice only by the host's own SIGKILL. Assert the process
3374+
// still exits promptly: ceiling (2500ms) + interrupt timeout (5000ms) with
3375+
// generous headroom, well under the fixture's ~20s full run and far below
3376+
// a host kill.
3377+
const start = Date.now();
3378+
const result = run("node", [SCRIPT, "task", "--turn-timeout-ms", "2000", "test prompt"], {
3379+
cwd: repo,
3380+
env: { ...buildEnv(binDir), CODEX_TURN_HARD_CEILING_MS: "2500" }
3381+
});
3382+
const elapsedMs = Date.now() - start;
3383+
3384+
assert.notEqual(result.status, 0, "must fail via the wall-clock ceiling, not complete");
3385+
assert.ok(
3386+
elapsedMs < 15000,
3387+
`must exit within ceiling + interrupt timeout, not hang on the unanswered turn/interrupt (took ${elapsedMs}ms)`
3388+
);
3389+
const fixtureState = JSON.parse(fs.readFileSync(path.join(binDir, "fake-codex-state.json"), "utf8"));
3390+
assert.ok(fixtureState.lastInterrupt, "turn/interrupt must still have been attempted");
3391+
});
3392+
33603393
test("a long reasoning stretch keeps the turn alive: reasoning deltas are not opted out at handshake", () => {
33613394
const repo = makeTempDir();
33623395
const binDir = makeTempDir();

0 commit comments

Comments
 (0)