Repository navigation
Conversation
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
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. π βΉοΈ Recent review info
π Walkthrough
Merge Risk: βͺ Minimal Β· up to
|
@trpc/client
@trpc/next
@trpc/openapi
@trpc/react-query
@trpc/server
@trpc/tanstack-react-query
@trpc/upgrade
commit: |
β¦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]>
|
Closing as superseded β #7664 landed the fix for this first. Congrats to mrityunjay-pandey on the merge. |
|
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. |
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
AbortControlleron every response'close'event β and Node emits'close'after every response, including ones that completed normally. As a result,opts.signalreportedaborted: truefor 100% of successful requests, so procedure code could not distinguish "client went away" from "request completed". Each spurious abort also constructs aDOMExceptionwith a captured stack trace (measured ~2.7% CPU on a trivial query under load in the issue).Root cause:
res.once('close', onAbort)β unconditionalac.abort()inpackages/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 beforeres.end(), sowritableFinishedisfalseand the abort still fires exactly when the client actually went away. I verified this discriminator empirically against realhttp.ServerResponseobjects: normal completion βwritableFinished: trueat'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)createServer: 5 completed requests β 0 aborted signals (fails before the fix: 5/5 aborted)Results:
incomingMessageToRequest.test.ts: 15/15 passstandalone,express,fastify,standalone.http2): all pass exceptstandalone > custom host, which fails identically on the pristine tree (binds to TEST-NET IP198.19.0.2, unreachable in this sandbox β pre-existing environment failure, unrelated)tsc --noEmiton@trpc/server: clean; ESLint + Prettier: cleanLimitations
writableFinished, which is set onceres.end()has been called and the stream finished. A response whose socket is destroyed afterend()but before the flush completes will still abort β arguably correct, since the client never received the complete response.β Checklist
Summary by CodeRabbit