Skip to content

Commit cfc4188

Browse files
committed
fix(broker): release stale socket ownership instead of tolerating it
Addresses the fourth review finding on #680, which is a defect the previous commit introduced. de754b0 taught isIdle() to ignore ownership held by a destroyed socket, but the busy guard and the notification target still tested those fields for non-null. The three checks could therefore disagree: after an abandoned streaming request assigned its destroyed socket to activeStreamSocket, the broker reported itself idle while answering every new client with BROKER_BUSY -- and because a connected client keeps sockets.size above zero, it never shut down either. A shared broker stuck in that state is worse than the leak this PR set out to fix, since callers fall back to direct app servers and the broker never recovers. Rather than teach one more predicate to tolerate a stale owner, the stale owner is now released. releaseDeadOwnership() clears activeRequestSocket, and clears activeStreamSocket together with activeStreamThreadIds, whenever the socket is destroyed. It runs wherever ownership is inspected: when arming and when the timer fires, when a client connects, before the busy guard, and before choosing a notification target. isIdle() goes back to plain null checks, so no check can disagree with another about whether the broker is in use. Test: a client abandons a streaming request, then a second client connects and issues a request, which must be served rather than rejected. Verified non-vacuous against de754b0, where the reconnecting client is refused.
1 parent de754b0 commit cfc4188

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,25 @@ async function main() {
113113

114114
// Ownership can outlive its socket: a client can disconnect while its streaming request
115115
// 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;
116+
// to activeStreamSocket. Release it at every point where ownership is inspected, rather
117+
// than teaching one check to tolerate it -- the idle check, the notification target and
118+
// the busy guard must never disagree about whether the broker is in use. A stale owner
119+
// that only the idle check ignored would leave the broker rejecting every new client
120+
// with BROKER_BUSY while a connected client also kept it from ever shutting down.
121+
function releaseDeadOwnership() {
122+
if (activeRequestSocket !== null && activeRequestSocket.destroyed) {
123+
activeRequestSocket = null;
124+
}
125+
if (activeStreamSocket !== null && activeStreamSocket.destroyed) {
126+
activeStreamSocket = null;
127+
activeStreamThreadIds = null;
128+
}
120129
}
121130

122131
// Idle means nobody is connected AND nothing is in flight. Holding an open socket is
123132
// enough to keep the broker alive, so a long streaming turn can never be cut short.
124133
function isIdle() {
125-
return sockets.size === 0 && !ownershipIsLive(activeRequestSocket) && !ownershipIsLive(activeStreamSocket);
134+
return sockets.size === 0 && activeRequestSocket === null && activeStreamSocket === null;
126135
}
127136

128137
function disarmIdleTimer() {
@@ -134,11 +143,13 @@ async function main() {
134143

135144
function armIdleTimer() {
136145
disarmIdleTimer();
146+
releaseDeadOwnership();
137147
if (idleTimeoutMs <= 0 || !isIdle()) {
138148
return;
139149
}
140150
idleTimer = setTimeout(() => {
141151
idleTimer = null;
152+
releaseDeadOwnership();
142153
// Re-check at fire time: a client may have connected while the timer was pending.
143154
if (!isIdle()) {
144155
armIdleTimer();
@@ -165,6 +176,7 @@ async function main() {
165176
}
166177

167178
function routeNotification(message) {
179+
releaseDeadOwnership();
168180
const target = activeRequestSocket ?? activeStreamSocket;
169181
if (!target) {
170182
return;
@@ -208,6 +220,7 @@ async function main() {
208220

209221
const server = net.createServer((socket) => {
210222
disarmIdleTimer();
223+
releaseDeadOwnership();
211224
sockets.add(socket);
212225
socket.setEncoding("utf8");
213226
let buffer = "";
@@ -259,6 +272,7 @@ async function main() {
259272
continue;
260273
}
261274

275+
releaseDeadOwnership();
262276
const allowInterruptDuringActiveStream =
263277
isInterruptRequest(message) && activeStreamSocket && activeStreamSocket !== socket && !activeRequestSocket;
264278

‎tests/broker-idle.test.mjs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,43 @@ test("broker still idles out after a client abandons a streaming request", async
235235
assert.ok(result, `broker stayed resident after the client abandoned a stream: ${broker.stderr()}`);
236236
assert.equal(result.code, 0);
237237
});
238+
239+
test("broker serves a reconnecting client after a stream was abandoned", async (t) => {
240+
// Releasing a dead owner only inside the idle check is not enough: the busy guard still
241+
// sees the non-null destroyed socket and answers every new client with BROKER_BUSY,
242+
// while that connected client also keeps the broker from ever idling out. The result is
243+
// a shared broker that is simultaneously "idle" and unusable.
244+
const broker = startBroker({ idleTimeout: 0 });
245+
t.after(() => broker.dispose());
246+
assert.equal(await broker.listening(), true, `broker never listened: ${broker.stderr()}`);
247+
248+
const abandoned = await connectTo(broker.socketPath);
249+
const started = readReply(abandoned, 1);
250+
sendLine(abandoned, { id: 1, method: "thread/start", params: { cwd: process.cwd(), ephemeral: true } });
251+
const threadId = (await started).result?.thread?.id;
252+
assert.ok(threadId, "fake codex did not start a thread");
253+
254+
sendLine(abandoned, {
255+
id: 2,
256+
method: "turn/start",
257+
params: { threadId, input: [{ type: "text", text: "hello" }] }
258+
});
259+
abandoned.destroy();
260+
261+
// Let the abandoned response land and assign the destroyed socket as the stream owner.
262+
await delay(600);
263+
264+
const reconnect = await connectTo(broker.socketPath);
265+
t.after(() => reconnect.destroy());
266+
const replied = readReply(reconnect, 10);
267+
sendLine(reconnect, { id: 10, method: "thread/start", params: { cwd: process.cwd(), ephemeral: true } });
268+
269+
const reply = await Promise.race([replied, delay(10000).then(() => null)]);
270+
assert.ok(reply, "reconnecting client got no reply at all");
271+
assert.equal(
272+
reply.error?.message,
273+
undefined,
274+
`reconnecting client was rejected: ${reply.error?.message ?? ""}`
275+
);
276+
assert.ok(reply.result?.thread?.id, "reconnecting client did not get a usable thread");
277+
});

0 commit comments

Comments
 (0)