Skip to content

Commit de754b0

Browse files
committed
fix(broker): re-arm the idle timer when an orphaned stream finishes
Addresses the third review finding on #680. A client can disconnect while its streaming request is still awaiting a response. The close handler clears ownership and arms the timer, but the response then assigns the already-closed socket to activeStreamSocket. The pending timer therefore finds the broker "busy", declines to re-arm, and when turn/completed later clears that stale ownership nothing schedules again -- the empty broker stays resident forever, which is the leak this PR exists to close. Two changes, closing two variants: - armIdleTimer() is now also called when ownership is released: in routeNotification on turn/completed, and on both the success and error paths of a request. The socket that owned the stream may already be gone, so no further close handler will fire to schedule it. - isIdle() no longer counts ownership held by a destroyed socket as busy. That covers the second variant, where turn/completed never arrives at all and the stale ownership would otherwise pin the broker open indefinitely. A destroyed socket has nobody listening, so there is no stream left to protect. Test: a client starts a thread, fires turn/start, and destroys the socket without reading the response. Verified non-vacuous against 09338b1, where it fails with "broker stayed resident after the client abandoned a stream".
1 parent 09338b1 commit de754b0

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,18 @@ async function main() {
111111
const sockets = new Set();
112112
let idleTimer = null;
113113

114+
// Ownership can outlive its socket: a client can disconnect while its streaming request
115+
// is still awaiting a response, and the response then assigns the already-closed socket
116+
// to activeStreamSocket. A destroyed socket has nobody listening, so treating it as
117+
// "busy" would pin the broker open forever if turn/completed never arrives.
118+
function ownershipIsLive(socket) {
119+
return socket !== null && !socket.destroyed;
120+
}
121+
114122
// Idle means nobody is connected AND nothing is in flight. Holding an open socket is
115123
// enough to keep the broker alive, so a long streaming turn can never be cut short.
116124
function isIdle() {
117-
return sockets.size === 0 && activeRequestSocket === null && activeStreamSocket === null;
125+
return sockets.size === 0 && !ownershipIsLive(activeRequestSocket) && !ownershipIsLive(activeStreamSocket);
118126
}
119127

120128
function disarmIdleTimer() {
@@ -170,6 +178,9 @@ async function main() {
170178
if (activeRequestSocket === target) {
171179
activeRequestSocket = null;
172180
}
181+
// The socket that owned this stream may already be gone, in which case no close
182+
// handler will fire again -- schedule here or the broker never idles out.
183+
armIdleTimer();
173184
}
174185
}
175186
}
@@ -288,6 +299,7 @@ async function main() {
288299
if (activeRequestSocket === socket) {
289300
activeRequestSocket = null;
290301
}
302+
armIdleTimer();
291303
} catch (error) {
292304
send(socket, {
293305
id: message.id,
@@ -299,6 +311,7 @@ async function main() {
299311
if (activeStreamSocket === socket && !isStreaming) {
300312
activeStreamSocket = null;
301313
}
314+
armIdleTimer();
302315
}
303316
}
304317
});

‎tests/broker-idle.test.mjs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,59 @@ test("broker accepts an idle timeout exactly at the Node timer limit", async (t)
179179
broker.child.kill("SIGTERM");
180180
await broker.exitedWithin(8000);
181181
});
182+
183+
function sendLine(socket, message) {
184+
socket.write(`${JSON.stringify(message)}\n`);
185+
}
186+
187+
function readReply(socket, id) {
188+
return new Promise((resolve, reject) => {
189+
let buffer = "";
190+
socket.on("data", (chunk) => {
191+
buffer += chunk;
192+
let index;
193+
while ((index = buffer.indexOf("\n")) >= 0) {
194+
const line = buffer.slice(0, index);
195+
buffer = buffer.slice(index + 1);
196+
if (!line.trim()) continue;
197+
let message;
198+
try {
199+
message = JSON.parse(line);
200+
} catch {
201+
continue;
202+
}
203+
if (message.id === id) resolve(message);
204+
}
205+
});
206+
socket.on("error", reject);
207+
});
208+
}
209+
210+
test("broker still idles out after a client abandons a streaming request", async (t) => {
211+
// A client can disconnect while turn/start is still awaiting its response. The close
212+
// handler arms the timer, but the response then assigns the already-closed socket to
213+
// activeStreamSocket, so the timer finds the broker "busy" and does not re-arm. When
214+
// turn/completed later clears that stale ownership nothing schedules again, and the
215+
// empty broker stays resident forever.
216+
const broker = startBroker({ idleTimeout: 400 });
217+
t.after(() => broker.dispose());
218+
assert.equal(await broker.listening(), true, `broker never listened: ${broker.stderr()}`);
219+
220+
const socket = await connectTo(broker.socketPath);
221+
const started = readReply(socket, 1);
222+
sendLine(socket, { id: 1, method: "thread/start", params: { cwd: process.cwd(), ephemeral: true } });
223+
const threadId = (await started).result?.thread?.id;
224+
assert.ok(threadId, "fake codex did not start a thread");
225+
226+
// Fire a streaming request and abandon it immediately, without reading the response.
227+
sendLine(socket, {
228+
id: 2,
229+
method: "turn/start",
230+
params: { threadId, input: [{ type: "text", text: "hello" }] }
231+
});
232+
socket.destroy();
233+
234+
const result = await broker.exitedWithin(15000);
235+
assert.ok(result, `broker stayed resident after the client abandoned a stream: ${broker.stderr()}`);
236+
assert.equal(result.code, 0);
237+
});

0 commit comments

Comments
 (0)