router-core@1.171.7+: SSR query stream never closes with ssr-query β "Serialization timeout after app render finished" #7529
Bug#
router-core@1.171.7+ regresses SSR streaming when used with @tanstack/react-router-ssr-query: the dehydration query stream is never closed, so the response stream hangs until the 60s serialization timeout fires ("Serialization timeout after app render finished"). Real browsers paint early; curl/bots see the full ~30β60s hang.
Root cause#
PR #7497 ("fix streaming", landed in router-core@1.171.7) added a reserveStreamFastPath() fast path and guarded onRenderFinished:
// router-core 1.171.6 (works):
onRenderFinished: (listener) => renderFinishedListeners.push(listener),
// router-core 1.171.7+ (regressed):
reserveStreamFastPath() {
if (!cleanupStarted && _serializationFinished && !streamFastPathReserved
&& renderFinishedListeners.length === 0 && !injectedHtmlBuffer && !scriptBuffer.hasPending()) {
streamFastPathReserved = true;
return true;
}
return false;
},
onRenderFinished: (listener) => {
if (cleanupStarted || streamFastPathReserved) return; // β silently drops the listener
renderFinishedListeners.push(listener);
},
@tanstack/router-ssr-query-core@1.169.1 registers the query-stream close via onRenderFinished:
// router-ssr-query-core 1.169.1, setupCoreRouterSsrQueryIntegration
router.serverSsr.onRenderFinished(() => {
if (!queryStream.isClosed()) queryStream.close();
...
});
When reserveStreamFastPath() has already set streamFastPathReserved = true by the time the integration registers its onRenderFinished listener, that listener is silently dropped. queryStream is therefore never closed β serializationFinished never becomes true β transformStreamWithRouter hits the serialization timeout.
Versions#
- Broken:
@tanstack/react-router@1.170.x(βrouter-core@1.171.7β1.171.9) +@tanstack/react-router-ssr-query@1.167.1(βrouter-ssr-query-core@1.169.1) - Works:
@tanstack/react-router@1.169.0(βrouter-core@1.169.0), orrouter-core@1.171.6(pre-#7497, no guard)
Confirmed onRenderFinished is unguarded in 1.171.6 and guarded in 1.171.7, 1.171.8, 1.171.9.
Reproduction#
React SSR with streaming (renderToReadableStream + transformReadableStreamWithRouter) and setupRouterSsrQueryIntegration, where a route loader uses ensureQueryData/prefetchQuery so there are dehydrated queries. On a route that resolves all queries quickly, reserveStreamFastPath() fires before the ssr-query integration registers its close listener β the stream hangs.
Expected#
The latest published @tanstack/react-router-ssr-query is 1.167.1, which does not appear to account for reserveStreamFastPath, so there is currently no version combination on router-core@1.171.7+ that closes the query stream. Either:
onRenderFinishedshould still register (or immediately invoke) the listener when the fast path is already reserved, or- ship a
router-ssr-query-corerelease coordinated with thereserveStreamFastPathchange.
Happy to provide a minimal repro repo if useful.
schiller-manuel(GitHub profile, opens in a new tab) added labelsinformation needed
Reproducer, as requested: https://github.com/radosek/tanstack-7529-repro
Live demo (Cloudflare Workers, hangs on every request): https://tanstack-7529-repro.radosek.workers.dev/ β the page streams its shell, then the tab spins forever; the document request never completes and the page never hydrates.
curl -s -m 20 https://tanstack-7529-repro.radosek.workers.dev/ -o out.html -w "bytes=%{size_download} time=%{time_total}s\n" # bytes=2471 time=20.0s (timeout) β body ends mid-stream after the dehydration script, no </html>Same app code, only the version changes:
@tanstack/react-router@tanstack/router-coreresult 1.169.01.169.0completes in ~3 ms 1.170.15(latest)1.171.13(latest)hangs until the ~60 s serialization timeout @tanstack/react-router-ssr-query@1.167.1(latest) in both cases. The entry server is the body ofv1.169.0's ownrenderRouterToStream(renderToReadableStreambranch) inlined verbatim βtransformReadableStreamWithRouter(router, stream)+new Response(responseStream, β¦).Corrected root cause#
Instrumenting
router-core@1.171.13shows the listener is not refused at registration β thestreamFastPathReservedarm I originally blamed never fires. The actual sequence on every hanging request:1. onRenderFinished registered OK (listeners=1) β ssr-query's close listener, during dehydrate() 2. reserveStreamFastPath returns false β transform takes the main stream path 3. cleanup() wipes renderFinishedListeners=1, β createRequestHandler's `finally`, serializationFinishedListeners=1 while the body is STILL STREAMING 4. ssr-query teardown closes the query stream β via onCleanup 5. setRenderFinished finds listeners=0 β close listener gone, completion signals destroyedcreateRequestHandlerchanged semantics between the two versions. Inv1.169.0, once the callback was invoked the handler never cleaned up β the stream owned the lifecycle, and a plainResponsewas safe:// v1.169.0 cbWillCleanup = true return cb({ request, router, responseHeaders }) } finally { if (!cbWillCleanup) router.serverSsr?.cleanup() }In
v1.171.x, cleanup is deferred only when the callback's return value is wrapped bycreateSsrStreamResponseβ an API that does not exist inv1.169.0β so the same code now triggersserverSsr.cleanup()while the body is still streaming. That wipes the render-finished and serialization-finished listeners the transform waits on, and the response never closes.I've updated #7591 accordingly: the transform now claims cleanup ownership when it attaches, and
createRequestHandlerskips its fallback cleanup when claimed (the transformed stream already cleans up when consumed/cancelled/errored/expired). A regression test reproduces the production path and fails without the fix; the reproduction app patched with the fix completes in ~3 ms on the broken version combo.@schiller-manuel
are you using just router or start in your actual application?
Just router β no Start. The app is a custom Vite SSR setup on Cloudflare Workers (
@cloudflare/vite-plugin):@tanstack/react-router+@tanstack/react-router-ssr-query+@tanstack/react-query, with a custom server entry doingcreateRequestHandlerβrenderToReadableStreamβtransformReadableStreamWithRouterβnew Response(stream)β same shape as the reproducer (which is that entry inlined fromrenderRouterToStream's v1.169.0 body).why not use renderRouterToStream then? i am trying to understand what went wrong here.
We inline
renderRouterToStream's body in our entry instead of calling it, solely to addonErrorβ stock doesn't expose it, and without it SSR render exceptions produce a 500 with zero log output. If it accepted anonErroroption we'd call it directly.Probably better PR would be to add
onErroroption torenderRouterToStream:DThis also happens with Start.
Not a minimum reproducer, but it did happen with my app at https://github.com/hokkyss/Portfolio/tree/dev/apps/public-tanstack-start
@hokkyss a minimal start repro would be helpful
Let me try to boot up reproducer from the template
Sorry, I couldn't replicate it yet, but high chance it is related with router/start version.
My app is no longer stuck after reverting to the old version in this commit https://github.com/hokkyss/Portfolio/commit/d68c946337358f2470d9d460387a708080c3c871.
This is the worker URL right before the commit
https://hokkyss-portfolio-tanstack-start-development.hokkyss2.workers.dev
radosek(GitHub profile, opens in a new tab) linked this to #7591
Sign in to join this conversation.
please provide a complete minimal reproducer project