Repository navigation
Description
Summary
For JS-backed streams, ReadableStream.prototype.pipeTo (and so pipeThrough) keeps every iteration of its loop pending until the pipe finishes. Each chunk leaves about 2 pending promises, plus the native objects their continuations hold, for every pipe stage. Memory grows linearly with the number of chunks and is released only when the stream closes. A long-lived stream through several TransformStream stages therefore grows without bound and eventually hits the isolate memory limit.
We hit this with LLM streaming via the Vercel AI SDK (streamText through @ai-sdk/gateway). One model call passes about 15 pipeThrough stages, so a 2-4 minute reasoning stream grows the live heap by 0.3-0.47 MB/s. Durable Objects running such streams are reset with "Durable Object's isolate exceeded its memory limit and was reset" (outcome exceededMemory) about 70 s into a stream.
Cause
WritableStreamJsController::pipeLoop (src/workerd/api/streams/standard.c++, unchanged on main as of 2026-11-25) continues the loop by returning the next iteration from the previous iteration's continuation:
auto onSuccess = JSG_VISITABLE_LAMBDA((this, ref = addRef()), (ref), (jsg::Lock& js) {
return pipeLoop(js);
});
...
return maybeAddFunctor(js, pipeLock.source.read(js), kj::mv(onSuccess), kj::mv(onFailure));Every iteration's promise is resolved with the next iteration's promise. V8 links them with handler-less reactions, so the chain stays alive back to the first iteration until the last one settles. A heap snapshot shows long chains (27k+ links each) of Promise β PromiseReaction with no fulfill/reject handlers, rooted in a global handle.
Reproduction
export default {
async fetch(request) {
const url = new URL(request.url);
const pipes = Number(url.searchParams.get("pipes") ?? 3);
const seconds = Number(url.searchParams.get("seconds") ?? 20);
const total = seconds * 200;
let i = 0;
let stream = new ReadableStream({
async pull(controller) {
if (i++ >= total) return controller.close();
await new Promise((r) => setTimeout(r, 5));
controller.enqueue({ i });
},
});
for (let p = 0; p < pipes; p++) {
stream = stream.pipeThrough(new TransformStream({ transform(chunk, c) { c.enqueue(chunk); } }));
}
const reader = stream.getReader();
while (!(await reader.read()).done) {}
return new Response("done");
},
};While the request runs, count live promises from the inspector: Runtime.queryObjects on Promise.prototype (it forces a full GC), plus Runtime.getHeapUsage.
Measured on workerd 1.20260601.1 (local, wrangler 4.97.0), about 200 chunks/s:
| Setup | Live promises over the run | Rate |
|---|---|---|
| Source read directly (pipes=0) | flat | 0/s |
Source + 3 identity pipeThrough |
320 β 25,622 in 21 s | ~1,200/s (~6 per chunk, ~2 per chunk per stage) |
Same, with pipeTo/pipeThrough replaced by an iterative JS read/write loop |
flat | 0/s |
fetch() body + 3 identity pipeThrough |
919 β 44,779 in 21 s | ~2,100/s |
AI SDK streamText over @ai-sdk/gateway (~15 stages) |
1,802 β 186,098 in 32 s | ~5,800/s, heap +0.31 MB/s |
Same streamText in a Durable Object, iterative JS pipe loop instead |
~flat | ~31/s |
Expected
Memory independent of how many chunks have passed through a pipe, as with an iterative loop and as in Node.js and browsers.
Suggested fix
Don't return the next iteration from the previous iteration's continuation. Drive the loop with a single pipe promise held on the pipe lock, settled once when the pipe ends, and start each iteration without chaining it to the previous one.
The experimental TypeScript streams (typescript_implemented_streams) drive their pipe with an iterative pump() loop in src/per_isolate/webstreams/readable.ts, which looks free of this pattern. That flag is $experimental, so deployed Workers can't use it. Is a fix to the C++ pipeLoop possible meanwhile, or is there a timeline for the TypeScript streams becoming the default?
Possibly related
- The
WRAPPABLE_EXTERNAL_MEMORYautogate (c81e418, 2026-11-18) charges each Wrappable's shallow size against the isolate. The retained chain holds native objects per chunk per stage, so with the gate on the same leak should reach the memory limit much sooner. Our resets started abruptly around 2026-11-25 with no change on our side, and they come earlier than the JS heap growth alone predicts. Can you confirm whether the gate was enabled for production then? - π Bug Report β Runtime APIs: AsyncLocalStorage store is never collected when it references a pending promise with a reaction attached inside run()Β #7611 and π Bug Report β Runtime APIs: Socket
closed/stream-EOF promises never settle at IoContext teardown, permanently leaking AsyncLocalStorage frames via pending reactionsΒ #7202: AsyncLocalStorage frames kept alive by pending reactions. Each promise in these chains carries a context tag, so with ALS in use the chains also keep their frames alive.
Environment
- workerd 1.20260601.1 locally (wrangler 4.97.0). Production on 2026-11-25.
compatibility_date = "2024-04-03",compatibility_flags = ["nodejs_compat_v2"]. No compatibility flag selects another pipe path.
Activity