-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Description
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
When multiple DEFAULT scoped providers inject the same TRANSIENT → TRANSIENT chain, the nested TRANSIENT instances are shared instead of being isolated.
TestController (DEFAULT) → TransientLogger [1] → NestedTransientService [1]
TestService (DEFAULT) → TransientLogger [2] → NestedTransientService [1] ← Reused!
Each TransientLogger should receive its own NestedTransientService instance, but they share the same instance.
The root cause is the contextId !== STATIC_CONTEXT condition in getEffectiveInquirer()
// packages/core/injector/injector.ts:977-988
private getEffectiveInquirer(...) {
return dependency?.isTransient &&
inquirer?.isTransient &&
parentInquirer &&
contextId !== STATIC_CONTEXT // ← This excludes STATIC context
? parentInquirer
: inquirer;
}Minimum reproduction code
https://github.com/stineslenea/logger-transient-example
Steps to reproduce
- Clone https://github.com/stineslenea/logger-transient-example
- Update @nestjs/* packages to 11.1.12
- Remove line 12 in src/test.service.ts: @Inject(REQUEST) private readonly request: any,
- Run npm start
- Call GET /test
- Check logs: NestedTransientService instance is shared between Controller and Service
Expected behavior
Each TransientLogger should receive its own NestedTransientService instance
TestController (DEFAULT) → TransientLogger [1] → NestedTransientService [1]
TestService (DEFAULT) → TransientLogger [2] → NestedTransientService [2] ← Expected
NestJS version
11.1.12
Packages versions
{
"@nestjs/common": "11.1.12",
"@nestjs/core": "11.1.12",
"@nestjs/platform-express": "11.1.12"
}
Node.js version
22.x
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
Related Issues/PRs
- Bug: nested transient scoped service instance is being reused within request context #15689: Same issue in REQUEST context (Fixed by fix(core): ensure nested transient provider isolation #15815)
- A transient service uses another transient service #16097: TRANSIENT instantiation in STATIC context (Fixed by PR fix(core): instantiate nested transient providers in static context #16098)
This is the missing piece. TRANSIENT isolation in STATIC context.
The fix in #15815 explicitly excludes STATIC context with contextId !== STATIC_CONTEXT.
This condition needs to be revisited to also handle STATIC context isolation.