|
| 1 | +/** |
| 2 | + * Cancellation of `trigger-long-running-operation` (#1124, #4846). |
| 3 | + * |
| 4 | + * The tool waits on a timer per step. Before #4846 it ignored the request's |
| 5 | + * cancellation: a cancelled call kept stepping and sending progress, and its |
| 6 | + * pending timer held a stdio server's process open after the client went |
| 7 | + * away. Each case cancels a call from a real SDK client, in both protocol |
| 8 | + * eras. |
| 9 | + * |
| 10 | + * - Over the stdio transport manager on in-memory pipes, the server's raw |
| 11 | + * output is read, since the SDK client silently drops progress for a |
| 12 | + * request it has cancelled: no progress may cross the wire after the |
| 13 | + * cancel, and the session keeps serving. |
| 14 | + * - The step timer is what kept the process alive, and nothing on the wire |
| 15 | + * shows whether it is still pending, so `node:timers/promises` is wrapped |
| 16 | + * (calling through) to show the tool's wait is abandoned on the cancel. |
| 17 | + * That wait clears its own timer when its signal aborts. |
| 18 | + */ |
| 19 | +import { PassThrough } from "node:stream"; |
| 20 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 21 | +import { Client } from "@modelcontextprotocol/client"; |
| 22 | +import { StdioServerTransport } from "@modelcontextprotocol/server/stdio"; |
| 23 | +import { connect, connectModern, contentOf, textOf } from "./harness.js"; |
| 24 | +import type { Session } from "./harness.js"; |
| 25 | +import { main, type StdioSession } from "../transports/stdio.js"; |
| 26 | + |
| 27 | +const timers = vi.hoisted(() => ({ |
| 28 | + sleeps: [] as Array<{ ms: number | undefined; settled: Promise<unknown> }>, |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock("node:timers/promises", async (importOriginal) => { |
| 32 | + const actual = await importOriginal<typeof import("node:timers/promises")>(); |
| 33 | + return { |
| 34 | + ...actual, |
| 35 | + setTimeout: <T>( |
| 36 | + ms?: number, |
| 37 | + value?: T, |
| 38 | + options?: Parameters<typeof actual.setTimeout>[2], |
| 39 | + ) => { |
| 40 | + const promise = actual.setTimeout(ms, value, options); |
| 41 | + timers.sleeps.push({ |
| 42 | + ms, |
| 43 | + settled: promise.then( |
| 44 | + () => "elapsed", |
| 45 | + (error: unknown) => error, |
| 46 | + ), |
| 47 | + }); |
| 48 | + return promise; |
| 49 | + }, |
| 50 | + }; |
| 51 | +}); |
| 52 | + |
| 53 | +let session: Session | undefined; |
| 54 | +const started: StdioSession[] = []; |
| 55 | + |
| 56 | +afterEach(async () => { |
| 57 | + timers.sleeps.length = 0; |
| 58 | + await session?.close(); |
| 59 | + session = undefined; |
| 60 | + for (const { shutdown } of started.splice(0)) { |
| 61 | + process.off("SIGINT", shutdown); |
| 62 | + } |
| 63 | +}); |
| 64 | + |
| 65 | +const delay = (ms: number) => |
| 66 | + new Promise<void>((resolve) => setTimeout(resolve, ms)); |
| 67 | + |
| 68 | +describe.each([ |
| 69 | + ["2025-era", {}], |
| 70 | + ["2026-07-28", { versionNegotiation: { mode: { pin: "2026-07-28" } } }], |
| 71 | +] as const)("over stdio, a cancelled call (%s)", (_era, opts) => { |
| 72 | + it("sends no progress after the cancel, and the session keeps serving", async () => { |
| 73 | + const toServer = new PassThrough(); |
| 74 | + const fromServer = new PassThrough(); |
| 75 | + started.push(await main(toServer, fromServer)); |
| 76 | + // Every line the server writes, read alongside the client's transport. |
| 77 | + let wire = ""; |
| 78 | + fromServer.on("data", (chunk: Buffer) => { |
| 79 | + wire += chunk.toString(); |
| 80 | + }); |
| 81 | + const client = new Client({ name: "stdio-test", version: "0.0.0" }, opts); |
| 82 | + await client.connect(new StdioServerTransport(fromServer, toServer)); |
| 83 | + |
| 84 | + // Four 50 ms steps: uncancelled, the last progress lands at about 200 ms. |
| 85 | + const controller = new AbortController(); |
| 86 | + const call = client.callTool( |
| 87 | + { |
| 88 | + name: "trigger-long-running-operation", |
| 89 | + arguments: { duration: 0.2, steps: 4 }, |
| 90 | + }, |
| 91 | + { |
| 92 | + signal: controller.signal, |
| 93 | + onprogress: () => controller.abort("cancelled by test"), |
| 94 | + }, |
| 95 | + ); |
| 96 | + await expect(call).rejects.toBeDefined(); |
| 97 | + await delay(300); |
| 98 | + expect(wire.match(/"notifications\/progress"/g)).toHaveLength(1); |
| 99 | + |
| 100 | + const echo = await client.callTool({ |
| 101 | + name: "echo", |
| 102 | + arguments: { message: "after cancel" }, |
| 103 | + }); |
| 104 | + expect(echo.content).toEqual([ |
| 105 | + { type: "text", text: "Echo: after cancel" }, |
| 106 | + ]); |
| 107 | + await client.close(); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe.each([ |
| 112 | + ["2025-era", () => connect()], |
| 113 | + ["2026-07-28", () => connectModern()], |
| 114 | +])("a cancelled call (%s)", (_era, open) => { |
| 115 | + it("abandons its pending step wait, so no timer holds the process open", async () => { |
| 116 | + session = await open(); |
| 117 | + const controller = new AbortController(); |
| 118 | + const call = session.client.callTool( |
| 119 | + { |
| 120 | + name: "trigger-long-running-operation", |
| 121 | + arguments: { duration: 20, steps: 2 }, |
| 122 | + }, |
| 123 | + { signal: controller.signal }, |
| 124 | + ); |
| 125 | + await vi.waitFor(() => expect(timers.sleeps).toHaveLength(1)); |
| 126 | + controller.abort("cancelled by test"); |
| 127 | + await expect(call).rejects.toBeDefined(); |
| 128 | + |
| 129 | + // The 10 s wait settles at once, rejected by the abort, not elapsed. |
| 130 | + expect(timers.sleeps[0]?.ms).toBe(10_000); |
| 131 | + const outcome = await timers.sleeps[0]?.settled; |
| 132 | + expect(outcome).toMatchObject({ name: "AbortError" }); |
| 133 | + // The loop stopped: no further step was scheduled. |
| 134 | + await delay(20); |
| 135 | + expect(timers.sleeps).toHaveLength(1); |
| 136 | + |
| 137 | + const result = await session.client.callTool({ |
| 138 | + name: "echo", |
| 139 | + arguments: { message: "still here" }, |
| 140 | + }); |
| 141 | + expect(textOf(contentOf(result)[0])).toBe("Echo: still here"); |
| 142 | + }); |
| 143 | +}); |
0 commit comments