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

Mongoose not reconnecting after disconnection event #14517

Open
2 tasks done
elizatlawy opened this issue Apr 10, 2024 · 3 comments
Open
2 tasks done

Mongoose not reconnecting after disconnection event #14517

elizatlawy opened this issue Apr 10, 2024 · 3 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Milestone

Comments

@elizatlawy
Copy link

elizatlawy commented Apr 10, 2024

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.10.1

Node.js version

14.18.3

MongoDB server version

5.0.9

Typescript version (if applicable)

No response

Description

My connection looks like this:

const OPTIONS = {
  useNewUrlParser: true,
  useUnifiedTopology: true
};

// Make `sanitizeFilter` true by default
mongoose.set('sanitizeFilter', true);
mongoose.set('strictQuery', true);

if (process.env.LOG_MONGO_QUERIES) {
  mongoose.set('debug', (collectionName, method, query, doc) => {
    logger.debug(`${collectionName}.${method}`, JSON.stringify(query), doc);
  });
}

const initMongooseConnection = () => {
  try {
    connection.on('connected', () => {
      logger.info(`Successfully connected to MongoDB`);
    });
    connection.on('reconnected', () => {
      logger.info(`Successfully reconnected to MongoDB`);
    });
    connection.on('error', error => {
      logger.error(`MongoDB encountered and error: ${error}`);
    });
    connection.on('connecting', () => {
      logger.info('MongoDB connecting...');
    });
    connection.once('open', () => {
      logger.info('MongoDB connection opened');
    });
    connection.on('disconnecting', () => {
      logger.info('MongoDB disconnecting...');
    });
    connection.on('disconnected', () => {
      logger.error('MongoDB disconnected');
    });
    connection.on('close', () => {
      logger.info('MongoDB connection closed');
    });
    mongoose.connect(MONGO_DB_CONNECTION_URI, OPTIONS);
  } catch (err) {
    logger.error(`Error connecting to mongoDB: ${err}`);
  }
};

If I restart my Mongodb I get the disconnected event but there is no reconnect attempt, I am able to reconnect only after restarting the Node.js server.

Steps to Reproduce

  1. Start the node.js server
  2. See that the Mongo connection is open
  3. Restart MongoDB
  4. disconnection e ent is fired
  5. No reconnect attempt

Expected Behavior

Mongoose should try to reconnect.

@vkarpov15 vkarpov15 added this to the 6.12.9 milestone Apr 16, 2024
@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label May 1, 2024
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented May 1, 2024

image

const mongoose = require('mongoose');

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
}

mongoose.connection.on('close', async () => {
  console.log('lost connection');
});

mongoose.connection.on('reconnect', () => console.log('reconnect'));

mongoose.connection.on('reconnected', () => console.log('reconnected'))

mongoose.connection.on('connected', () => console.log('connected'));

mongoose.connection.on('disconnected', () => console.log('disconnected'))

mongoose.connection.on('disconnecting', () => console.log('disconnecting'))

run();

@elizatlawy
Copy link
Author

@IslandRhythms - Did you use the same version I mentioned?

I tried and the issue is still reproduced with your simple test:
image

@IslandRhythms IslandRhythms added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels May 6, 2024
@IslandRhythms
Copy link
Collaborator

Does not occur for me on 6.10.1
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

3 participants