Skip to content

Commit 09338b1

Browse files
committed
fix(broker): close the listener before teardown and bound the idle timeout
Addresses both review findings on #680. Stop accepting before the asynchronous app-server teardown. shutdown() closed appClient first and only then closed the server, so the endpoint kept accepting for the whole of that await. ensureBrokerSession's readiness probe could connect during that window, judge a shutting-down broker "ready" and select it, then lose the connection with no retry -- and an accepted socket could leave server.close() waiting on an already-closed app server. server.close() now runs first, which stops listening immediately and resolves once existing connections drain, so the same reordering also covers the SIGTERM and SIGINT paths. Reject idle timeouts above Node's timer range. setTimeout() overflows past 2^31-1 ms: it emits TimeoutOverflowWarning and then fires after 1ms. A user asking for a 30-day timeout would therefore get an almost immediate shutdown -- the precise opposite of the request. Values above 2147483647 ms are now rejected at startup, consistent with how non-numeric and negative values are handled. Tests: an over-range timeout is rejected, exactly 2147483647 is accepted (the guard is not off by one), and the SIGTERM case now asserts that the socket and pidfile are cleaned up, covering the reordered shutdown on the signal path.
1 parent e19c574 commit 09338b1

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const STREAMING_METHODS = new Set(["turn/start", "review/start", "thread/compact
1717
// whether it is serving anyone -- an external sweep cannot, and racing one is unsafe.
1818
const DEFAULT_IDLE_TIMEOUT_MS = 30 * 60 * 1000;
1919
const IDLE_TIMEOUT_ENV = "CODEX_COMPANION_BROKER_IDLE_MS";
20+
// setTimeout() overflows above 2^31-1 ms: it warns and then fires after 1ms, so an
21+
// over-large timeout would shut the broker down almost immediately -- the exact opposite
22+
// of what was asked for. Reject instead, so the mistake is visible at startup.
23+
const MAX_IDLE_TIMEOUT_MS = 2147483647;
2024

2125
function resolveIdleTimeoutMs(rawOption, env = {}) {
2226
const raw = rawOption ?? env[IDLE_TIMEOUT_ENV];
@@ -35,6 +39,11 @@ function resolveIdleTimeoutMs(rawOption, env = {}) {
3539
`Invalid idle timeout ${JSON.stringify(text)}: expected a non-negative number of milliseconds.`
3640
);
3741
}
42+
if (parsed > MAX_IDLE_TIMEOUT_MS) {
43+
throw new Error(
44+
`Invalid idle timeout ${JSON.stringify(text)}: must be at most ${MAX_IDLE_TIMEOUT_MS} ms (Node timer limit).`
45+
);
46+
}
3847
return parsed;
3948
}
4049

@@ -166,11 +175,16 @@ async function main() {
166175
}
167176

168177
async function shutdown(server) {
178+
// Stop accepting FIRST. server.close() stops listening immediately and resolves once
179+
// existing connections drain. Tearing down the app server first would leave the
180+
// endpoint accepting throughout that await, so ensureBrokerSession's readiness probe
181+
// could connect, judge a shutting-down broker "ready", and then lose the connection.
182+
const closed = new Promise((resolve) => server.close(resolve));
169183
for (const socket of sockets) {
170184
socket.end();
171185
}
186+
await closed;
172187
await appClient.close().catch(() => {});
173-
await new Promise((resolve) => server.close(resolve));
174188
if (listenTarget.kind === "unix" && fs.existsSync(listenTarget.path)) {
175189
fs.unlinkSync(listenTarget.path);
176190
}

‎tests/broker-idle.test.mjs‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ test("broker with --idle-timeout 0 never idles out", async (t) => {
123123
await delay(1600);
124124
assert.equal(broker.alive(), true, "idle shutdown ran even though it was disabled");
125125

126+
// SIGTERM shares shutdown() with the idle path, so this also covers the ordering there.
126127
broker.child.kill("SIGTERM");
127-
await broker.exitedWithin(8000);
128+
const result = await broker.exitedWithin(8000);
129+
assert.ok(result, "broker did not exit on SIGTERM");
130+
assert.equal(fs.existsSync(broker.socketPath), false, "SIGTERM shutdown left the socket behind");
131+
assert.equal(fs.existsSync(broker.pidFile), false, "SIGTERM shutdown left the pidfile behind");
128132
});
129133

130134
test("broker rejects a non-numeric --idle-timeout instead of silently never expiring", async (t) => {
@@ -151,3 +155,27 @@ test("blank --idle-timeout falls back to the default instead of silently disabli
151155
broker.child.kill("SIGTERM");
152156
await broker.exitedWithin(8000);
153157
});
158+
159+
test("broker rejects an idle timeout beyond Node's timer range", async (t) => {
160+
// setTimeout() overflows above 2^31-1 and fires after 1ms, which would shut the broker
161+
// down almost immediately. Asking for ~30 days must fail loudly, not silently invert.
162+
const broker = startBroker({ idleTimeout: 30 * 24 * 60 * 60 * 1000 });
163+
t.after(() => broker.dispose());
164+
165+
const result = await broker.exitedWithin(8000);
166+
assert.ok(result, "broker did not exit on an out-of-range idle timeout");
167+
assert.equal(result.code, 1);
168+
assert.match(broker.stderr(), /must be at most 2147483647 ms/);
169+
});
170+
171+
test("broker accepts an idle timeout exactly at the Node timer limit", async (t) => {
172+
// Boundary: 2147483647 is valid, so the guard must not be off by one.
173+
const broker = startBroker({ idleTimeout: 2147483647 });
174+
t.after(() => broker.dispose());
175+
176+
assert.equal(await broker.listening(), true, `broker never listened: ${broker.stderr()}`);
177+
assert.equal(broker.alive(), true, "broker rejected a timeout that is exactly at the limit");
178+
179+
broker.child.kill("SIGTERM");
180+
await broker.exitedWithin(8000);
181+
});

0 commit comments

Comments
 (0)