Skip to content

Commit 162f7e8

Browse files
committed
fix(broker): retry cleanup when an explicit unsubscribe's requester leaves
When the final owner sends thread/unsubscribe and disconnects before the upstream request fails, nothing retried the cleanup: the owner was already removed, so the close path had nothing to release, and the explicit path restored ownership only to a still-open socket. Schedule the bounded automatic retry in that case. Test fixture: write state atomically so a test never reads a partial document, and add a delayed single-failure unsubscribe behavior for the new regression test.
1 parent e7fba1e commit 162f7e8

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ async function main() {
346346
if (outcome?.error) {
347347
if (!socket.destroyed && sockets.has(socket)) {
348348
addThreadOwner(socket, threadId);
349+
} else {
350+
// The requester is gone, so nobody will retry on its behalf. Fall back
351+
// to the automatic cleanup path for the now-unowned thread.
352+
scheduleUnsubscribeRetry(threadId, 0);
349353
}
350354
throw outcome.error;
351355
}

‎tests/broker-subscriptions.test.mjs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,20 @@ test("broker rolls back child threads inherited through a failed provisional cla
765765
assert.deepEqual(readState(broker.statePath).subscriptions, []);
766766
await secondClient.end();
767767
});
768+
769+
test("broker retries cleanup when a client disconnects during a failing explicit unsubscribe", async (t) => {
770+
const broker = startBroker("unsubscribe-fails-once-delayed");
771+
t.after(() => broker.stop());
772+
assert.equal(await broker.listening(), true, `broker never listened: ${broker.stderr()}`);
773+
774+
const client = await connectClient(broker.socketPath);
775+
const threadId = (await client.request("thread/start", { cwd: process.cwd(), ephemeral: false })).thread.id;
776+
// Send the explicit unsubscribe and drop the socket before the upstream reply.
777+
void client.request("thread/unsubscribe", { threadId }).catch(() => {});
778+
await waitFor(() => readState(broker.statePath)?.unsubscribeRequests?.length === 1);
779+
client.destroy();
780+
781+
// The first attempt fails 300 ms later, after the requester is gone; the broker must retry on its own.
782+
await waitForUnsubscribes(broker.statePath, [threadId, threadId]);
783+
assert.deepEqual(readState(broker.statePath).subscriptions, []);
784+
});

‎tests/fake-codex-fixture.mjs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ const readline = require("node:readline");
2525
}
2626
2727
function saveState(state) {
28-
fs.writeFileSync(STATE_PATH, JSON.stringify(state, null, 2));
28+
// Write atomically so a test reading the file never sees a partial document.
29+
const tmpPath = STATE_PATH + ".tmp";
30+
fs.writeFileSync(tmpPath, JSON.stringify(state, null, 2));
31+
fs.renameSync(tmpPath, STATE_PATH);
2932
}
3033
3134
function requiresExperimental(field, message, state) {
@@ -400,6 +403,13 @@ rl.on("line", (line) => {
400403
saveState(state);
401404
break;
402405
}
406+
if (BEHAVIOR === "unsubscribe-fails-once-delayed" && state.unsubscribeRequests.length === 1) {
407+
saveState(state);
408+
setTimeout(() => {
409+
send({ id: message.id, error: { code: -32000, message: "thread unsubscribe failed" } });
410+
}, 300);
411+
break;
412+
}
403413
if (
404414
BEHAVIOR === "unsubscribe-fails" ||
405415
(BEHAVIOR === "unsubscribe-fails-once" && state.unsubscribeRequests.length === 1)

0 commit comments

Comments
 (0)