Client network socket disconnected error after upgrading to MongoDB driver v6

Running a node js (v 18.13.0) app under Heroku (Heroku-22 stack) using express and mongoose to access a MongoDB Atlas cluster. This app was working until I migrated it from mongoose v6.8.4 (MongoDB driver v4) up to mongoose v8.16.1 (MongoDB driver v6). After this update, the app fails to connect to the database, with the error:
Client network socket disconnected before secure TLS connection was established

Some background: I made updates to the app for breaking changes between mongoose versions. Successfully tested the app with the latest mongoose package / MongoDB driver against a local MongoDB 8.0 community server. However it fails when running under Heroku while trying to connect to the Atlas db. Note that SSL certificates are managed by Heroku. I also see that in the latest MongoDB driver release notes, TLS certificate authority and certificate-key files are now read asynchronously (don’t know if this factors into the problem).

I finally reverted my app to use the mongoose v6.8.4 / MongoDB driver v4 package (while keeping the migration code changes I had made to move up to the latest driver), and viola, the app is now working again under Heroku and my Atlas MongoDB cluster.

So, it seems that some difference in the latest drivers behavior is causing a break in how my app or Heroku or Atlas is dealing with the connect. Any ideas?

This is not an IP white list problem. Also, my mongoose connect code is rather vanilla:

const useSsl = (app.get(‘env’) !== ‘development’);
mongoose.connect(mongoDbUrl, {ssl: useSsl})
.then(() => console.log(‘Connected to MongoDB’))
.catch(error => console.error(‘Error connecting to MongoDB:’, error));

1 Like

Here’s some new information. I tried some versions of mongoose / MongoDB driver that are earlier than the latest 6.17.0 driver, but later than my v4 driver. Mongoose 8.12 / MongoDB driver 6.14 still has the problem. But, mongoose 7.6.3 / MongoDB 5.9 works! This strongly points to some change introduced in the MongoDB v6 drivers that is playing a role in this failure. Not to say there is necessarily a bug in the driver. It could just be a valid change that is interacting with the way either Heroku is managing SSL/TLS and/or my node js code.

Anyone familiar with changes introduced in the MongoDB v6 drivers regarding connection logic? This would be very helpful in gaining insight into what’s causing this issue. In the meantime, I am not blocked in my development, as using the MongoDB v5 driver gives me some more runway before having to move to a newer driver.

1 Like

Hi, we’re experiencing the same issue (Client network socket disconnected before secure TLS connection was established) with the following setup:

  • Node.js runtime 20
  • MongoDB driver ~6.15.0
  • Mongoose ~8.13.0

We also tested with:

  • Node.js runtimes 20 and 22
  • MongoDB driver ~6.17.0
  • Mongoose ~8.16.0

Our infrastructure runs on AWS Lambda, connecting to MongoDB Atlas via a VPC.

We’ll try the versions David_Klein mentioned (Mongoose 7.6.3 / MongoDB driver 5.9) to see if the issue persists.

1 Like

Just to clarify, our MongoDB Atlas cluster has already been fully migrated to version 8.

The MongoDB Node.js driver 5.9 is not fully compatible with MongoDB 8 (see: Compatibility - Node.js Driver v6.17 - MongoDB Docs), but since we’re not using any of the new features, it has been working for us so far.

We tested downgrading the versions, and everything seems to be working, so we suspect the issue might be related to Mongoose, the MongoDB Node.js driver, or a new configuration introduced recently.

Could someone assist us with this?

1 Like

FYI: We are observing this issue under high transaction load (8 or more transactions per second).

1 Like