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

memory leak when breaking in the middle of a cursor's async iterator

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.0
    • Affects Version/s: 4.9.0
    • Component/s: None
    • 2
    • Not Needed
    • Not Needed

      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

            Assignee:
            durran.jordan@mongodb.com Durran Jordan
            Reporter:
            castilho.gabriel14@gmail.com GCastilho N/A
            Neal Beeken
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: