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

Uncaught exception MongoServerSelectionError during primary node election #11661

Closed
francescov1 opened this issue Apr 13, 2022 · 6 comments
Closed

Comments

@francescov1
Copy link
Contributor

Do you want to request a feature or report a bug?

Report a bug

What is the current behavior?

We are currently running a MongoDB Atlas replica set. We are finding that when our cluster needs to re-elect a new primary, our application loses connection to MongoDB, regains connection, then throws an uncaught exception MongoServerSelectionError: The server is in quiesce mode and will shut down.

If the current behavior is a bug, please provide the steps to reproduce.

Here is the code we use for connecting:

  mongoose
    .connect(config.database, {
      maxPoolSize: 100,
      autoIndex: config.mongoose.autoCreateIndicesAndCollections,
      autoCreate: config.mongoose.autoCreateIndicesAndCollections,
      promiseLibrary: Bluebird
    })
    .catch(handleError);

  mongoose.connection.on('connected', () => log.info('Mongodb connected'));
  mongoose.connection.on('open', () => log.info('Mongodb connection opened'));
  mongoose.connection.on('error', handleError);

  mongoose.connection.on('disconnected', () =>
    log.warn('Mongodb disconnected')
  );

The error can be reproduced using the Test Failover option in Atlas or by scaling the cluster up or down. When doing this, here are the logs we see:

# initial connection
info: Mongodb connected {"timestamp":"2022-04-13T17:06:57.590Z"}
info: Mongodb connection opened {"timestamp":"2022-04-13T17:06:57.595Z"}

# disconnect event from primary re-election
warn: Mongodb disconnected {"timestamp":"2022-04-13T17:14:48.557Z"}

# reconnect events immediately after connection loss
info: Mongodb connected {"timestamp":"2022-04-13T17:14:48.738Z"}
info: Mongodb connection opened {"timestamp":"2022-04-13T17:14:48.739Z"}

# uncaught exception immediately after reconnect
error: MongoServerSelectionError: The server is in quiesce mode and will shut down {"type":"uncaughtException","timestamp":"2022-04-13T17:15:19.418Z"}

What is the expected behavior?

  • Mongoose does not throw an uncaught exception, the error is reported through the mongoose.connection.on('error', ...) listener.
  • Mongoose does not lose connection during a re-election (this could be an underlying Mongo issue so might not be relevant here).

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.js: 16.8
Mongoose: 6.1.6
MongoDB: 5.0

@vkarpov15 vkarpov15 added this to the 6.2.15 milestone Apr 14, 2022
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Apr 14, 2022
@francescov1
Copy link
Contributor Author

Not sure how to create a repro script since my only way to repro is to perform a Test Failover in Mongo Atlas. All you need is a local Node.js process running with an opened connection to the database to see the issue.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Apr 14, 2022

Die you try Mongoose 6.3? We updated the Mongodb native Driver. Maybe they fixed the issue?

@francescov1
Copy link
Contributor Author

I'll try that out!

@francescov1
Copy link
Contributor Author

After updating to mongoose 6.3.0 the uncaught exception is not thrown anymore but I am still seeing the disconnect event occur during a re-election (logs look the exact same as above, other than the error log at the end).

@vkarpov15 vkarpov15 modified the milestones: 6.3.4, 6.3.6 May 11, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.3.6, 6.3.7 Jun 7, 2022
@vkarpov15
Copy link
Collaborator

@francescov1 seeing the 'disconnected' event during re-election is expected. The unhandled 'error' is not expected, but that looks fixed.

The 'disconnected' event is purely for debugging and monitoring. It doesn't necessarily mean that Mongoose can't talk to any members in the replica set or execute any operations. It just means that Mongoose lost connectivity to the replica set primary, which is expected during replica set failover. As long as your replica set failover happens quickly (faster than serverSelectionTimeoutMS) none of your operations should fail, you can still save(), find(), etc. But those operations won't succeed until after the replica set failover completes.

@vkarpov15 vkarpov15 removed this from the 6.3.7 milestone Jun 11, 2022
@vkarpov15 vkarpov15 removed the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Jun 11, 2022
@vkarpov15 vkarpov15 removed their assignment Jun 11, 2022
@francescov1
Copy link
Contributor Author

@vkarpov15 Thanks for the thorough explanation, this is great to hear 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants