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

Mongos topology leaks memory on server reconnect

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.1.10
    • Affects Version/s: Not Applicable
    • Component/s: Core
    • Labels:
      None

      From the ticket:

      I have been battling some weird memory leaks in our apps, and have found out that the culprit seems to be the mongodb driver which doesn't handle the Pool teardown properly when running against Mongos proxies (and maybe Replicasets as well).

      This is very easily reproducible by just connecting to a set of mongos proxies (2 in my case). At app start, a heapdump shows that there are two Pool instances when everything works. If I shut down network on the machine running the driver, after 30 seconds it will start eating memory like crazy and have thousands of Pool instances in the heap. This will go on indefinitely while you have a network outage, and even when you regain network connectivity, all of these instances remains and the app uses several hundred MB of RAM.

      This was the snippet I used for testing:

       

      {{// Simplest mongoDB connection app
      const MongoClient = require('mongodb').MongoClient;

      function heapDump() {
      const mem = process.memoryUsage();
      console.log('%s, Memory usage: %f', new Date().toISOString(), mem.rss);
      }

      async function start() {

      const client = await MongoClient.connect('mongodb://user:password@mongos1,mongos2/admin',

      Unknown macro: { useNewUrlParser}

      );
      const db = client.db();
      }

      start()
      .catch(err => console.error(err));
      setInterval(heapDump, 1000);}}

      The haInterval speeds things up, by default it increases with 2 Pools every 5 seconds, with this config it increases every 10 ms, making it more evident. The leak is still there.

      Not sure why it takes 30 second for the pool increase to start, but I'm guessing that is the heartbeat timeout against the servers or something.

      This was tested against mongodb@3.1.9, meaning mongodb-core@3.1.8, but I think it affect all 3.x versions (I've verified it in mongodb@3.1.4/mongo-core@3.1.3)

            Assignee:
            matt.broadstone@mongodb.com Matt Broadstone
            Reporter:
            matt.broadstone@mongodb.com Matt Broadstone
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: