Potential Memory Leak in TanStack Start with SSR Streaming #5289
Which project does this relate to?#
Start
Describe the bug#
When a TanStack Start application is put under load, its memory usage grows linearly with each request that involves SSR streaming of a component. This eventually leads to performance degradation, connection errors, and server crashes. The issue seems specific to SSR-rendered pages. Pure API routes (e.g., a GET handler returning a simple response) do not exhibit the same memory growth. We have been able to reproduce this behavior on the Bun runtime.
Your Example Website or App#
https://github.com/TanStack/router/tree/main/examples/react/start-bun
Steps to Reproduce the Bug or Issue#
cd start-bun bun install bun run build && bun run start oha "http://localhost:3000/" -c 100 -n 10000
While the test is running, observe the server's memory usage using a tool like htop or your system's activity monitor.
Expected behavior#
The server's memory usage should remain stable and relatively low throughout the load test. The test should complete with a very high success rate (>99%) and minimal connection errors.
Screenshots or Videos#
https://github.com/user-attachments/assets/32c1ce0c-8bd9-4c3b-80b3-390f851f2ccc
Platform#
- Router / Start Version: Start: 1.132.24; Router: 1.132.23
- OS: Linux
- Browser: Chrome
- Browser Version: 140
- Bundler: vite
- Bundler Version: 7.1.7
Additional context#
No response
i have identified what causes this:
we call
toResponsefromh3the actual problematic part is this:
https://github.com/h3js/h3/blob/main/src/response.ts#L99
For bun,
FastResponseis justResponse, see https://github.com/h3js/srvx/blob/main/src/adapters/bun.ts#L13So what this does is really just creating a new
Responseto set different headers and re-use the previous response'sbody/(which in case of SSR streaming is aReadableStream).return new Response( val.body, { status: val.status, statusText: val.statusText, headers: mergeHeaders(eventHeaders, val.headers), }, )This works as expected in node (i was not able to reproduce that memory issue there).
So maybe this is just a bug in bun? @Jarred-Sumner
waiting for upstream bug to be resolved
@schiller-manuel The same seems to be happening with us while we are on node. The server rans out of memory in production after a couple days.
It's a little tricky to fix in Bun without regressions, still working on it.
I recommend against doing this though. The GC lifetime of ReadableStream is complex and passing the same ReadableStream into two Response objects makes it more complex. And likely one or more AbortSignal's are in use.
It would not be at all surprising if this pattern creates memory leaks in other runtimes and in browsers too.
@cloudycotton can you please provide a reproducer?
The issue has fixed in latest bun canary (pre 1.3).
jlucaso1(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Adding this snippet to the top provides some useful info:
We can see that
protectedObjectTypeCountsshows10000instances ofReadableStream:Adding some console.logs shows it does call at least one
end/closefunction for each ReadableStream. But there seem to be multiple ReadableStream's created per request. If the one directly attached to theResponseobject hasn't calledcloseorendor similar, that would cause this to happen.