Skip to content

Commit f0ddd6b

Browse files
committed
fix(broker): fail a claim whose in-flight unsubscribe outlives the wait
When the bounded wait for an in-flight thread/unsubscribe expires, do not send the resume anyway: that would race the outstanding unsubscribe and could leave a tracked owner without an upstream subscription. Reject the request with a retryable error, release the provisional claim, and clear the busy state so other clients continue.
1 parent fc22719 commit f0ddd6b

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ const UNSUBSCRIBE_RETRY_DELAYS_MS = [100, 500, 2000];
1616
// the same thread. A hung cleanup request must not wedge the shared broker.
1717
const UNSUBSCRIBE_WAIT_TIMEOUT_MS = 5000;
1818

19+
// Resolves true once the promise settles, or false if the timeout expires first.
1920
function settleWithin(promise, timeoutMs) {
2021
let timer;
2122
return Promise.race([
2223
promise.then(
23-
() => {},
24-
() => {}
24+
() => true,
25+
() => true
2526
),
2627
new Promise((resolve) => {
27-
timer = setTimeout(resolve, timeoutMs);
28+
timer = setTimeout(() => resolve(false), timeoutMs);
2829
timer.unref?.();
2930
})
3031
]).finally(() => clearTimeout(timer));
@@ -459,13 +460,28 @@ async function main() {
459460
activeRequestSocket = socket;
460461
// Let an in-flight unsubscribe for the same thread settle first so it cannot
461462
// overtake the new subscription. The wait is bounded: a hung cleanup request
462-
// must not block this client or keep the broker busy for everyone else.
463-
await Promise.all(
463+
// must not block this client or keep the broker busy for everyone else. If
464+
// it expires, fail the request instead of racing the outstanding unsubscribe.
465+
const settled = await Promise.all(
464466
[...provisionalThreadIds].map((threadId) => {
465467
const pending = pendingUnsubscribes.get(threadId);
466-
return pending ? settleWithin(pending, UNSUBSCRIBE_WAIT_TIMEOUT_MS) : null;
468+
return pending ? settleWithin(pending, UNSUBSCRIBE_WAIT_TIMEOUT_MS) : true;
467469
})
468470
);
471+
if (settled.includes(false)) {
472+
send(socket, {
473+
id: message.id,
474+
error: buildJsonRpcError(
475+
-32000,
476+
"Codex thread is still being released upstream; retry the request shortly."
477+
)
478+
});
479+
if (activeRequestSocket === socket) {
480+
activeRequestSocket = null;
481+
}
482+
void releaseThreadOwners(socket, addedProvisionalThreadIds);
483+
return;
484+
}
469485

470486
try {
471487
const result =

‎tests/broker-subscriptions.test.mjs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ test("broker retries a transient upstream unsubscribe failure", async (t) => {
612612
assert.match(broker.stderr(), new RegExp(`Failed to unsubscribe Codex thread ${threadId}`));
613613
});
614614

615-
test("broker bounds the wait for a hung unsubscribe before a resume proceeds", async (t) => {
615+
test("broker fails a resume when an in-flight unsubscribe outlives the bounded wait", async (t) => {
616616
const broker = startBroker("resume-fails-unsubscribe-hangs");
617617
t.after(() => broker.stop());
618618
assert.equal(await broker.listening(), true, `broker never listened: ${broker.stderr()}`);
@@ -627,9 +627,18 @@ test("broker bounds the wait for a hung unsubscribe before a resume proceeds", a
627627

628628
const secondClient = await connectClient(broker.socketPath);
629629
const startedAt = Date.now();
630-
const resumed = await waitWithTimeout(secondClient.request("thread/resume", { threadId }), 8000);
631-
assert.notEqual(resumed, null, "resume never completed while the upstream unsubscribe hung");
630+
const resumed = await waitWithTimeout(
631+
secondClient.request("thread/resume", { threadId }).then(
632+
() => ({ status: "fulfilled" }),
633+
(error) => ({ status: "rejected", error })
634+
),
635+
8000
636+
);
637+
assert.notEqual(resumed, null, "resume never settled while the upstream unsubscribe hung");
638+
assert.equal(resumed.status, "rejected");
639+
assert.match(resumed.error.message, /still being released upstream/);
632640
assert.ok(Date.now() - startedAt >= 4000, "resume did not wait for the in-flight unsubscribe");
641+
// The resume was never sent upstream, so the fake still lists only the original subscription.
633642
assert.deepEqual(readState(broker.statePath).subscriptions, [threadId]);
634643

635644
const thirdClient = await connectClient(broker.socketPath);

0 commit comments

Comments
 (0)