Skip to content

no disconnection after a re-connection #6249

Closed
@tomgrossman

Description

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

What is the current behavior?
After the connection is re-connected and DB goes down again, there is no disconnection event and the readyState stays in 1 - connected

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

  1. start the DB
  2. start the app - make sure the readyState is 1
  3. stop the DB
  4. wait for disconnection - readyState is 0
  5. start the DB
  6. wait for reconnection - readyState is 1
  7. stop the DB
  8. the connection never gets a disconnected event and the readyState is 1

using the below script, the console output is:

connected
open
createConnection
ping: true readyState: 1
ping: true readyState: 1
ping: false readyState: 1
disconnected
ping: false readyState: 0
ping: false readyState: 0
ping: false readyState: 0
ping: false readyState: 0
ping: false readyState: 0
ping: true readyState: 0
ping: true readyState: 0
ping: true readyState: 0
ping: true readyState: 0
connected
reconnected
ping: true readyState: 1
ping: true readyState: 1
ping: false readyState: 1
ping: false readyState: 1
ping: false readyState: 1
ping: false readyState: 1
ping: false readyState: 1
ping: false readyState: 1
ping: false readyState: 1
ping: false readyState: 1
...

you can see that the ping to the server turns to false but there is no disconnection.
the script that is used:

const tcpp          = require('tcp-ping');
const Promise       = require('bluebird');
const mongoose      = require('mongoose');
mongoose.Promise = Promise;

const host = 'localhost';
const port = '2244';
const DB_URL = `mongodb://${host}:${port}/test_database`;
const ConnectionOptions = {
    promiseLibrary: Promise,
    reconnectTries: Number.MAX_VALUE
};

const DbConnection = mongoose.createConnection(DB_URL, ConnectionOptions, (err) => {
    console.log('createConnection');
    if (err) {
        console.log('createConnection', err.message);
    }
});

DbConnection.on('error', function (error) {
    console.log('error: ', error.message)
});

DbConnection.on('disconnected', function () {
    console.log('disconnected');
});

DbConnection.on('close', function () {
    console.log('close');
});

DbConnection.on('open', function () {
    console.log('open');
});

DbConnection.on('connected', function () {
    console.log('connected');
});

DbConnection.on('connecting', function () {
    console.log('connecting');
});

DbConnection.on('reconnected', function () {
    console.log('reconnected');
});

process.on('unhandledRejection', function (err) {
    if ('MongoError' === err.name) {
        console.log('unhandledRejection: ' + err.message);
    }
});

setInterval(() => {
    tcpp.probe(host, port, (err, available) => {
        console.log('ping:', available, 'readyState:', DbConnection.readyState);
    })
}, 3000);

module.exports = DbConnection;

What is the expected behavior?
When there is a connection loss, there should be a disconnected event and the readyState should update accordingly, like in the first disconnection.
I've tested almost the same script with the latest native driver using the isConnected function instead of the readyState and it works fine there. whenever the ping is gone, the isConnected function returns false.

Please mention your node.js, mongoose and MongoDB version.
node.js - 8.10.0
mongoose - 5.0.10
MongoDB - 3.4.10

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.underlying library issueThis issue is a bug with an underlying library, like the MongoDB driver or mongodb-core

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions