Skip to content

Commit 4d2d9e5

Browse files
cliffhallclaude
andcommitted
fix(filesystem): claim the initial roots load's generation before deferring it
Deferring initialize() a microtask (so its barrier is installed before roots/list is sent) also deferred its generation claim. The initial load now takes its generation synchronously in oninitialized and only its request is deferred, so a roots/list_changed handled before that request runs always counts as newer (Copilot on #5102). The SDK currently runs the deferred initial load before a later notification's handler, so this ordering is not reachable today (moving the claim back fails no test); the claim makes it explicit. New test: an update announced right after initialize wins over the held initial roots. Also renames the re-entrant announcement test. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 3836cf8 commit 4d2d9e5

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎src/filesystem/__tests__/server-roots.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ describe("roots/list_changed", () => {
584584

585585
// A client may announce a newer change while answering an older refresh's
586586
// roots/list; the older refresh must not replace the newer one's barrier.
587-
it("keeps the newest barrier when answering a refresh announces another change (#5101)", async () => {
587+
it("keeps the newest barrier when a client's roots/list answer announces another change (#5101)", async () => {
588588
let calls = 0;
589589
let announce: () => Promise<void> = async () => {};
590590
let releaseNewest!: () => void;
@@ -629,6 +629,37 @@ describe("roots/list_changed", () => {
629629
);
630630
});
631631

632+
// An update handled before the deferred initial roots/list runs must still
633+
// count as newer than the initial load (#5101).
634+
it("applies an update announced right after initialize over the initial roots (#5101)", async () => {
635+
let calls = 0;
636+
let releaseInitial!: () => void;
637+
const initialHeld = new Promise<void>(
638+
(resolve) => (releaseInitial = resolve),
639+
);
640+
const conn = connectTracked([], {
641+
capabilities: ROOTS,
642+
listRoots: async () => {
643+
calls += 1;
644+
if (calls === 1) {
645+
await initialHeld;
646+
return [rootOf(rootDir)];
647+
}
648+
return [rootOf(cliDir)];
649+
},
650+
});
651+
const { client } = await conn;
652+
await client.sendRootsListChanged();
653+
await vi.waitFor(() => expect(calls).toBe(2));
654+
releaseInitial();
655+
await vi.waitFor(() =>
656+
expect(stderr).toHaveBeenCalledWith(
657+
"Discarded a stale roots/list answer: a newer roots update has started",
658+
),
659+
);
660+
expect(await allowedDirectoriesOf(client)).toEqual([cliDir]);
661+
});
662+
632663
it("logs and keeps the current directories when the re-fetch fails", async () => {
633664
let fail = false;
634665
const { client } = await connectTracked([], {

‎src/filesystem/server.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,16 +839,18 @@ export function createServer(
839839

840840
// Handles post-initialization setup, specifically checking for and fetching
841841
// MCP roots. Tool calls wait for it (see `initialization` above), which is
842-
// installed before initialize() sends its roots/list.
842+
// installed before initialize() sends its roots/list. The initial load claims
843+
// its generation here, synchronously, so a roots/list_changed handled before
844+
// the deferred request runs takes a newer one and is not discarded as stale.
843845
server.server.oninitialized = () => {
844-
initialization = Promise.resolve().then(initialize);
846+
const generation = ++rootsGeneration;
847+
initialization = Promise.resolve().then(() => initialize(generation));
845848
};
846849

847-
async function initialize(): Promise<void> {
850+
async function initialize(generation: number): Promise<void> {
848851
const clientCapabilities = server.server.getClientCapabilities();
849852

850853
if (clientCapabilities?.roots) {
851-
const generation = ++rootsGeneration;
852854
try {
853855
const response = await server.server.listRoots();
854856
/* v8 ignore else -- the SDK validates the roots/list result against ListRootsResultSchema, which requires roots, so the else cannot run */

0 commit comments

Comments
 (0)