Skip to content

http: memory leak if a server is not closed #48604

Description

@mcollina

Version

v18.16.1

Platform

Mac

Subsystem

http

What steps will reproduce the bug?

Run

const http = require('http')

async function main () {
  let i = 0
  while (true) {
    if (i % 100 === 0) {
      global.gc()
    }

    if (i % 100000 === 0) {
      console.log(process.memoryUsage())
    }

    http.createServer((req, res) => {})
    i++
  }
}

main()

How often does it reproduce? Is there a required condition?

All the time

What is the expected behavior? Why is that the expected behavior?

The memory should not grow uncontrolled

What do you see instead?

Memory growing and after many iterations a crash

Additional information

No response

Activity

  1. added
    confirmed-bugIssues and PRs for confirmed bugs.
    httpIssues and PRs related to the http subsystem.
    on Jun 29, 2023
  2. lpinca commented on Jun 29, 2023

    @lpinca
    Member

    To be honest I don't think this is a bug. It's like opening sockets and never closing them.

  3. lpinca commented on Jun 29, 2023

    @lpinca
    Member

    Sorry, my bad, I did not notice that there is no server.listen() in the example, but anyway I think that the "leak" is expected with that sync loop.

  4. lpinca commented on Jun 29, 2023

    @lpinca
    Member

    I can reproduce even with an async loop so it's not that.

  5. lpinca commented on Jun 29, 2023

    @lpinca
    Member

    This seems to fix the issue

    diff --git a/lib/_http_server.js b/lib/_http_server.js
    index 0242e7a089..571ed15c6d 100644
    --- a/lib/_http_server.js
    +++ b/lib/_http_server.js
    @@ -549,7 +549,7 @@ function Server(options, requestListener) {
       this.timeout = 0;
       this.maxHeadersCount = null;
       this.maxRequestsPerSocket = 0;
    -  setupConnectionsTracking(this);
    +  // setupConnectionsTracking(this);
       this[kUniqueHeaders] = parseUniqueHeadersOption(options.uniqueHeaders);
     }
     ObjectSetPrototypeOf(Server.prototype, net.Server.prototype);
    

    cc: @ShogunPanda

  6. theanarkh commented on Jun 30, 2023

    @theanarkh
    Contributor

    Maybe we should call setupConnectionsTracking on listen event ?

  7. lpinca commented on Jun 30, 2023

    @lpinca
    Member

    It makes sense to me, there is no connection to track if the server is not listening.

  8. ShogunPanda commented on Jun 30, 2023

    @ShogunPanda
    Contributor

    That is fine, but it does not explain the leak. I'll try to dig into this in the next few days.

  9. theanarkh commented on Jun 30, 2023

    @theanarkh
    Contributor

    I think because the anonymous function hold the server object.

    setInterval(checkConnections.bind(server), server.connectionsCheckingInterval).unref();
  10. ShogunPanda commented on Jun 30, 2023

    @ShogunPanda
    Contributor

    Yes, I confirm that is the issue. I'll move that call in a listen even as suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.httpIssues and PRs related to the http subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions