Repository navigation
fix(core): share one in-flight load between concurrent lazy loads - #18371
Merged
kamilmysliwiec merged 1 commit intoNov 3, 2026
Merged
kamilmysliwiec merged 1 commit into
kamilmysliwiec merged 1 commit into
Conversation
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.
4 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
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, andcreatePrototypes()sets every class provider's instance toObject.create(proto)before the first await, soisInstantiatedis already true from that point.A second
load()that arrives while the first one awaits an async factory gets[]back fromscanForModules(thelazy && !moduleInserted && isInstantiatedguard 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 readsnull. If both calls start beforecreatePrototypes(), 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):Issue Number: N/A
Steps to reproduce
lazy-module-loader.tsand run the new "when load() is called while another load() of the module is in flight" block inpackages/core/test/injector/lazy-module-loader/lazy-module-loader.spec.tsload()returns the providers built once, with the factory's value injectedWhat is the new behavior?
The loader keeps a
WeakMap<Module, Promise<void>>of the modules a runningload()is building. Aload()whose scan comes back empty awaits the build that owns the module before it returns the ref. Aload()whose scan returns modules anotherload()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 infinally.The state is keyed by
Module, soload(DashboardModule), which buildsReportsModuleas its import, also makes a concurrentload(ReportsModule)wait for it. There's no global queue, so a provider factory thatload()s an unrelated module doesn't wait on itself.The
{ logger: false }swap from #17631 now wraps only the build. Aload()that only waits never touches the InstanceLoader logger, so it can't capture the SilentLogger set by anotherload()and put it back after that load already restored the original.Same probe on this branch:
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 everyload()waiting on it with the same error and the module leaves the map, so a laterload()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 sameload()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 existingtoEqual([...])calls in the spec.Does this PR introduce a breaking change?
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)logger.service.spec.ts"should serialise a plain JS object…" and the platform-node body-parser charset testintegration/lazy-modules: 3/3