Skip to content

Memory leak when writing to a fetch request in stream #5665

@ceifa

Description

@ceifa

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:

const { readable, writable } = new TransformStream<Uint8Array>();
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 #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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions