Skip to content

refactor(server): start the sse maxDurationMs timer alongside the stream it ends - #7699

Merged
trpc-bot merged 1 commit into
mainfrom
refactor/sse-max-duration-timer
Oct 19, 2026
Merged

trpc-bot merged 1 commit into
mainfrom
refactor/sse-max-duration-timer

Conversation

@KATT

@KATT KATT commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #7666.

What changed

resolveResponse no longer starts the SSE maxDurationMs timer inside each RPC call and threads a clearMaxDurationTimer callback back out through RPCResultOk (along with a catch cleanup and a try/catch around initResponse).

Instead:

  • RPCResultOk carries a single abort?: ReturnType<typeof combinedAbortController>, set only for subscriptions.
  • The timer is started in the SSE response branch, next to the reader wrapper that already handles the abort signal. When it fires, it calls abort.controller.abort().
  • A single onStreamEnd clears the timer and removes the abort listener. It runs when the stream finishes, when a read errors, and when the body is cancelled. onAbort calls onStreamEnd() and then reader.cancel().

Because the timer only exists once there is a stream to end, the paths where the procedure throws or responseMeta throws never create one, so they need no cleanup.

Behaviour note

Max duration is now measured from when the SSE stream is created, not from before the procedure (and its middlewares) runs. In practice that is usually a difference of a few milliseconds.

Why not using

The timer has to outlive resolveResponse: it lives as long as the response body, so a scope-bound using disposal doesn't fit.

Tests

sse maxDurationMs timer is not left pending when the subscription %s (in packages/tests/server/adapters/fetch.test.ts) now has a startsTimer column. It is true for "completes", "is cancelled" and "errors the stream", and false for "throws" and "fails in responseMeta". The test asserts how many max-duration timers were created and that none of them were left uncleared.


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

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of streaming subscriptions with maximum-duration limits. Limits are now applied while the response stream is active, and cleanup occurs when the stream completes, is cancelled, or fails.
    • Failures before streaming begins no longer start unnecessary duration timers, helping ensure consistent subscription behavior across outcomes.

@KATT
KATT requested a review from a team as a code owner October 19, 2026 22:15
@coderabbitai

coderabbitai Bot commented Oct 19, 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: 2d0fac7a-cf68-4c7f-a89a-b5450a518a8e
πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 243cd99 and 7d8f769.

πŸ“’ Files selected for processing: 2
  • packages/server/src/unstable-core-do-not-import/http/resolveResponse.ts
  • packages/tests/server/adapters/fetch.test.ts

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

πŸ“ Walkthrough

Subscription RPC results now carry an abort controller. The SSE response handler manages the max-duration timer and stream cancellation. The fetch adapter test checks which subscription outcomes start a timer and verifies that started timers are cleared.

Changes: SSE subscription duration handling

Layer / File(s) Summary
Return the subscription abort controller
packages/server/src/unstable-core-do-not-import/http/resolveResponse.ts
Subscription RPC results now retain the combined abort controller. Timer setup and cleanup are removed from RPC execution.
Manage timer and stream cancellation
packages/server/src/unstable-core-do-not-import/http/resolveResponse.ts, packages/tests/server/adapters/fetch.test.ts
The SSE response handler sets the configured timer and clears it on stream end or abort. Abort cancels the stream reader. Tests check expected timer creation and cleanup.

Priority: ⬇️ Low

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

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant RPCResultOk
  participant SSEResponseHandler
  participant AbortController
  participant StreamReader
  RPCResultOk->>SSEResponseHandler: provide subscription abort controller
  SSEResponseHandler->>AbortController: abort when max-duration timer expires
  AbortController->>StreamReader: cancel on abort
  SSEResponseHandler->>SSEResponseHandler: clear timer and remove abort listener on stream end or abort
Loading

Suggested reviewers: nick-lucas

Merge Risk: βšͺ Minimal Β· up to 7d8f7

The timer now tracks the stream’s lifetime, with no identified cleanup or cancellation defect. This change is mergeable after normal checks.

Pre-merge checks: Passed 4 Β· Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: moving the SSE maxDurationMs timer to start with the stream.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches: πŸ’‘ 1
πŸ“ Generate docstrings: πŸ’‘
  • Commit to this branch
  • Create a new PR
πŸ§ͺ Generate unit tests (beta)
  • Commit to this branch
  • 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/unstable-core-do-not-import/http/resolveResponse.ts

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

packages/tests/server/adapters/fetch.test.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 19, 2026

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

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

@trpc/next

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

@trpc/openapi

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

@trpc/react-query

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

@trpc/server

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

@trpc/tanstack-react-query

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

@trpc/upgrade

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

commit: 7d8f769

@vercel

vercel Bot commented Oct 19, 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 19, 2026 10:30pm UTC
og-image Ready Ready Preview Oct 19, 2026 10:30pm UTC

Request Review

@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 β€” 7d8f769b Deployed Oct 19, 2026 by vercel[bot]
Preview – og-image β€” 7d8f769b Deployed Oct 19, 2026 by vercel[bot]
Preview – examples-todomvc β€” 7d8f769b Deployed Oct 19, 2026 by vercel[bot]
Preview – next-prisma-starter β€” 7d8f769b Deployed Oct 19, 2026 by vercel[bot]
Preview – trpc-next-app-dir β€” 7d8f769b Deployed Oct 19, 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.

2 participants