Since upgrading version to 6, disconnected event followed by unhandled promise rejection "mongoServerSelectionError" #14550
Labels
help wanted
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Prerequisites
Mongoose version
6.12.8
Node.js version
18.20.2
MongoDB version
4.17.2
Operating system
None
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
Please provide some insight or direction on debugging this issue.
We run a number of Nodejs services in AWS Lambda. My team has migrated the Mongoose version to 6.12.8 about 7 months ago, and at the same time, we upgraded our Nodejs version from 14 to 18. At that time, we noticed an intermittent issue arising; the first lambda invocation will run successfully, then if there is 2-5 minutes before the next invocation, it will then immediately log the mongo 'disconnected' event followed by an 'error' event (both still logging within the context of the previous invocation judging by the apiRequestID) and an Unhandled Promise Rejection 'mongoServerSelectionError'. This causes the entire lambda instance to blow up and need to restart for the next invocation.
We do not know what is causing the serverSelectionError. We have observed the issue in many of our services and lambda handlers. We have tried to recreate the error both locally and within a simplified lambda environment, but we cannot intentionally reproduce it.
I have seen many other reported issues both here on github and elsewhere referring to a serverSelectionError after upgrading to version 6, but I don't believe those were specific to running within a lambda environment.
Here is one example of logs:
I believe the Unhandled Promise Rejection is originating from this piece of code in connections.js:825:
const serverSelectionError = new ServerSelectionError(); this.$initialConnection = promise. then(() => this). catch(err => { this.readyState = STATES.disconnected; if (err != null && err.name === 'MongoServerSelectionError') { err = serverSelectionError.assimilaateError(err); } if (this.listeners('error').length > 0) { immediate(() => this.emit('error', err)); } **throw err;** });
Here is how we are creating the connection:
const connection = await mongoose.createConnection(modelConnectionMap[domain], { maxPoolSize: (options && options.maxPoolSize) ? options.maxPoolSize : 2, minPoolSize: (options && options.minPoolSize) ? options.minPoolSize : 1, connectTimeoutMS: 300000, socketTimeoutMS: 7200000, });
We have a connection per domain, which is only created once when any domain is first referenced.
The text was updated successfully, but these errors were encountered: