[start] Duplicate Set-Cookie headers dropped on Linux (Bun) for 2xx Response returns #7189
Send to my agentDescribe the bug#
When a TanStack Start API route returns a Response that contains two Set-Cookie headers, only one of them reaches the client on Linux + Bun. The same code works correctly on macOS + Bun (same Bun version, same package versions).
Observed while wiring BetterAuth into a TanStack Start app: BetterAuth sets better-auth.session_token and better-auth.session_data on a successful sign-in. On GitHub Actions (ubuntu-latest, Bun 1.3.9) only session_data survives to the browser. On macOS (Bun 1.3.9) both survive.
Your Example Website or App#
Minimal reproduction route (drops an extra route into any TanStack Start app scaffold):
// src/routes/api/cookies.ts
import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/api/cookies")({
server: {
handlers: {
POST: async () => {
const headers = new Headers();
headers.append("set-cookie", "a=1; Path=/; HttpOnly; SameSite=Lax");
headers.append("set-cookie", "b=2; Path=/; HttpOnly; SameSite=Lax");
return new Response(JSON.stringify({ ok: true }), { status: 200, headers });
},
},
},
});
Hit it from both environments:
curl -i -X POST http://localhost:3001/api/cookies
I can stand up a full minimal-repro repo if it would help — just wanted to file the observation first.
Steps to Reproduce the Bug or Issue#
- Scaffold a TanStack Start app (React).
- Add the route above.
- Run
bun run devon macOS. Curl the route — the response carries twoSet-Cookieheaders. - Run the same code on a GitHub Actions
ubuntu-latestrunner (or any Linux + Bun 1.3.9). Curl the route — the response carries oneSet-Cookieheader (b=2). Cookieais silently dropped.
Expected behavior#
Both Set-Cookie values should reach the client, regardless of OS/runtime. This is critical for any auth library that sets both an opaque session token and a signed session-data cache in the same response (BetterAuth is one such case; see better-auth/better-auth).
Additional context#
- Affected versions (from
bun.lock):@tanstack/react-start@1.167.34,@tanstack/react-start-server@1.166.37,@tanstack/start-server-core@1.167.18,h3@2.0.1-rc.20,bun@1.3.9,vite@8.0.8. - I verified via a debug response header (
x-auth-debug-cookies) set from inside the route handler thatresponse.headers.getSetCookie().length === 2at the moment the handler returns — so the drop happens after the route handler returns, somewhere between TanStack Start'sprepareResponse/h3_toResponseand the wire. - Explicitly rebuilding the
Responsewithheaders.getSetCookie()+headers.append("set-cookie", …)in the route handler does not fix it on Linux. Both values are in the returnedHeadersobject; one still gets dropped. - Removing all h3 event-header writers (i.e. not using anything that calls
setCookiefrom@tanstack/react-start/server) also does not fix it on Linux, so theprepareResponse→mergeHeaders$1path that mergespreparedHeadersintoval.headersis not the culprit on its own. - The suspect lines I looked at:
@tanstack/start-server-core/src/request-response.ts—mergeEventResponseHeadersearly-returns onresponse.ok, andmergeHeaders$1inh3@2.0.1-rc.20/dist/h3-*.mjsusestarget.set(name, value)for non–set-cookieheaders which would preserve duplicates, but I did not prove this is where the drop happens. - Workaround used in our codebase: disable BetterAuth's
cookieCacheso only a singlesession_tokencookie is emitted. Confirms the drop is specific to duplicateSet-Cookieentries, not specific to any cookie name or value.
Happy to iterate on a minimal-repro repo or capture a trace if that speeds up triage.
Here's a standalone, runnable reproduction with a matrix CI that shows the platform split:
https://github.com/awinogradov/reproduce-tanstack-router-cookie-issue
What it does#
src/routes/api/cookies.ts— a single POST handler that builds a freshHeadersobject, appends twoSet-Cookievalues (a=1; …andb=2; …), and returns a 200Response. It also sets anx-debug-cookiesheader withheaders.getSetCookie().lengthat the moment of return, so you can see what the handler emitted vs what the client received.scripts/repro.ts— boots the TanStack Start dev server viabun run dev, waits forhttp://localhost:3001/to respond, POSTs to/api/cookies, and exits0ifresponse.headers.getSetCookie().length === 2or1otherwise..github/workflows/repro.yml— runs the script on bothubuntu-latestandmacos-latestGitHub runners with Bun 1.3.9.
First CI run#
First push, no tweaks: run #24428746631 —
macos-latest✅,ubuntu-latest❌, 17s total.ubuntu-latest output:
[repro] status: 200 [repro] x-debug-cookies (set by the handler): 2 [repro] client-visible set-cookie count: 1 [repro] [0] b=2; Path=/; HttpOnly; SameSite=Lax [repro] FAIL — handler set 2 cookies, client saw 1 on linux/x64 bun=1.3.9macos-latest output:
[repro] status: 200 [repro] x-debug-cookies (set by the handler): 2 [repro] client-visible set-cookie count: 2 [repro] [0] a=1; Path=/; HttpOnly; SameSite=Lax [repro] [1] b=2; Path=/; HttpOnly; SameSite=Lax [repro] PASS — both cookies reached the client on darwin/arm64 bun=1.3.9Identical source, identical dependency tree (
bun.lockcommitted), identical Bun version (oven-sh/setup-bun@v2pinned to1.3.9) — the only difference between the two jobs is the OS. On Linux, the firstSet-Cookie(a=1) is silently dropped; only the second (b=2) reaches the client.Note the debug header: the handler consistently reports 2 cookies at return time, so the drop happens after the route handler returns, somewhere in the TanStack Start → h3 → Bun HTTP response chain.
Happy to iterate on the repro if it's missing anything.
One more clarification since it came up reading the repro — we're not running Node.js anywhere in this reproduction, neither in dev nor in CI:
- The dev server runs via
bun run dev→vite devexecuted by the Bun runtime. - The reproduction script runs via
bun ./scripts/repro.ts. bun install --no-savesets up the dependency tree.- The GitHub Actions matrix uses
oven-sh/setup-bun@v2and never installs Node. - Downstream in the project where we first hit this (acclaim-ai/frontend#21), the production image is
oven/bun:1-alpineand the runtime command isbun dist/server/server.js— same story.
So whatever is dropping the second
Set-Cookieis isolated to the TanStack Start → h3 → Bun HTTP response path on Linux. Node'shttpmodule /res.setHeaderis not in play, which should help narrow the search.- The dev server runs via
@schiller-manuel, is it helpful enough? If you need anything else, let me know 🙏🏼
run it with node then. want to rule out a bun issue here
@schiller-manuel I don't see the reason.
So whatever is dropping the second Set-Cookie is isolated to the TanStack Start → h3 → Bun HTTP response path on Linux. Node's http module / res.setHeader is not in play, which should help narrow the search.
But if you need help with it, I’m here.
can you just execute the same test on linux on node instead of using bun?
Sorry for the noise on this, @schiller-manuel — I went further on the reproduction and it turns out this is not a TanStack Router / Start issue. You can safely deprioritise or close #7189; I owe you the time you already spent.
What changed since the first comment#
I added a
nodejsbranch to the reproduction repo that runs the exact same TanStack Start app, the same Vite dev server, the same route handler, the sameheaders.getSetCookie()rebuild — just under Node.js LTS viaactions/setup-node@v4instead ofoven-sh/setup-bun@v2. Both branches ship a matrix workflow that runs onubuntu-latest+macos-latest, and both scripts dump the response in three ways: thex-debug-cookiescustom header (what the handler emitted),response.headers.getSetCookie()(whatfetchparses), and a raw TCP socket read (what's actually on the wire).Repro: https://github.com/awinogradov/reproduce-tanstack-router-cookie-issue —
mainbranch is Bun,nodejsbranch is Node LTS.Test matrix#
Runtime Runner handler emitted raw TCP wire fetch().getSetCookie()result Bun 1.3.9 macos-latest 2 2 2 ✅ PASS Bun 1.3.9 ubuntu-latest 2 1 ( b=2only)1 ( b=2only)❌ FAIL Node LTS (24.x) macos-latest 2 2 2 ✅ PASS Node LTS (24.x) ubuntu-latest 2 2 2 ✅ PASS Run links:
- Bun ubuntu fail — https://github.com/awinogradov/reproduce-tanstack-router-cookie-issue/actions/runs/24430602277
- Node ubuntu pass — https://github.com/awinogradov/reproduce-tanstack-router-cookie-issue/actions/runs/24430979246
Identical source, identical Vite dev server, identical TanStack Start version tree, identical GitHub runner image. The only axis that flips the result is the runtime.
What the raw socket pinned down#
When we drop into a plain TCP socket and read the bytes directly off the wire on
ubuntu-latest+ Bun, the secondSet-Cookieis already gone beforefetch()ever touches the response. Only the last-appended value (b=2) survives. Every other custom header on the same response (x-debug-cookies,Access-Control-Allow-Origin,Vary, lowercasecontent-type) passes through unchanged — the drop is specificallySet-Cookie, specifically on duplicate keys. That matches anres.setHeaderoverwrite pattern, notres.appendHeader.Under Node LTS on the same ubuntu-latest image, the same TanStack Start / Vite / srvx
sendNodeResponsepath writes bothSet-Cookielines to the wire correctly. The first link in the chain that changes the answer isnodeRes.writeHead(status, statusText, flatArray)insidesrvx/adapters/node.mjs— srvx spreads[...webRes.headers]and.flat()s it into["set-cookie", "a=1", "set-cookie", "b=2", ...]. Node'shttp.ServerResponse.writeHeadpreserves duplicate keys in that flat form; Bun'snode:httpcompat layer on Linux x86_64 does not.Conclusion#
This is a Bun
node:httpcompat bug, not a TanStack Router bug. Neither theprepareResponsemerge path in@tanstack/start-server-core, nor themergeHeaders$1in h3'stoResponse, nor thetanstackStartCookiesplugin in better-auth are involved — all three were ruled out earlier by isolating/removing them. TanStack Router is the messenger here via srvx'ssendNodeResponse.I'll file a minimised, framework-free reproduction upstream against oven-sh/bun (bare
node:httpserver +writeHeadwith a flat array containing twoset-cookiepairs) and link it back here when it's open.Sorry again for taking your time on a false trail. Feel free to close this issue whenever you're comfortable.
Sign in to join this conversation.
please provide a complete minimal reproducer project. does this also fail with node?