Skip to content

Injectable instances can have references to Request objects and can hold it forewer in request scope #13365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 of 15 tasks
breeeew opened this issue Mar 27, 2024 · 0 comments
Closed
5 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@breeeew
Copy link
Contributor

breeeew commented Mar 27, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When nest handle any request on first time - it gets metadata for controller's handler and stores it in cache forever.
When that controller utilizes the request object, that injected by REQUEST token - it will have reference to request object in scope and will hold it forewer, because of cache.

Minimum reproduction code

https://github.com/breeeew/nestjs-leak-reproduction

FIX

Steps to reproduce

  1. npm i
  2. npm run start:debug
  3. attach debugger and dump heap - search for AppService in debuggers console - there will be just prototype object
  4. collect garbage
  5. navigate in browser to /
  6. collect garbage
  7. dump heap again
  8. search for AppService again - there will be second entry and it will be real instance

before
after

Expected behavior

All unused objects must be collected by gc

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

latest

Packages versions

[System Information]
OS Version     : Linux 6.5.0-26-generic
NodeJS Version : v18.19.1
NPM Version    : 10.2.4 

[Nest CLI]
Nest CLI Version : 10.3.2 

[Nest Platform Information]
platform-express version : 10.3.5
schematics version       : 10.1.1
testing version          : 10.3.5
common version           : 10.3.5
core version             : 10.3.5
cli version              : 10.3.2

Node.js version

18, 20

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

Our case: sometimes user requests some handler in request scope, that downloads a huge json - and that json have been parsed and holds a huge amount of memory. And it also slows down garbage collectiong process (while collecting it increases cpu usage and memory usage by nodejs)

@breeeew breeeew added the needs triage This issue has not been looked into label Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

2 participants