Skip to content

refactor(server): rewrite resolveResponse as a pipeline with errors as values - #7894

Draft
KATT wants to merge 7 commits into
mainfrom
cursor/refactor-resolve-response-2443
Draft

KATT wants to merge 7 commits into
mainfrom
cursor/refactor-resolve-response-2443

Conversation

@KATT

@KATT KATT commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

🤖 Draft for discussion, not for merge.

Why

  • resolveResponse was one 850-line function: one try/catch, error handling copied four times, stream cleanup wired by hand.

What

  • A pipeline: parse, accept, create context, call, then build the JSON, JSONL or SSE response. Steps return Result values; settle() holds the only try/catch.
  • Stream bodies are async generators. Abort listeners and the maxDurationMs timer are using resources. No polyfill or lib change.
  • Fixes: onError runs once when a subscription fails before streaming. A stream over httpLink or httpBatchLink fails only its call, keeping path. A single call's error gets its procedure's type. responseMeta runs at most once.
  • If the response fails after responseMeta, the 500 drops responseMeta's and the stream's headers, keeping vary, allow and createContext headers.
  • Public API:
// @trpc/server/unstable-core-do-not-import
- getRequestInfo(opts): Promise<TRPCRequestInfo> // throws
+ getRequestInfo(opts): Promise<Result<TRPCRequestInfo, TRPCError>>

// resolveResponse: new optional option; the fetch adapter passes its resHeaders
+ resHeaders?: Headers

Links

None

Alternatives considered

  • Generators for the JSON paths: Promise.all reads more clearly.
  • DisposableStack: missing on Node 18 and 20, and the build doesn't lower it.

Testing

Each fix's test in resolveResponse.regressions.test.ts fails on the commit before it; getRequestInfo's change is enforced by typecheck.

AI model

Claude Opus 5.5

Open in Web Open in Cursor 

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@vercel

vercel Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
examples-next-minimal-starter Ready Ready Preview Nov 5, 2026 12:55am UTC
examples-todomvc Ready Ready Preview Nov 5, 2026 12:55am UTC
next-prisma-starter Ready Ready Preview Nov 5, 2026 12:55am UTC
next-prisma-websockets-starter Ready Ready Preview Nov 5, 2026 12:55am UTC
tanstack-start-sse-chat Ready Ready Preview Nov 5, 2026 12:55am UTC
trpc-next-app-dir Ready Ready Preview Nov 5, 2026 12:55am UTC
www Ready Ready Preview Nov 5, 2026 12:55am UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@pkg-pr-new

pkg-pr-new Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/@trpc/client@7894

@trpc/next

npm i https://pkg.pr.new/@trpc/next@7894

@trpc/openapi

npm i https://pkg.pr.new/@trpc/openapi@7894

@trpc/react-query

npm i https://pkg.pr.new/@trpc/react-query@7894

@trpc/server

npm i https://pkg.pr.new/@trpc/server@7894

@trpc/tanstack-react-query

npm i https://pkg.pr.new/@trpc/tanstack-react-query@7894

@trpc/upgrade

npm i https://pkg.pr.new/@trpc/upgrade@7894

commit: 8891071

@cursor
cursor Bot force-pushed the cursor/refactor-resolve-response-2443 branch from 30951ec to 29365d3 Compare November 4, 2026 23:30
@cursor
cursor Bot force-pushed the cursor/refactor-resolve-response-2443 branch from 29365d3 to 8891071 Compare November 5, 2026 00:53
meefs pushed a commit to meefs/trpc that referenced this pull request Nov 5, 2026
…s or errorFormatter throws (trpc#7931)

<!-- CURSOR_AGENT_PR_BODY_BEGIN -->
🤖

## Why

- With the standalone, Express and Next.js adapters, a streamed body
that fails after its headers were sent made `internal_exceptionHandler`
write a second response. Node then threw an uncaught `write after end`,
and the process exited. One cause is a JSONL value that `JSON.stringify`
can't handle, such as a `bigint`.
- A throwing `errorFormatter` made the same handler throw. The request
got no response, and the rejection was unhandled.

## What

- `internal_exceptionHandler` only ends a response whose headers were
already sent.
- If the error formatter throws, it answers with a minimal
`INTERNAL_SERVER_ERROR` that reveals nothing about the error, like the
ws adapter since trpc#7915.

## Links

None

## Alternatives considered

- Make a JSONL value that can't be serialized fail only its own call.
That's a bigger change to the stream producer, so it goes into the
`resolveResponse` rewrite (trpc#7894) instead.

## Testing

Regression check: both tests in `nodeHTTPExceptionHandler.test.ts` fail
on `main`. The first records the uncaught `write after end`. The second
never gets a response.

## AI model

Claude Opus 5.5
<!-- CURSOR_AGENT_PR_BODY_END -->

<div><a
href="https://cursor.com/agents/bc-2b80ea20-543b-539a-ac42-4d10f7dc2443?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source
media="(prefers-color-scheme: light)"
srcset="https://cursor.com/assets/images/open-in-web-light.png"><img
alt="Open in Web" width="114" height="28"
src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a>&nbsp;<a
href="https://cursor.com/background-agent?bcId=bc-2b80ea20-543b-539a-ac42-4d10f7dc2443&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source
media="(prefers-color-scheme: light)"
srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img
alt="Open in Cursor" width="131" height="28"
src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a>&nbsp;</div>

<!-- codesmith:footer -->
---
<a
href="https://backend.blacksmith.sh/track/enable-autofix?expires=1796427726&installation_model_id=443568&pr_number=7931&ref=codesmith_pr_footer&repository=trpc%2Ftrpc&return_to=https%3A%2F%2Fgithub.com%2Ftrpc%2Ftrpc%2Fpull%2F7931&signature=9083b9aa072e878edd679cb7f25b81100e7198516eb090ce2d67ba26595082da"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-light.svg"><img
alt="Autofix with [code]smith"
src="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark-3x.png"
width="190" height="28"></picture></a>
<sup><a
href="https://app.blacksmith.sh/trpc/codesmith/trpc/pr/7931?autoLogin=true&ref=codesmith_pr_footer">Open
in [code]smith</a> · Need help on this PR? Tag
<code>@codesmith-bot</code> with what you need.</sup>

<!-- codesmith:autofix:disabled -->
<!-- /codesmith:footer -->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved error handling when generating error responses fails,
avoiding uncaught errors and duplicate responses.
* If error formatting fails, the request now receives a safe internal
server error response with HTTP status 500.
* Streamed responses that fail during serialization are handled safely,
including when response headers have already been sent.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Cursor Agent <[email protected]>
meefs pushed a commit to meefs/trpc that referenced this pull request Nov 17, 2026
…disconnects (trpc#8062)

## Why

- When a client disconnects mid-body, the node-http adapters hang: the
procedure never runs, `onError` never fires, and the body promise leaks.
The body `ReadableStream` only listened to the source stream's
`data`/`end`, so an aborted or failed source stream never ended it, and
`await req.text()` in the JSON content type waited forever.

## What

- `createBody()` now also listens to the source stream's `error` and
`close` events, and ends the `ReadableStream` with a
`CLIENT_CLOSED_REQUEST` error when the stream goes away before a clean
`end`.
- Clean `end` still closes the stream as before; `close` after `end` is
ignored via `readableEnded`.
- This is the request-body layer only, orthogonal to the
response-pipeline rewrites in trpc#7894 and trpc#7934.

## Links

- None

## Alternatives considered

- Every consumer racing `req.text()` against the request's abort signal
— lost: the stream knows when it dies; ending it once at the source
fixes every reader.

## Testing

- Regression check: `packages/tests/server/adapters/standalone.test.ts >
calls onError when the client disconnects mid-body` fails on main (waits
1.5s, `onError` never fires); passes with this change.
-
`packages/server/src/adapters/node-http/incomingMessageToRequest.test.ts
> rejects the body when the client disconnects mid-body` also fails
without the fix.

## AI model

glm-5.3

<!-- codesmith:footer -->
---
<a
href="https://backend.blacksmith.sh/track/enable-autofix?expires=1797402378&installation_model_id=443568&pr_number=8062&ref=codesmith_pr_footer&repository=trpc%2Ftrpc&return_to=https%3A%2F%2Fgithub.com%2Ftrpc%2Ftrpc%2Fpull%2F8062&signature=31f1dd129d5234cf4ac42309680374db1d04a57b0bb61454adb6de2647121f86"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-light.svg"><img
alt="Autofix with [code]smith"
src="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark-3x.png"
width="190" height="28"></picture></a>
<sup><a
href="https://app.blacksmith.sh/trpc/codesmith/trpc/pr/8062?autoLogin=true&ref=codesmith_pr_footer">Open
in [code]smith</a> · Need help on this PR? Tag
<code>@codesmith-bot</code> with what you need.</sup>

<!-- codesmith:autofix:disabled -->
<!-- /codesmith:footer -->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Requests interrupted while the client is sending a body now fail
promptly with a clear error instead of potentially hanging.
* Interrupted requests are reported through the server’s error handling,
and the affected procedure does not run.
* Request streams are cleaned up after completion, errors, or premature
disconnects, helping interrupted requests close reliably.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: pengzh1 <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Alexander Johansson <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>

This branch was successfully deployed

5 active deployments
Preview – www — 88910718 Deployed Nov 5, 2026 by vercel[bot]
Preview – trpc-next-app-dir — 88910718 Deployed Nov 5, 2026 by vercel[bot]
Preview – examples-todomvc — 88910718 Deployed Nov 5, 2026 by vercel[bot]
Preview – next-prisma-starter — 88910718 Deployed Nov 5, 2026 by vercel[bot]
Preview – examples-next-minimal-starter — 88910718 Deployed Nov 5, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants