Skip to content

fix(server): retry a lazy router load after it rejects - #8150

Merged
KATT merged 1 commit into
trpc:mainfrom
Kulraj69:fix/lazy-router-retry
Nov 23, 2026
Merged

KATT merged 1 commit into
trpc:mainfrom
Kulraj69:fix/lazy-router-retry

Conversation

@Kulraj69

@Kulraj69 Kulraj69 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Why

A lazy() router whose loader rejects once never loads again: once() keeps the rejected promise, so every later call rethrows the same error until the process restarts.

What

  • once() in router.ts drops the cached promise when it rejects, so the next call runs the loader again.
  • Successful and in-flight loads stay cached, so concurrent callers still share one load.

Links

None

Alternatives considered

  • Wrap the loader in lazy() instead of changing once(): once() is only used for lazy loads, so fixing it there is simpler.

Testing

Regression check: packages/tests/server/regression/issue-8149-lazy-router-retry.test.ts fails without the fix (the second call still rejects with the first error) and passes with it. It also checks a successful load and a concurrent load each call the loader once.

AI model

Devin (Cognition AI) wrote this change.

Summary by CodeRabbit

  • Bug Fixes
    • Lazy router loads can now be retried after a failure. Successful loads are reused, and concurrent requests share the same in-progress load.
  • Tests
    • Added coverage for retrying failed loads, reusing successful loads, and handling concurrent requests.

Regression check: packages/tests/server/regression/issue-8149-lazy-router-retry.test.ts catches a cached rejection preventing later loads.

Co-authored-by: Devin <[email protected]>
@Kulraj69
Kulraj69 requested a review from a team as a code owner November 22, 2026 23:24
@vercel

vercel Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
examples-todomvc Ready Ready Preview Nov 22, 2026 11:25pm UTC
next-prisma-starter Ready Ready Preview Nov 22, 2026 11:25pm UTC
trpc-next-app-dir Ready Ready Preview Nov 22, 2026 11:25pm UTC
www Ready Ready Preview Nov 22, 2026 11:25pm UTC

Request Review

@vercel

vercel Bot commented Nov 22, 2026

Copy link
Copy Markdown

@Kulraj69 is attempting to deploy a commit to the trpc Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

once now caches promise-producing calls, clears the cache after rejection, and rethrows the error. Regression tests cover retry after failure, reuse of successful loads, and sharing an in-flight load.

Changes: Lazy router retry

Layer / File(s) Summary
Promise cache and regression tests
packages/server/src/unstable-core-do-not-import/router.ts, packages/tests/server/regression/issue-8149-lazy-router-retry.test.ts
once caches the in-flight or fulfilled promise and resets the cache after rejection. Tests check that a later call retries, successful loads are reused, and concurrent calls share one load.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: katt, juliusmarminge

Merge Risk: Minimal · up to ff5fe

This change lets a lazy router load be retried after it fails, while successful and in-flight loads are still cached. The change is small and has regression tests. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title 'fix(server): retry a lazy router load after it rejects' matches the main change. The raw summary says the once helper now clears its cached promise when the promise rejects, so a later ca…
Linked Issues check Passed Issue #8149 has one direct, open coding requirement: a lazy() router whose loader rejects must load again on a later call, and concurrent callers must still share one in-flight load. At the reviewed…
Out of Scope Changes check Passed The PR changes two files. once() in router.ts gets the reset-on-rejection logic that issue #8149 asks for. The new test file exercises only the lazy router retry, successful caching, and concurren…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title 'fix(server): retry a lazy router load after it rejects' matches the main change. The raw summary says the once helper now clears its cached promise when the promise rejects, so a later call invokes the loader again. The added regression tests cover the retry after rejection, reuse of a successful load, and sharing of one in-flight load. The title is specific and concise, and it names the scope (server) and the behavior changed.

Full details: Linked Issues check

Explanation

Issue #8149 has one direct, open coding requirement: a lazy() router whose loader rejects must load again on a later call, and concurrent callers must still share one in-flight load. At the reviewed head, once() in packages/server/src/unstable-core-do-not-import/router.ts sets result = uncalled inside the rejection handler and rethrows. A later call therefore runs the loader again. The result is cached while it is pending or after it fulfills, so concurrent callers share one load. The new regression test packages/tests/server/regression/issue-8149-lazy-router-retry.test.ts covers three cases: a rejected load is retried and the successful result is cached, concurrent calls share one in-flight load, and the loader runs once for each. The test file was read at head. The diff of router.ts against the merge base could not be read, so the change is confirmed from the head file and the PR summary, not from the diff itself.

Full details: Out of Scope Changes check

Explanation

The PR changes two files. once() in router.ts gets the reset-on-rejection logic that issue #8149 asks for. The new test file exercises only the lazy router retry, successful caching, and concurrent sharing behavior. Both changes connect directly to the linked issue. No unrelated changes were identified in the PR summary or the head file. The whole-PR diff of router.ts could not be read, so the absence of other edits rests on the PR summary and the AI-generated change summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • 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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: trpc/trpc
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 0813ceb
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 85010db7-40f5-4439-a465-35e44a64c818
Commits

Reviewing files that changed from the base of the PR and between 0813ceb and ff5fe72.

Files selected for processing: 2

  • packages/server/src/unstable-core-do-not-import/router.ts
  • packages/tests/server/regression/issue-8149-lazy-router-retry.test.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.

@trpc-bot trpc-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test fails on main (the second call rejects with Loading failed) and passes here.

@KATT
KATT added this pull request to the merge queue Nov 23, 2026
Merged via the queue into trpc:main with commit 908e90a Nov 23, 2026
31 of 36 checks passed
@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 Nov 24, 2026

This branch was successfully deployed

4 active deployments
Preview – www — ff5fe722 Deployed Nov 22, 2026 by vercel[bot]
Preview – next-prisma-starter — ff5fe722 Deployed Nov 22, 2026 by vercel[bot]
Preview – trpc-next-app-dir — ff5fe722 Deployed Nov 22, 2026 by vercel[bot]
Preview – examples-todomvc — ff5fe722 Deployed Nov 22, 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: lazy() router that fails to load once is never retried

3 participants