Repository navigation
[email protected]+: SSR query stream never closes with ssr-query → "Serialization timeout after app render finished" #7529
Description
Bug
[email protected]+ 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 [email protected]) 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/[email protected] 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/[email protected](→[email protected]–1.171.9) +@tanstack/[email protected](→[email protected]) - Works:
@tanstack/[email protected](→[email protected]), or[email protected](pre-fix: fix streaming #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 [email protected]+ 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.
Activity
fix(router-core): don't drop onRenderFinished listeners after fast pa…
fix(router-core): don't drop onRenderFinished listeners after fast pa…
schiller-manuel commentedon Jun 9, 2026
please provide a complete minimal reproducer project
fix(router-core): don't tear down SSR state while the response body i…
radosek commentedon Jun 12, 2026
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.
Same app code, only the version changes:
@tanstack/react-router@tanstack/router-core1.169.01.169.01.170.15(latest)1.171.13(latest)@tanstack/[email protected](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
[email protected]shows the listener is not refused at registration — thestreamFastPathReservedarm I originally blamed never fires. The actual sequence on every hanging request:createRequestHandlerchanged 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: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
schiller-manuel commentedon Jun 12, 2026
are you using just router or start in your actual application?
radosek commentedon Jun 12, 2026
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).schiller-manuel commentedon Jun 12, 2026
why not use renderRouterToStream then? i am trying to understand what went wrong here.
radosek commentedon Jun 12, 2026
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.radosek commentedon Jun 12, 2026
Probably better PR would be to add
onErroroption torenderRouterToStream:Dhokkyss commentedon Jun 21, 2026
This 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
schiller-manuel commentedon Jun 21, 2026
@hokkyss a minimal start repro would be helpful
hokkyss commentedon Jun 21, 2026
Let me try to boot up reproducer from the template
hokkyss commentedon Jun 21, 2026
Still Failed Repro
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 hokkyss/Portfolio@d68c946.
This is the worker URL right before the commit
https://hokkyss-portfolio-tanstack-start-development.hokkyss2.workers.dev
chore: bump deps
chore: bump deps
chore: bump deps