cloudflare / cloudflare/workerd

Memory leak when writing to a fetch request in stream

Open
#5,665 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

I'm not sure if should be an issue on this repo or workers-sdk.

### Versions

wrangler 4.53.0, Windows 11

### How to reproduce

Create a worker with the following code on the fetch handler:

```typescript
const { readable, writable } = new TransformStream();
const writer = writable.getWriter();

const responsePromise = fetch('https://reqbin.com/echo/post/json', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'PostmanRuntime/7.49.1',
},
body: readable,
});

const encoder = new TextEncoder()

for (let i = 0; i < 1000000000; i++) {
await writer.ready;
await writer.write(encoder.encode(JSON.stringify({ hello: 'world' })));
await new Promise((resolve) => setTimeout(resolve, 1));
}

await writer.close();

const response = await responsePromise;
if (!response.ok) {
return new Response(`Upstream server responded with ${response.status}`, { status: 502 });
}

return response;
```

I expect it to stream to run clean in terms of memory, keep streaming to network and doesn't hold any value, but what is happening is, after some iterations, it keeps growing in memory, and if I take a heap snapshot I can see a lot of promises created that never resolves. Can that be related to https://github.com/cloudflare/workerd/pull/4344 @danlapid @anonrig ?

It looks like a memory leak on workerd, I cannot reproduce it with node with the exact same code (and duplex: 'half' on fetch init).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.