fix(core): isolate nested transient instances in static context#16362
Open
veeceey wants to merge 2 commits intonestjs:masterfrom
Open
fix(core): isolate nested transient instances in static context#16362veeceey wants to merge 2 commits intonestjs:masterfrom
veeceey wants to merge 2 commits intonestjs:masterfrom
Conversation
When multiple DEFAULT-scoped providers inject the same TRANSIENT chain (e.g., ServiceA -> TransientLogger -> NestedTransient and ServiceB -> TransientLogger -> NestedTransient), the nested transient instances were incorrectly shared because the transient map key (inquirerId) is per-class, not per-instance. In STATIC context, getEffectiveInquirer returned the intermediate TRANSIENT wrapper's id, which is the same regardless of which DEFAULT parent initiated the chain. This fix introduces a composite key (parentInquirer.id:inquirer.id) for nested TRANSIENT chains in STATIC context via getEffectiveInquirerId(). The composite key differentiates resolution paths so each DEFAULT parent gets its own isolated nested transient instances. Also updates TestingInjector.resolveComponentHost() to forward the new inquirerIdOverride parameter to the parent class. Closes nestjs#16257
Pull Request Test Coverage Report for Build 4594ff5f-f403-4927-91ad-e3405f57f30cDetails
💛 - Coveralls |
Author
|
friendly ping - any chance someone could take a look at this? the transient scope isolation issue is kind of subtle but it bites people |
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.
Description
Fixes #16257
When multiple DEFAULT-scoped providers inject the same TRANSIENT -> TRANSIENT chain, the deeply nested transient instances are incorrectly shared. For example:
In this scenario,
serviceA.logger.nestedandserviceB.logger.nestedpoint to the sameNestedTransientinstance, even though bothTransientLoggerinstances are correctly different.Root cause
The transient map uses the inquirer's wrapper ID as the key. In STATIC context,
getEffectiveInquirer()has acontextId !== STATIC_CONTEXTguard that prevents it from returning the parent inquirer for nested TRANSIENT chains. This means the intermediateTransientLoggerwrapper's ID is used as the key regardless of which DEFAULT parent (ServiceA vs ServiceB) initiated the chain — so the nested instance gets shared.Fix
Introduces
getEffectiveInquirerId()which returns a composite key (parentInquirer.id:inquirer.id) for nested TRANSIENT chains in STATIC context. This ensures each resolution path through a different DEFAULT parent produces its own isolated nested instance, without affecting the existing behavior for non-nested cases or REQUEST-scoped contexts.Also updates
TestingInjector.resolveComponentHost()to forward the newinquirerIdOverrideparameter.Type of change
Checklist