Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Since upgrading version to 6, disconnected event followed by unhandled promise rejection "mongoServerSelectionError" #14550

Closed
1 task done
defields923 opened this issue May 1, 2024 · 4 comments
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@defields923
Copy link

defields923 commented May 1, 2024

Prerequisites

  • I have written a descriptive issue title

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:
image
image

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.

@defields923 defields923 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels May 1, 2024
@defields923
Copy link
Author

defields923 commented May 3, 2024

I have one more finding:

The code I showed in connection.js is definitely what is causing our handler to blow up. That catch block is immediately invoked when the lambda handler is called, preventing us from attempting to reconnect. If I deploy the code with that "throw err" removed, I still see the disconnected event but our code automatically handles the reconnect and it functions fine from there.

I see now that the createConnection method isn't returning a promise by default, so our try/catch isn't handling the promise rejection. I will see if fixing this solves our issue.

Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label May 18, 2024
@billouboq
Copy link
Contributor

@defields923 Did you find a solution to it ?

@github-actions github-actions bot removed the Stale label May 20, 2024
@defields923
Copy link
Author

@defields923 Did you find a solution to it ?

Yes, our code was calling "createConnection" without using "asPromise." On previous version of Mongoose, this did not cause same issue, but after upgrading, we needed to use it. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants