-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
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?
- create cloud.mongodb.com free tier cluster
- clone github.com/yrambler2001/mongodb-4.16-connections-bug
- 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
- npm run start-bad,
- after 1-2 minutes, on the cloud.mongodb.com you can see 300-500 connections
- npm run start-ok
- after 1-2 minutes, on the cloud.mongodb.com you can see 100 connections
- related to
-
NODE-5106 MongoClient concurrent connect can lead to Topology leak
- Closed