Skip to content
Source
Sign inSign up
TanStack/router

Since start-server-core 1.169.36, middleware that copies the page Response drops the connection when `Response` is wrapped #8627

Send to my agent
Closedaavora-bf(GitHub profile, opens in a new tab) opened this issue 2 comments

Hi, Ben here.

Following up my comment https://github.com/TanStack/router/pull/7588#issuecomment-5993036431 as requested with a minimal repo of the issue. The following is from my AI which I've tried to tidy up some of the wording.

Which project does this relate to?

TanStack Start

Describe the bug

Found while debugging a production issue. This follows up https://github.com/TanStack/router/pull/7588#issuecomment-5993036431.

Since @tanstack/start-server-core 1.169.36 and #8204, a request middleware that returns a copy of the page response can kill the SSR stream. The server closes the socket before it writes a status line, and Start logs nothing.

The copy is new Response(result.response.body, ...), which is a common way to add a header. Natively new Response(stream).body === stream, so Start sees the copy as a continuation and the page streams fine. Wrap the global Response in something that pipes stream bodies and that identity breaks. Start then sees a replacement and disposes the page's SSR stream. The copy's body reads from that stream, so the read fails and srvx destroys the socket.

We hit this with two libraries together. WorkOS AuthKit's middleware copies the response whenever it sets a cookie, such as after a session refresh. varlock's leak prevention patches Response and pipes stream bodies through a scanner. Every page load failed once the access token expired. Behind an AWS ALB it showed up as a 502 with nothing in the app logs.

transferResponseBodyOwnership avoids it, but a middleware author has no way to find that out. The docs don't mention it. The #8204 changeset doesn't say that copying a response in middleware now needs it. And the only symptom is a dropped connection.

Complete minimal reproducer

https://github.com/aavora-bf/tanstack-start-response-copy-repro

Steps to Reproduce the Bug

  1. pnpm install
  2. pnpm repro. It builds once, serves the production build with nitro's node-server preset under four configurations, and curls / for each.
  3. Expect this output:
@tanstack/start-server-core 1.169.39

1. copy in middleware                                      HTTP 200
2. patched Response, no copy                               HTTP 200
3. patched Response + copy in middleware                   FAILED (curl exit 52: (52) Empty reply from server)
4. patched Response + copy + transferOwnership             HTTP 200

The copying middleware is in src/start.ts. The Response wrapper is a 10-line Proxy in src/server.ts. Env flags switch each one on.

Dev mode fails too. Run REPRO_PATCH_RESPONSE=1 REPRO_COPY=1 pnpm dev, then curl -i localhost:3000/, and Vite answers 500 with socket hang up.

It's a regression. @tanstack/react-start 1.168.54, which pulls in start-server-core 1.169.35, returns 200 for case 3. 1.168.55, with start-server-core 1.169.36, fails.

Expected behavior

Case 3 serves the page, as it did in 1.169.35. If Start can't tell a copy from a replacement, I'd expect a logged error or a dev warning that names transferResponseBodyOwnership. A dropped connection with no log is very hard to trace.

Platform

  • Router / Start Version: @tanstack/react-start 1.168.60, @tanstack/react-router 1.170.41, @tanstack/start-server-core 1.169.39
  • OS: Linux on WSL2, Node 24.13.1. Production runs node:24-slim behind an AWS ALB.
  • Browser: n/a, reproduced with curl
  • Browser Version: n/a
  • Bundler: vite, nitro 3.0.260903-beta
  • Bundler Version: 8.3.3

Additional context

The break is in createStartHandler. The copy fails inheritsResponseOwnership, so disposeResponseOwnership(current, "middleware response replaced") disposes the SSR stream response. The router's SSR cleanup then errors the transform with "SSR stream transform aborted by router SSR cleanup". srvx's node adapter destroys the socket when its body read rejects. Server functions and server routes are fine, because they have no SSR stream to dispose.

Suggestions:

  1. Warn when "middleware response replaced" disposes a live SSR stream, at least in dev, and name transferResponseBodyOwnership in the message.
  2. Document transferResponseBodyOwnership for middleware authors.

One more thing about #7588, which I read at fc9f148 but haven't run. Start's own createReconciledResponse rebuilds the page with new Response(response.body, ...) when a response helper changes the status or headers. adoptRebuilt registers that rebuild only against an existing source, so it also falls back to body identity. With a wrapped Response, a page whose loader calls setResponseHeader or setCookie would dispose its own stream, and no middleware could work around it. Calling responseBodySources.set(rebuilt, exposed) whenever the rebuild has a body would stop that.

  1. thanks for creating this issue. will look into this. about new Response(result.response.body, ...): you can just modify a Response's headers (unless those are frozen when the Response comes from e.g. fetch) this is usually the faster and memory-wise cheaper way.

  2. I patched it with this to temp resolve it. My issue was due to WorkOS session refresh could cancel the active SSR stream when Varlock wrapped the response body, returning truncated HTML.

    diff --git a/dist/server/middleware-body.js b/dist/server/middleware-body.js
    index 2be1567a8f740266bae0d38c0f8e04c6b0d1d5e2..e39ceef19c12fb89acbd1ea3c1018409bcaa213e 100644
    --- a/dist/server/middleware-body.js
    +++ b/dist/server/middleware-body.js
    @@ -1,4 +1,5 @@
     import { getAuthkit, validateConfig, getConfig } from './authkit-loader.js';
    +import { transferResponseBodyOwnership } from '@tanstack/react-start/server';
     let configValidated = false;
     // eslint-disable-next-line @typescript-eslint/no-explicit-any
     export async function middlewareBody(args, options) {
    @@ -39,6 +40,9 @@ export async function middlewareBody(args, options) {
             statusText: result.response.statusText,
             headers: result.response.headers,
         });
    +    if (result.response.body && newResponse.body) {
    +        transferResponseBodyOwnership(result.response, newResponse);
    +    }
         for (const [key, value] of headerEntries) {
             if (key.toLowerCase() === 'set-cookie') {
                 newResponse.headers.append(key, value);
    ``

Sign in to join this conversation.