question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mongoose doesn't reconnect after receiving specific mongo events

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.5.0

Node.js version

14.19.3

MongoDB server version

4.2.21

Description

Recently upgraded to Mongoose 6 from 5 and getting the following behavior:

I get the following 2 events from Mongo: serverDescriptionChanged with type of Unknown topologyDescriptionChanged with type of ReplicaSetNoPrimary

Looking at the code in mongoose, these 2 events fall on both setting the state to disconnected and no reconnection is attempted: https://github.com/Automattic/mongoose/blob/ba323df0b2c56ba76ff81bd67b4dbac5f6f78c01/lib/connection.js#L877-L915

Is there a reason why this case would require to manually initiate a reconnect?

Steps to Reproduce

  1. Start a mongoose connection
  2. Receive/trigger serverDescriptionChanged with type of Unknown and topologyDescriptionChanged with type of ReplicaSetNoPrimary
  3. Mongoose disconnects and no reconnect is attempted

Expected Behavior

A reconnection would be triggered

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
david-badalovcommented, Aug 6, 2022

“Is there a reason why this case would require to manually initiate a reconnect?” No. In Mongoose 6, you never need to manually reconnect. The ‘disconnected’ event is just for monitoring and debugging.

What makes you think that Mongoose disconnects and no reconnect is attempted?

After upgrading to mongoose 6 I see a recurring heartbeat failure and the aforementioned events (this does not happen with mongoose 5). Following these events and mongoose setting the disconnected state my queries start failing:

I start getting the following errors if buffering is enabled:

"kind":"MongooseError","message":"Operation `-.findOne()` buffering timed out after 10000ms"

and a different error if it is not:

MongooseError: Cannot call `-.findOne()` before initial connection is complete if `bufferCommands = false`. Make sure you `await mongoose.connect()` if you have `bufferCommands = false`.
0reactions
github-actions[bot]commented, Oct 1, 2022

This issue was closed because it has been inactive for 19 days since being marked as stale.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose Reconnect Event is Not getting fired after some time
I finally found the answer for this, We need to use the server options of mongodb-native driver options like this.
Read more >
Mongoose not reconnecting when MongoDB driver does #6140
After a disconnect the self.isConnected() check never evaluates to true, therefore the reconnect event is never fired.
Read more >
Mongoose.connect is not working - Drivers & ODMs - MongoDB
I am getting “no connection” in my console. I wrote this in other file and required it in my index.js file.Here is code...
Read more >
Mongoose v6.8.2: Connecting to MongoDB
disconnected : Emitted when Mongoose lost connection to the MongoDB server. This event may be due to your code explicitly closing the connection,...
Read more >
Express Tutorial Part 3: Using a Database (with Mongoose)
This tutorial provides a brief overview of some of the available options and then goes on to show in detail the particular mechanisms...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found