Skip to content

fix(core): share one in-flight load between concurrent lazy loads - #18371

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
maxymlyskov:fix/wait-for-in-flight-lazy-module-load
Nov 3, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
maxymlyskov:fix/wait-for-in-flight-lazy-module-load

Conversation

@maxymlyskov

Copy link
Copy Markdown
Contributor

Context: @kamilmysliwiec closed #18183 (lazy rollback) because a safe rollback first needs concurrent load() calls of the same module to share a single in-flight load. This PR does only that part. A failed load() behaves as before, and rollback is left for a follow-up

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

LazyModuleLoader.load() keeps no state for a load that's still running. The first call inserts the module and starts building it, and createPrototypes() sets every class provider's instance to Object.create(proto) before the first await, so isInstantiated is already true from that point.

A second load() that arrives while the first one awaits an async factory gets [] back from scanForModules (the lazy && !moduleInserted && isInstantiated guard in scanner.ts). load() reads that as "loaded already" and returns the ModuleRef right away. get(Service) on it is the bare prototype: the constructor never ran and the injected fields are undefined. A factory provider reads null. If both calls start before createPrototypes(), both scan, both build, and the constructors run twice.

The docs show load() used on demand from a request handler, which is where 2 requests can hit this. This reproduces with a real express app over HTTP on master (a lazy module with a 100 ms async DB factory, and requests to a handler that lazy-loads it):

PROBE concurrent: req1 -> 200 report | req2 (20ms later) -> 500 {"statusCode":500,"message":"Internal server error"} | req3 (after) -> 200 report | DB factory calls=1 ctor calls=1
PROBE simultaneous: req1 -> 200 report | req2 -> 500 {"statusCode":500,"message":"Internal server error"} | DB factory calls=1 ReportsService ctor calls=1

Issue Number: N/A

Steps to reproduce

  1. take the spec from this PR with master's lazy-module-loader.ts and run the new "when load() is called while another load() of the module is in flight" block in packages/core/test/injector/lazy-module-loader/lazy-module-loader.spec.ts
  2. Expected: a second load() returns the providers built once, with the factory's value injected
  3. Actual (raw output on untouched master):
 FAIL  packages/core/test/injector/lazy-module-loader/lazy-module-loader.spec.ts > LazyModuleLoader > load > when load() is called while another load() of the module is in flight > should return the providers after the first load() built them
AssertionError: expected undefined to be 'connection' // Object.is equality
 FAIL  packages/core/test/injector/lazy-module-loader/lazy-module-loader.spec.ts > LazyModuleLoader > load > when load() is called while another load() of the module is in flight > should construct the providers once when both load() calls start together
AssertionError: expected 2 to be 1 // Object.is equality
 FAIL  packages/core/test/injector/lazy-module-loader/lazy-module-loader.spec.ts > LazyModuleLoader > load > when load() is called while another load() of the module is in flight > should wait for a load() that builds the module as an import
AssertionError: expected undefined to be 'connection' // Object.is equality
 Test Files  1 failed (1)
      Tests  3 failed | 1 passed | 9 skipped (13)

What is the new behavior?

The loader keeps a WeakMap<Module, Promise<void>> of the modules a running load() is building. A load() whose scan comes back empty awaits the build that owns the module before it returns the ref. A load() whose scan returns modules another load() is already building waits for that build and then builds only the rest. Each load registers its own modules right after it starts the build, before any await, and removes them in finally.

The state is keyed by Module, so load(DashboardModule), which builds ReportsModule as its import, also makes a concurrent load(ReportsModule) wait for it. There's no global queue, so a provider factory that load()s an unrelated module doesn't wait on itself.

The { logger: false } swap from #17631 now wraps only the build. A load() that only waits never touches the InstanceLoader logger, so it can't capture the SilentLogger set by another load() and put it back after that load already restored the original.

Same probe on this branch:

PROBE concurrent: req1 -> 200 report | req2 (20ms later) -> 200 report | req3 (after) -> 200 report | DB factory calls=1 ctor calls=1
PROBE simultaneous: req1 -> 200 report | req2 -> 200 report | DB factory calls=1 ReportsService ctor calls=1

Unchanged: a single load(), sequential loads, the "loaded already" path for a module that finished building, and { logger: false } on a single load (the #17631 test still passes). A failed build rejects every load() waiting on it with the same error and the module leaves the map, so a later load() gets what it gets on master today. Rollback is the second half of the #18183 route and isn't in here.

One known limit: a provider factory that calls load() on its own module (or on a module the same load() is building) now waits on that build. On master it got the unbuilt providers back.

The 3 tests above fail on master and pass here. The 4th, "should restore the logger after concurrent load() calls with logger false", passes on master too; it's there to fail if the swap ran in every load() again. The lint-staged hook also reflowed 2 existing toEqual([...]) calls in the spec.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

the "should not construct the provider again on repeated load() calls" spec from #17430 already pins construct-once for sequential calls, this does the same for concurrent ones

  • packages/core: 1690 passed (1686 on master + the 4 new)
  • full unit suite: 5771 passed, 2 failed. Both fail on master too: logger.service.spec.ts "should serialise a plain JS object…" and the platform-node body-parser charset test
  • integration/lazy-modules: 3/3
  • prettier and oxlint clean

LazyModuleLoader.load() kept no state for a load that was still running.
A second load() of the same module found it already scanned and, once
createPrototypes() had run, returned its ModuleRef at once: class
providers were bare prototypes and async factory providers read null.
When both calls started before that point, both built the module and
the constructors ran twice.

The loader now records which modules each load() is building. A load()
that finds a module already scanned awaits the build that owns it, and
one whose scan returns modules another load() is building awaits that
build and builds only the rest. The logger swap for { logger: false }
wraps only the build, so a load() that only waits leaves the logger
alone.
@kamilmysliwiec
kamilmysliwiec merged commit 4d892ae into nestjs:master Nov 3, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants