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

node mongodb@4 creates unlimited connections to the database

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.17.0
    • Affects Version/s: None
    • Component/s: None
    • 2
    • Not Needed
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      What problem are you facing?

      mongo client have autoconnect on operation which introduced a bug when you can create multiple connections by executing multiple operations and not awaiting them.

      without `await client.connect()`, the following code will create 5 internal `client.connect() `executions:
      ```
      client.db("admin").command({ ping: 1 });
      client.db("admin").command({ ping: 1 });
      client.db("admin").command({ ping: 1 });
      client.db("admin").command({ ping: 1 });
      client.db("admin").command({ ping: 1 });
      ```
       
      here is the code that will create a new connection . It will be executed 5 times.
      Inside client.connect function there is an await which prevents   if (client.topology == null) { to omit reconnecting. 
       

      this is a code that sets client.topology 

       

      and this is await which prevents setting that client.topology 

       

      mongodb@5.5.0 has a connection lock which fixes the issue completely https://github.com/mongodb/node-mongodb-native/blob/v5.5.0/src/mongo_client.ts#L418

      it should be backported to mongodb@4

       

      this issue caused a bug in Meteor https://github.com/meteor/meteor/issues/12642#issuecomment-1567398407

      What driver and relevant dependency versions are you using?

      "mongodb": "4.16.0"

      Steps to reproduce?

      1. create cloud.mongodb.com free tier cluster
      2. clone github.com/yrambler2001/mongodb-4.16-connections-bug 
      3. open works-bad.js and works-well.js and change connection string to cluster created in step 1. KEEP THE mongodb+srv PART AND &maxPoolSize=100&minPoolSize=100 PART
      4. npm run start-bad,
      5. after 1-2 minutes, on the cloud.mongodb.com you can see 300-500 connections
      6. npm run start-ok
      7. after 1-2 minutes, on the cloud.mongodb.com you can see 100 connections

      https://youtu.be/WmdRn7h2HdM

            Assignee:
            bailey.pearson@mongodb.com Bailey Pearson
            Reporter:
            yrambler2001@gmail.com Юра Синишин
            Neal Beeken
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: