Skip to content

fix(server): don't abort request signal after normal response completion - #7665

Closed
pintu544 wants to merge 1 commit into
trpc:mainfrom
pintu544:fix/issue-7663-node-http-abort-signal
Closed

pintu544 wants to merge 1 commit into
trpc:mainfrom
pintu544:fix/issue-7663-node-http-abort-signal

Conversation

@pintu544

@pintu544 pintu544 commented Oct 16, 2026 •

Copy link
Copy Markdown

Closes #7663

🎯 Changes

Bug fix in the node-http adapter (, shared by the standalone, Express, and Fastify adapters).

Problem: the adapter aborted the request's AbortController on every response 'close' event β€” and Node emits 'close' after every response, including ones that completed normally. As a result, opts.signal reported aborted: true for 100% of successful requests, so procedure code could not distinguish "client went away" from "request completed". Each spurious abort also constructs a DOMException with a captured stack trace (measured ~2.7% CPU on a trivial query under load in the issue).

Root cause: res.once('close', onAbort) β†’ unconditional ac.abort() in packages/server/src/adapters/node-http/incomingMessageToRequest.ts.

Solution: only abort when the response did not finish (!res.writableFinished). A mid-request client disconnect (or a cut-off stream) closes the response before res.end(), so writableFinished is false and the abort still fires exactly when the client actually went away. I verified this discriminator empirically against real http.ServerResponse objects: normal completion β†’ writableFinished: true at 'close'; mid-stream abort β†’ false.

Tests

New regression tests in packages/server/src/adapters/node-http/incomingMessageToRequest.test.ts (vitest, existing helpers):

  • writableFinished: true + 'close' β†’ signal.aborted === false (fails before the fix)
  • writableFinished: false + 'close' β†’ signal.aborted === true (pins disconnect behavior)
  • integration via createServer: 5 completed requests β†’ 0 aborted signals (fails before the fix: 5/5 aborted)

Results:

  • incomingMessageToRequest.test.ts: 15/15 pass
  • adapter integration (standalone, express, fastify, standalone.http2): all pass except standalone > custom host, which fails identically on the pristine tree (binds to TEST-NET IP 198.19.0.2, unreachable in this sandbox β€” pre-existing environment failure, unrelated)
  • tsc --noEmit on @trpc/server: clean; ESLint + Prettier: clean

Limitations

  • The fix relies on writableFinished, which is set once res.end() has been called and the stream finished. A response whose socket is destroyed after end() but before the flush completes will still abort β€” arguably correct, since the client never received the complete response.

βœ… Checklist

  • I have followed the steps listed in the Contributing guide.
  • I have added or updated the tests related to the changes made.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed request cancellation handling when a response closes. Requests that finish normally are no longer incorrectly marked as aborted, while requests whose responses close before completion are still cancelled. This helps ensure that request status accurately reflects whether the response completed or was interrupted, including when several completed requests are handled in sequence.

The node-http adapter aborted the request's AbortController on every
response 'close' event, including responses that completed normally.
This made opts.signal report aborted for 100% of successful requests
and wasted CPU constructing DOMExceptions under load.

Only abort when the response did not finish (res.writableFinished),
which preserves the client-disconnect behavior while leaving
successfully completed requests unaborted.

Closes trpc#7663
@pintu544
pintu544 requested a review from a team as a code owner October 16, 2026 06:03
@vercel

vercel Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
next-prisma-starter Ready Ready Preview Oct 16, 2026 6:04am UTC
og-image Ready Ready Preview Oct 16, 2026 6:04am UTC

Request Review

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack β†’

No actionable comments were generated in the recent review. πŸŽ‰

ℹ️ Recent review info
βš™οΈ Run configuration
  • Configuration used: Repository: trpc/trpc/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5645c839-b288-437d-ab53-8e112c99c099
πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 10e2ef9 and f36ba60.

πŸ“’ Files selected for processing: 2
  • packages/server/src/adapters/node-http/incomingMessageToRequest.test.ts
  • packages/server/src/adapters/node-http/incomingMessageToRequest.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

πŸ“ Walkthrough

The node HTTP adapter now aborts a request signal on response closure only if the response has not finished. Tests cover both response states and verify that completed requests leave their signals unaborted.

Changes: Request signal abort behavior

Layer / File(s) Summary
Guard aborts on response completion
packages/server/src/adapters/node-http/incomingMessageToRequest.ts, packages/server/src/adapters/node-http/incomingMessageToRequest.test.ts
The close handler checks res.writableFinished before aborting. Tests cover finished and unfinished responses and verify that five completed requests do not abort their signals.

Priority: βž– Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix Β· Severity of issue fixed: Medium

Suggested reviewers: katt

Merge Risk: βšͺ Minimal Β· up to f36ba

Normally completed responses leave the request signal unaborted, while unfinished responses still abort it. The inspected server paths support this behavior, and no actionable merge blocker remains.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary fix: preventing request-signal abortion after normal response completion.
Description check Passed The description explains the problem, root cause, solution, tests, validation results, limitation, linked issue, and checklist status. It is complete enough to assess the change, although it does not …
Linked Issues check Passed Issue #7663 requires the shared node-http adapter to keep the request signal active after normal response completion and to abort it when the response closes before finishing. The change in `incomingM…
Out of Scope Changes check Passed The pull request changes only the node-http adapter implementation and its focused regression tests. The comments identify issue #7663. The tests directly verify the required normal-completion and dis…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.
✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • Create a new PR
  • Autofix Β· Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

πŸ”§ ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/server/src/adapters/node-http/incomingMessageToRequest.test.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

packages/server/src/adapters/node-http/incomingMessageToRequest.ts

ESLint skipped: the matched ESLint configuration already failed (missing-dependency).

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

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Oct 16, 2026

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

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

@trpc/next

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

@trpc/openapi

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

@trpc/react-query

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

@trpc/server

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

@trpc/tanstack-react-query

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

@trpc/upgrade

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

commit: f36ba60

KATT added a commit to noahisdai/trpc that referenced this pull request Oct 19, 2026
…ends (trpc#7666)

Related to trpc#7663

## 🎯 Changes

With `sse.maxDurationMs` set, `resolveResponse` starts a timer for each
subscription and only clears it when the request signal aborts. If the
signal never aborts, the timer stays pending for the full
`maxDurationMs` and keeps its closure and the event loop alive. That
happens when:

- the generator returns
- the procedure throws before streaming (e.g. in a middleware)
- the response body is cancelled
- `responseMeta` throws while the SSE response is being built
- the SSE stream errors (e.g. `onError` throws while an error is being
formatted)

On `main` the fetch adapter already hits this whenever `request.signal`
isn't aborted after a completed response. The node-http adapters will
hit it too once trpc#7663 is fixed, because trpc#7664 and trpc#7665 stop aborting
the signal after a normal response.

This PR clears the timer in all five cases. `fetch.test.ts` covers each
one with a request whose signal never aborts, and all five fail on
`main`.

## βœ… Checklist

- [x] I have followed the steps listed in the [Contributing
guide](https://github.com/trpc/trpc/blob/main/CONTRIBUTING.md).
- [ ] If necessary, I have added documentation related to the changes
made.
- [x] I have added or updated the tests related to the changes made.


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

* **Bug Fixes**
* SSE subscriptions now clear their maximum-duration timers when streams
complete, are canceled, or encounter errorsβ€”including failures during
procedure execution or response metadata setup. Stream completion also
removes its abort listener, helping prevent resources from lingering
after a subscription ends.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Alexander Johansson <[email protected]>
Co-authored-by: Cursor <[email protected]>
@pintu544

Copy link
Copy Markdown
Author

Closing as superseded β€” #7664 landed the fix for this first. Congrats to mrityunjay-pandey on the merge.

@pintu544 pintu544 closed this Oct 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 21, 2026

This branch was successfully deployed

5 active deployments
Preview – www β€” f36ba607 Deployed Oct 16, 2026 by vercel[bot]
Preview – examples-todomvc β€” f36ba607 Deployed Oct 16, 2026 by vercel[bot]
Preview – next-prisma-starter β€” f36ba607 Deployed Oct 16, 2026 by vercel[bot]
Preview – trpc-next-app-dir β€” f36ba607 Deployed Oct 16, 2026 by vercel[bot]
Preview – og-image β€” f36ba607 Deployed Oct 16, 2026 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: node-http adapter aborts every request's signal after a normal response

1 participant