Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-4599

Investigate NODE-4598 - memory leak when breaking in the middle of a cursor's async iterator

    XMLWordPrintable

Details

    • Task
    • Status: Closed
    • Unknown
    • Resolution: Done
    • None
    • 4.10.0
    • None
    • 2
    • Not Needed

    Description

      NODE-4598 Description

      What problem are you facing?

      When using an async iterator, like

      const cursor = col.find()
       
      for await (const doc of query) {
        console.log(doc)
        break
      }

      breaking the iterator before the end causes a memory leak. The 'sessions' used on the find are not closed/cleaned up
      Printing activeSessions's size from client.s.activeSessions reveals that, when breaking in the middle of an async iterator (before the end), the number of sessions increases indefinitely, which does NOT happen if you consume the iterator thought the end (the number stays consistent)

      What driver and relevant dependency versions are you using?

      mongodb driver for nodejs, latest version (4.9.0)

      It seems that the bug was introduced on [#3286|https://github.com/mongodb/node-mongodb-native/pull/3286,] it was the last PR that changed the src/mongo_client.ts and introduced the activeSessions Set

      I've tested it with a version before this commit (4.5.0), that uses the old sessions Set and the leak seems to not happen

      Steps to reproduce?

      I've made a repository to reproduce this bug: https://github.com/GCastilho/mongodb-cursor-memory-leak

      Just clone it, and run 'npm start'. The used data as well as the 'activeSessions' will be printed periodically for one minute

      Attachments

        Issue Links

          Activity

            People

              durran.jordan@mongodb.com Durran Jordan
              dbeng-pm-bot PM Bot
              Durran Jordan Durran Jordan
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: