Skip to content

Commit 00015e3

Browse files
committed
[CC-5589] Bound detached test broker lifetime
Attributed-To: Codex
1 parent d735a66 commit 00015e3

3 files changed

Lines changed: 77 additions & 3 deletions

File tree

‎plugins/codex/scripts/app-server-broker.mjs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { getLiveProcessPids, getProcessIdentity } from "./lib/process.mjs";
1919

2020
const DEFAULT_CHILD_IDLE_MS = 5 * 60 * 1000;
2121
const STREAMING_METHODS = new Set(["turn/start", "review/start", "thread/compact/start"]);
22+
const TEST_BROKER_TTL_ENV = "CODEX_COMPANION_TEST_BROKER_TTL_MS";
2223
const BROKER_CLEANUP_UNVERIFIED_RPC_CODE = -32002;
2324
const BROKER_SHUTDOWN_RPC_CODE = -32003;
2425
const BROKER_NOT_ACTIVATED_RPC_CODE = -32004;
@@ -97,6 +98,18 @@ function writePidFile(pidFile) {
9798
fs.writeFileSync(pidFile, `${process.pid}\n`, "utf8");
9899
}
99100

101+
function resolveTestBrokerTtlMs() {
102+
const raw = process.env[TEST_BROKER_TTL_ENV];
103+
if (raw == null || raw === "") {
104+
return null;
105+
}
106+
const value = Number(raw);
107+
if (!Number.isInteger(value) || value < 100 || value > 10 * 60 * 1000) {
108+
throw new Error(`${TEST_BROKER_TTL_ENV} must be an integer from 100 to 600000 milliseconds.`);
109+
}
110+
return value;
111+
}
112+
100113
async function main() {
101114
const [subcommand, ...argv] = process.argv.slice(2);
102115
if (subcommand !== "serve") {
@@ -117,6 +130,7 @@ async function main() {
117130
const listenTarget = parseBrokerEndpoint(endpoint);
118131
const pidFile = options["pid-file"] ? path.resolve(options["pid-file"]) : null;
119132
const activationRequired = options["require-activation-stdin"] === true;
133+
const testBrokerTtlMs = resolveTestBrokerTtlMs();
120134
writePidFile(pidFile);
121135

122136
const childIdleMs = resolveChildIdleMs();
@@ -141,6 +155,7 @@ async function main() {
141155
let streamTurnCounter = 0;
142156
let blockedCleanup = null;
143157
let brokerRegistration = null;
158+
let testBrokerTtlTimer = null;
144159
const sockets = new Set();
145160

146161
function getBrokerRegistration() {
@@ -458,6 +473,10 @@ async function main() {
458473
server.close(resolve);
459474
});
460475
shutdownPromise = (async () => {
476+
if (testBrokerTtlTimer) {
477+
clearTimeout(testBrokerTtlTimer);
478+
testBrokerTtlTimer = null;
479+
}
461480
cancelChildIdleClose();
462481
for (const socket of sockets) {
463482
socket.destroy();
@@ -722,6 +741,18 @@ async function main() {
722741
if (activationRequired) {
723742
setupActivationGate(server);
724743
}
744+
if (testBrokerTtlMs != null) {
745+
// Test suites intentionally exercise real detached broker behavior. A
746+
// bounded test-only lifetime prevents an interrupted runner from leaving
747+
// its fixture broker behind after normal teardown becomes impossible.
748+
testBrokerTtlTimer = setTimeout(() => {
749+
testBrokerTtlTimer = null;
750+
void shutdown(server)
751+
.catch(() => {})
752+
.finally(() => process.exit(0));
753+
}, testBrokerTtlMs);
754+
testBrokerTtlTimer.unref?.();
755+
}
725756
});
726757
}
727758

‎tests/fake-codex-fixture.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ export function buildEnv(binDir) {
726726
const sep = process.platform === "win32" ? ";" : ":";
727727
return {
728728
...process.env,
729+
CODEX_COMPANION_TEST_BROKER_TTL_MS: "30000",
729730
PATH: `${binDir}${sep}${process.env.PATH}`
730731
};
731732
}

‎tests/runtime.test.mjs‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,38 @@ test("an unreachable registered broker is never terminated while an owner is liv
15271527
assert.equal(loadBrokerSession(repo)?.pid, first.pid);
15281528
});
15291529

1530+
test("a detached test broker self-expires when its test runner cannot clean it", async (t) => {
1531+
if (process.platform === "win32") {
1532+
t.skip("Unix broker sockets are required for this contract.");
1533+
return;
1534+
}
1535+
1536+
const repo = makeTempDir();
1537+
const socketPath = path.join("/private/tmp", `cxc-test-ttl-${process.pid}-${Date.now()}.sock`);
1538+
const endpoint = `unix:${socketPath}`;
1539+
const broker = spawn(process.execPath, [BROKER_SCRIPT, "serve", "--endpoint", endpoint, "--cwd", repo], {
1540+
cwd: repo,
1541+
env: {
1542+
...process.env,
1543+
CODEX_COMPANION_TEST_BROKER_TTL_MS: "250"
1544+
},
1545+
detached: true,
1546+
stdio: "ignore"
1547+
});
1548+
broker.unref();
1549+
const ownership = captureProcessOwnership(broker.pid, { cwd: repo });
1550+
t.after(async () => {
1551+
await terminateProcessTree(broker.pid, {
1552+
expectedRootIdentity: ownership.rootIdentity,
1553+
ownershipSnapshot: ownership
1554+
}).catch(() => {});
1555+
});
1556+
1557+
assert.equal(await waitForBrokerEndpoint(endpoint, 2000), true);
1558+
await waitFor(() => !hasLiveProcessIdentity(broker.pid, ownership.rootIdentity), { timeoutMs: 2000 });
1559+
assert.equal(fs.existsSync(socketPath), false);
1560+
});
1561+
15301562
test("pre-activation broker exits when its launcher pipe closes", async (t) => {
15311563
if (process.platform === "win32") {
15321564
t.skip("Unix process groups are required for this contract.");
@@ -1541,7 +1573,14 @@ test("pre-activation broker exits when its launcher pipe closes", async (t) => {
15411573
const child = spawn(
15421574
process.execPath,
15431575
[BROKER_SCRIPT, "serve", "--endpoint", endpoint, "--cwd", repo, "--pid-file", pidFile, "--require-activation-stdin"],
1544-
{ cwd: repo, detached: true, stdio: ["pipe", "ignore", "ignore"] }
1576+
{
1577+
cwd: repo,
1578+
env: { ...process.env, CODEX_COMPANION_TEST_BROKER_TTL_MS: "30000" },
1579+
detached: true,
1580+
stdio: ["pipe", "ignore", "ignore"],
1581+
timeout: 30000,
1582+
killSignal: "SIGKILL"
1583+
}
15451584
);
15461585
const ownershipSnapshot = captureProcessOwnership(child.pid, { cwd: repo });
15471586
t.after(async () => {
@@ -1595,8 +1634,10 @@ test("an unregistered broker refuses to activate a detached app-server child", a
15951634
const broker = spawn(process.execPath, [BROKER_SCRIPT, "serve", "--endpoint", endpoint, "--cwd", repo], {
15961635
cwd: repo,
15971636
env,
1598-
detached: true,
1599-
stdio: ["ignore", "ignore", "ignore"]
1637+
detached: false,
1638+
stdio: ["ignore", "ignore", "ignore"],
1639+
timeout: 30000,
1640+
killSignal: "SIGKILL"
16001641
});
16011642
const brokerOwnership = captureProcessOwnership(broker.pid, { cwd: repo, env });
16021643
t.after(async () => {
@@ -1605,6 +1646,7 @@ test("an unregistered broker refuses to activate a detached app-server child", a
16051646
expectedRootIdentity: brokerOwnership.rootIdentity,
16061647
ownershipSnapshot: brokerOwnership
16071648
}).catch(() => {});
1649+
await waitFor(() => !hasLiveProcessIdentity(broker.pid, brokerOwnership.rootIdentity));
16081650
});
16091651
assert.equal(await waitForBrokerEndpoint(endpoint, 2000), true);
16101652

0 commit comments

Comments
 (0)