Skip to content

bug: lazy() router that fails to load once is never retried #8149

Closed

Description

@ofri-peretz

Provide environment information

System:
  OS: macOS 26.2
Binaries:
  Node: 24.12.0
  npm: 11.6.2
npmPackages:
  @trpc/server: 11.29.5

Describe the bug

If the function passed to lazy() rejects once, that router never loads again. getProcedureAtPath awaits lazyRouter.load() (router.ts L460), and load is wrapped in once() (L354), which keeps the promise it got even when that promise rejects. After one failed dynamic import, every call into that subtree rethrows the same error and the loader is not called a second time, until the process restarts.

I'd expect a later call to try the loader again.

Link to reproduction

https://stackblitz.com/github/ofri-peretz/repro-trpc-lazy

To reproduce

import { initTRPC, lazy } from '@trpc/server';

const t = initTRPC.create();

let attempts = 0;
const user = lazy(async () => {
  attempts++;
  if (attempts === 1) throw new Error('chunk failed to load');
  return t.router({ hello: t.procedure.query(() => 'hi') });
});

const caller = t.router({ user }).createCaller({});

await caller.user.hello().catch(() => {}); // rejects, attempts === 1
await caller.user.hello(); // rejects again, attempts is still 1

npm install && npm start in the repro above prints:

call 1: chunk failed to load (loader attempts: 1)
call 2: chunk failed to load (loader attempts: 1)
call 3: chunk failed to load (loader attempts: 1)

Additional information

once() is at packages/server/src/unstable-core-do-not-import/router.ts L102-111. Clearing the stored promise when it rejects fixes it:

if (result === uncalled) {
  result = fn();
  if (result instanceof Promise) {
    result.catch(() => {
      result = uncalled;
    });
  }
}

With that change (I patched the built once in the repro's node_modules to check) the same script prints call 2: hi (loader attempts: 2). Concurrent callers still share one in-flight load, since the reset only happens after the rejection.

Happy to send a PR with a test if that direction works for you.

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR fixing this bug!

Activity

  1. added a commit that references this issue on Nov 23, 2026
    908e90a
  2. github-actions commented on Nov 24, 2026

    @github-actions
    Contributor

    This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.

  3. locked as resolved and limited conversation to collaborators on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions