Skip to content

Commit 93b5660

Browse files
committed
fix(broker): keep a pending unsubscribe outstanding until it settles
A release that followed a timed-out claim installed a fresh cleanup wrapper that waited for the original unsubscribe for only another five seconds, then settled as skipped once a retried claim owned the thread. The retry could then resume upstream while the original unsubscribe was still in flight. Make a wrapper wait for its predecessor without a bound so the pending entry never settles before every underlying request has, and reject retried claims until then.
1 parent f0ddd6b commit 93b5660

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,13 @@ async function main() {
191191
const previous = pendingUnsubscribes.get(threadId);
192192
const execute = async () => {
193193
if (previous) {
194-
await settleWithin(previous, UNSUBSCRIBE_WAIT_TIMEOUT_MS);
194+
// Wait without a bound. The pending entry must not settle while any
195+
// earlier upstream unsubscribe for this thread is still outstanding,
196+
// otherwise a retried claim could slip past a hung cleanup request.
197+
await previous.then(
198+
() => {},
199+
() => {}
200+
);
195201
}
196202
if (threadSockets.has(threadId)) {
197203
return { result: null, error: null, skipped: true };

‎tests/broker-subscriptions.test.mjs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,41 @@ test("broker fails a resume when an in-flight unsubscribe outlives the bounded w
650650
await thirdClient.end();
651651
await secondClient.end();
652652
});
653+
654+
test("broker keeps rejecting claims until a hung unsubscribe settles", async (t) => {
655+
const broker = startBroker("resume-fails-unsubscribe-hangs");
656+
t.after(() => broker.stop());
657+
assert.equal(await broker.listening(), true, `broker never listened: ${broker.stderr()}`);
658+
659+
const firstClient = await connectClient(broker.socketPath);
660+
const threadId = (await firstClient.request("thread/start", { cwd: process.cwd(), ephemeral: false })).thread.id;
661+
await firstClient.end();
662+
const unsubscribeStarted = await waitFor(() =>
663+
readState(broker.statePath)?.unsubscribeRequests?.includes(threadId)
664+
);
665+
assert.equal(unsubscribeStarted, true, "unsubscribe request was not observed");
666+
667+
const attempt = async () => {
668+
const client = await connectClient(broker.socketPath);
669+
const outcome = await waitWithTimeout(
670+
client.request("thread/resume", { threadId }).then(
671+
() => ({ status: "fulfilled" }),
672+
(error) => ({ status: "rejected", error })
673+
),
674+
8000
675+
);
676+
await client.end();
677+
return outcome;
678+
};
679+
680+
// The first claim times out on the hung unsubscribe. Its release installs a
681+
// follow-up cleanup that must stay pending; a retry must still be rejected.
682+
const first = await attempt();
683+
assert.equal(first?.status, "rejected");
684+
assert.match(first.error.message, /still being released upstream/);
685+
const retry = await attempt();
686+
assert.equal(retry?.status, "rejected", "a retry slipped past the still-outstanding unsubscribe");
687+
assert.match(retry.error.message, /still being released upstream/);
688+
assert.deepEqual(readState(broker.statePath).requestOrder, []);
689+
assert.deepEqual(readState(broker.statePath).subscriptions, [threadId]);
690+
});

‎tests/fake-codex-fixture.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ rl.on("line", (line) => {
364364
}
365365
const thread = ensureThread(state, message.params.threadId);
366366
thread.updatedAt = now();
367-
if (BEHAVIOR === "unsubscribe-delayed") {
367+
if (BEHAVIOR === "unsubscribe-delayed" || BEHAVIOR === "resume-fails-unsubscribe-hangs") {
368368
state.requestOrder = [...(state.requestOrder || []), "thread/resume"];
369369
}
370370
state.subscriptions = [...new Set([...(state.subscriptions || []), thread.id])];

0 commit comments

Comments
 (0)