The best way to control Claude Code, Codex and more from any computer or phone
fix(shared): stopping an MCP call releases a stalled JSON response body (#38025)
Co-authored-by: Grok 4.7 <noreply@x.ai> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 17, 2026 at 10:28 UTC
48359de5f4ee1803fca2af8c1afd96e03d5300de
2 files changed
+65
-41
packages/shared/src/mcpResponsePayloads.test.ts
+32
-26
@@ -93,31 +93,37 @@ it.effect("picks the SSE reader from the media type alone, whatever its case", (
93
}),
94
);
95
96
-it.effect("releases a stalled SSE response body when its consumer is interrupted", () =>
97
- Effect.gen(function* () {
98
- const reading = Promise.withResolvers<void>();
99
- let controller!: ReadableStreamDefaultController<Uint8Array>;
100
- const body = new ReadableStream<Uint8Array>(
101
- {
102
- start(value) {
103
- controller = value;
104
- },
105
- pull() {
106
- reading.resolve();
96
+it.effect.each(["text/event-stream", "application/json"])(
97
+ "releases a stalled %s response body when its consumer is interrupted",
98
+ (contentType) =>
99
+ Effect.gen(function* () {
100
+ const reading = Promise.withResolvers<void>();
101
+ let controller!: ReadableStreamDefaultController<Uint8Array>;
102
+ const body = new ReadableStream<Uint8Array>(
103
+ {
104
+ start(value) {
105
+ controller = value;
106
+ },
107
+ pull() {
108
+ reading.resolve();
109
+ },
110
},
108
- },
109
- { highWaterMark: 0 },
110
- );
111
- const response = new Response(body, { headers: { "content-type": "text/event-stream" } });
112
- const consumer = yield* Stream.runDrain(responsePayloads(response, String)).pipe(
113
- Effect.forkChild,
114
- );
115
- yield* Effect.promise(() => reading.promise);
116
- yield* Fiber.interrupt(consumer);
117
- try {
118
- expect(body.locked, "interrupted SSE response must release its body").toBe(false);
119
- } finally {
120
- if (body.locked) controller.close();
121
- }
122
- }),
111
+ { highWaterMark: 0 },
112
+ );
113
+ const response = new Response(body, { headers: { "content-type": contentType } });
114
+ const consumer = yield* Stream.runDrain(responsePayloads(response, String)).pipe(
115
+ Effect.forkChild,
116
+ );
117
+ yield* Effect.promise(() => reading.promise);
118
+ yield* Fiber.interrupt(consumer);
119
+ try {
120
+ expect(
121
+ () => controller.enqueue(new TextEncoder().encode("late response bytes")),
122
+ "interrupted MCP response must stop accepting body bytes",
123
+ ).toThrow(TypeError);
124
+ expect(body.locked, "interrupted MCP response must release its body").toBe(false);
125
+ } finally {
126
+ if (body.locked) controller.close();
127
+ }
128
+ }),
129
);
packages/shared/src/mcpResponsePayloads.ts
+33
-15
@@ -28,6 +28,30 @@ async function* sseDataLines(
28
}
29
}
30
31
+async function readText(reader: ReadableStreamDefaultReader<Uint8Array>): Promise<string> {
32
+ const decoder = new TextDecoder();
33
+ let text = "";
34
+ while (true) {
35
+ const { done, value } = await reader.read();
36
+ if (done) return text + decoder.decode();
37
+ text += decoder.decode(value, { stream: true });
38
+ }
39
+}
40
+
41
+// Interrupting a read cannot settle the promise parked on read(), so the reader is cancelled
42
+// from a finalizer instead; that settles the read and closes the body.
43
+const scopedReader = (body: ReadableStream<Uint8Array>) =>
44
+ Effect.acquireRelease(
45
+ Effect.sync(() => body.getReader()),
46
+ (reader) =>
47
+ Effect.promise(() =>
48
+ reader
49
+ .cancel()
50
+ .catch(() => undefined)
51
+ .then(() => reader.releaseLock()),
52
+ ),
53
+ );
54
+
55
export const discardResponseBody = (response: Response): Effect.Effect<void> =>
56
Effect.promise(() => response.body?.cancel().catch(() => undefined) ?? Promise.resolve());
57
@@ -38,27 +62,21 @@ export function responsePayloads<E>(
62
if (response.status === 202 || response.status === 204) {
63
return Stream.unwrap(discardResponseBody(response).pipe(Effect.as(Stream.empty)));
64
}
65
+ const body = response.body;
66
+ if (body === null) return Stream.empty;
67
const mediaType = (response.headers.get("content-type") ?? "").split(";", 1)[0]!.trim();
68
if (mediaType.toLowerCase() === "text/event-stream") {
43
- const body = response.body;
44
- if (body === null) return Stream.empty;
45
- // Interrupting fromAsyncIterable cannot return a generator parked on read(), so the
46
- // reader is cancelled from a finalizer instead; that settles the read and closes the body.
69
return Stream.unwrap(
48
- Effect.acquireRelease(
49
- Effect.sync(() => body.getReader()),
50
- (reader) =>
51
- Effect.promise(() =>
52
- reader
53
- .cancel()
54
- .catch(() => undefined)
55
- .then(() => reader.releaseLock()),
56
- ),
57
- ).pipe(Effect.map((reader) => Stream.fromAsyncIterable(sseDataLines(reader), onError))),
70
+ scopedReader(body).pipe(
71
+ Effect.map((reader) => Stream.fromAsyncIterable(sseDataLines(reader), onError)),
72
+ ),
73
).pipe(Stream.mapEffect((data) => Effect.try({ try: () => JSON.parse(data), catch: onError })));
74
}
75
return Stream.unwrap(
61
- Effect.tryPromise({ try: () => response.text(), catch: onError }).pipe(
76
+ scopedReader(body).pipe(
77
+ Effect.flatMap((reader) =>
78
+ Effect.tryPromise({ try: () => readText(reader), catch: onError }),
79
+ ),
80
Effect.flatMap((text) =>
81
text.trim().length === 0
82
? Effect.succeed(Stream.empty)