Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(mcp): avoid waiting for cloned response cancellation
Afloat8448f646c284
diff --git a/server/mcp-http-framing.test.ts b/server/mcp-http-framing.test.tsindex 695ad6e374..fc38ce425b 100644--- a/server/mcp-http-framing.test.ts+++ b/server/mcp-http-framing.test.ts@@ -1,8 +1,75 @@-import { describe, expect, it } from "vitest";+import { describe, expect, it, vi } from "vitest";import { RemoteMcpClient } from "./mcp-http.ts";describe("MCP event stream framing", () => {+ it.each([+ { name: "SSE byte limit", type: "text/event-stream", text: ":".repeat(129), error: "event stream too large" },+ { name: "notification failure", type: "text/event-stream", text: 'data: {"jsonrpc":"2.0","method":"notifications/tools/list_changed"}\n\n', error: "notification failed" },+ { name: "JSON byte limit", type: "application/json", text: " ".repeat(129), error: "response too large" },+ { name: "matched SSE response", type: "text/event-stream", text: 'data: {"jsonrpc":"2.0","id":1,"result":{"tools":[]}}\n\n', error: undefined },+ ])("settles a cloned response after $name while its sibling remains open", async ({ type, text, error }) => {+ let sourceCancelled = false;+ const original = new Response(new ReadableStream<Uint8Array>({+ start(controller) { controller.enqueue(new TextEncoder().encode(text)); },+ cancel() { sourceCancelled = true; },+ }), { headers: { "content-type": type } });+ const response = original.clone();+ const body = response.body!;+ const getReader = body.getReader.bind(body);+ let markCancellationStarted!: () => void;+ const cancellationStarted = new Promise<void>((resolve) => { markCancellationStarted = resolve; });+ const readerSpy = vi.spyOn(body, "getReader").mockImplementation(() => {+ const reader = getReader();+ const cancel = reader.cancel.bind(reader);+ reader.cancel = (reason?: unknown) => {+ const pending = cancel(reason);+ markCancellationStarted();+ return pending;+ };+ return reader;+ });+ const client = new RemoteMcpClient({ type: "http", url: "https://fixture.example/mcp", headers: {} }, {+ maxBytes: 128,+ fetch: async () => response,+ onNotification: () => { throw new Error("notification failed"); },+ });+ const result = client.request("tools/list", {}, new AbortController().signal).then(+ (value) => ({ value, error: undefined }),+ (failure: unknown) => ({ value: undefined, error: failure }),+ );+ try {+ await cancellationStarted;+ expect(body.locked).toBe(false);+ const settled = await result;+ if (error) expect(settled.error).toEqual(expect.objectContaining({ message: error }));+ else expect(settled).toEqual({ value: { tools: [] }, error: undefined });+ expect(sourceCancelled).toBe(false);+ } finally {+ await original.body!.cancel();+ await result;+ readerSpy.mockRestore();+ await client.close();+ }+ });++ it.each(["text/event-stream", "application/json"])("keeps the %s size error when cancellation rejects", async (type) => {+ const body = new ReadableStream<Uint8Array>({+ start(controller) { controller.enqueue(new TextEncoder().encode(" ".repeat(129))); },+ cancel() { return Promise.reject(new Error("cancellation failed")); },+ });+ const client = new RemoteMcpClient({ type: "http", url: "https://fixture.example/mcp", headers: {} }, {+ maxBytes: 128,+ fetch: async () => new Response(body, { headers: { "content-type": type } }),+ });+ try {+ await expect(client.request("tools/list", {}, new AbortController().signal)).rejects.toThrow(+ type === "text/event-stream" ? "event stream too large" : "response too large",+ );+ expect(body.locked).toBe(false);+ } finally { await client.close(); }+ });+it("cancels and unlocks an event stream rejected for exceeding the byte limit", async () => {let cancelled = false;const body = new ReadableStream<Uint8Array>({diff --git a/server/mcp-http.ts b/server/mcp-http.tsindex 708cab19d7..045d27e5c4 100644--- a/server/mcp-http.ts+++ b/server/mcp-http.ts@@ -150,7 +150,7 @@ async function readSse(}} finally {signal.removeEventListener("abort", onAbort);- await reader.cancel().catch(() => {});+ void reader.cancel().catch(() => {});reader.releaseLock();}}@@ -170,7 +170,7 @@ async function readBounded(response: Response, maxBytes = MAX_REMOTE_MCP_BYTES):text += decoder.decode(value, { stream: true });}} finally {- await reader.cancel().catch(() => {});+ void reader.cancel().catch(() => {});reader.releaseLock();}}