Skip to content

Commit fd45841

Browse files
committed
fix(broker): roll back children inherited through a failed claim
A child notification that arrived while a provisional resume or review was waiting or in flight attributed the child to the claiming socket, and a rejected or failed claim released only the parent. The child then stayed owned until the socket closed. Record children inherited through an open claim and release them together with the claim when it is rejected or fails.
1 parent 68b4029 commit fd45841

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ async function main() {
136136
const threadSockets = new Map();
137137
const pendingUnsubscribes = new Map();
138138
const unsubscribeRetryTimers = new Map();
139+
// Threads a socket claimed for a request that has not succeeded yet, plus any
140+
// child threads it inherited through those claims while the request was open.
141+
const provisionalClaims = new Map();
139142

140143
function cancelUnsubscribeRetry(threadId) {
141144
const retry = unsubscribeRetryTimers.get(threadId);
@@ -303,7 +306,14 @@ async function main() {
303306
continue;
304307
}
305308
for (const socket of sourceOwners) {
306-
addThreadOwner(socket, subscribedThreadId);
309+
if (addThreadOwner(socket, subscribedThreadId)) {
310+
// Ownership inherited through a claim that has not succeeded yet is
311+
// rolled back with that claim.
312+
const claim = provisionalClaims.get(socket);
313+
if (claim?.threadIds.has(sourceThreadId)) {
314+
claim.inheritedThreadIds.add(subscribedThreadId);
315+
}
316+
}
307317
}
308318
}
309319
}
@@ -473,6 +483,16 @@ async function main() {
473483
addedProvisionalThreadIds.add(threadId);
474484
}
475485
}
486+
const claim = { threadIds: addedProvisionalThreadIds, inheritedThreadIds: new Set() };
487+
if (addedProvisionalThreadIds.size > 0) {
488+
provisionalClaims.set(socket, claim);
489+
}
490+
const rollBackClaim = () => {
491+
if (provisionalClaims.get(socket) === claim) {
492+
provisionalClaims.delete(socket);
493+
}
494+
void releaseThreadOwners(socket, new Set([...claim.threadIds, ...claim.inheritedThreadIds]));
495+
};
476496
activeRequestSocket = socket;
477497
// Let an in-flight unsubscribe for the same thread settle first so it cannot
478498
// overtake the new subscription, and so an explicit unsubscribe does not
@@ -501,7 +521,7 @@ async function main() {
501521
if (activeRequestSocket === socket) {
502522
activeRequestSocket = null;
503523
}
504-
void releaseThreadOwners(socket, addedProvisionalThreadIds);
524+
rollBackClaim();
505525
return;
506526
}
507527

@@ -511,6 +531,9 @@ async function main() {
511531
? await handleThreadUnsubscribe(socket, message.params ?? {})
512532
: await appClient.request(message.method, message.params ?? {});
513533
trackSubscriptionResults(socket, message.method, result);
534+
if (provisionalClaims.get(socket) === claim) {
535+
provisionalClaims.delete(socket);
536+
}
514537
send(socket, { id: message.id, result });
515538
if (isStreaming && !socket.destroyed && sockets.has(socket)) {
516539
activeStreamSocket = socket;
@@ -532,7 +555,7 @@ async function main() {
532555
}
533556
// Release after replying: a hung upstream unsubscribe must not withhold
534557
// the error or leave the broker busy for other clients.
535-
void releaseThreadOwners(socket, addedProvisionalThreadIds);
558+
rollBackClaim();
536559
}
537560
}
538561

‎tests/broker-subscriptions.test.mjs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,3 +725,35 @@ test("broker rejects an explicit unsubscribe that would queue behind a hung clea
725725
await thirdClient.end();
726726
await secondClient.end();
727727
});
728+
729+
test("broker rolls back child threads inherited through a failed provisional claim", async (t) => {
730+
const broker = startBroker("with-delayed-subagent");
731+
t.after(() => broker.stop());
732+
assert.equal(await broker.listening(), true, `broker never listened: ${broker.stderr()}`);
733+
734+
const firstClient = await connectClient(broker.socketPath);
735+
const threadId = (await firstClient.request("thread/start", { cwd: process.cwd(), ephemeral: false })).thread.id;
736+
await firstClient.request("turn/start", {
737+
threadId,
738+
input: [{ type: "text", text: "spawn a child while another client is resuming" }]
739+
});
740+
firstClient.destroy();
741+
await waitForUnsubscribes(broker.statePath, [threadId]);
742+
743+
// The resume fails after 250 ms; the delayed child arrives at 100 ms while the
744+
// claim is still open, so the claiming socket inherits it.
745+
const secondClient = await connectClient(broker.socketPath);
746+
await assert.rejects(
747+
secondClient.request("thread/resume", { threadId, persistFullHistory: true }),
748+
/forced resume failure after child arrival/
749+
);
750+
const childThread = readState(broker.statePath).threads.find(
751+
(thread) => thread.name === "delayed-design-challenger"
752+
);
753+
assert.ok(childThread, "delayed child thread was not created");
754+
755+
// Both the parent and the inherited child are released while the client stays connected.
756+
await waitForUnsubscribes(broker.statePath, [threadId, threadId, childThread.id]);
757+
assert.deepEqual(readState(broker.statePath).subscriptions, []);
758+
await secondClient.end();
759+
});

‎tests/fake-codex-fixture.mjs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ rl.on("line", (line) => {
347347
}
348348
349349
case "thread/resume": {
350+
if (BEHAVIOR === "with-delayed-subagent" && message.params.persistFullHistory === true) {
351+
setTimeout(() => {
352+
send({ id: message.id, error: { code: -32000, message: "forced resume failure after child arrival" } });
353+
}, 250);
354+
break;
355+
}
350356
if (BEHAVIOR === "resume-fails-unsubscribe-hangs" && message.params.persistFullHistory === true) {
351357
setTimeout(() => {
352358
send({ id: message.id, error: { code: -32000, message: "forced resume failure" } });

0 commit comments

Comments
 (0)