Skip to content

Incorrect lifecycle behavior of transient service #15553

@albert-mirzoyan

Description

@albert-mirzoyan

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

The onModuleInit hook is called on an transient service at bootstrap without constructing an instance (aka constructor is not called, DI is not processed) if the service is used in the request scoped Controller.

Minimum reproduction code

https://github.com/albert-mirzoyan/reproducible-transient-init/tree/main

Steps to reproduce

  1. Uncomment the injection of RequestService in the controller's constructor:
    //private readonly requestService: RequestService
  2. (Optional) Decorate the controller with @Controller({ scope: Scope.REQUEST })
  3. Restart the application.

Expected behavior

The hook TransientService.onModuleInit() should not be called if the instance is not constructed, and when it is constructed in request scope, the hook will not be called anyway.

NestJS version

11.0.1

Packages versions

  "dependencies": {
    "@nestjs/common": "^11.0.1",
    "@nestjs/core": "^11.0.1",
    "@nestjs/platform-express": "^11.0.1",
    "reflect-metadata": "^0.2.2",
    "rxjs": "^7.8.1"
  },

Node.js version

22.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

Labels

needs triageThis issue has not been looked into

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions