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

No disconnected event when MongoDB docker container closed v5.7.3 #8212

Closed
shaneguayCanopy opened this issue Oct 3, 2019 · 8 comments
Closed
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@shaneguayCanopy
Copy link

This is an interesting issue in that the 'disconnected' event is never triggered when the MongoDB is brought down. We are running MongoDB through a Docker container and running a local node.js file.

We are using the latest Mongoose version 5.7.3.

When we tried an older version 5.0.12 it works. We found a similar issue that was described as being fixed in 5.0.12 from this ticket ("error event not triggered #6131") and indeed in 5.0.12 we did properly receive the 'disconnected' event when we closed the MongoDB container.

So it seems this bug has regressed and is back in latest version

mongoose.connection.on('disconnected', () => { console.log('DISCONNECTED!'); });

@shaneguayCanopy
Copy link
Author

shaneguayCanopy commented Oct 3, 2019

We've tracked the issue down to the use of the useUnifiedTopology: true setting in the connect method.

When we remove this option it works in the latest Mongoose version 5.7.3.

mongoose.connect(dbConnectionString, {
        useNewUrlParser: true,
        useFindAndModify: false,
        autoReconnect: false,
        autoIndex: false,
        //useUnifiedTopology: true,
        bufferMaxEntries: 0
    }).then

@vkarpov15 vkarpov15 added this to the 5.7.4 milestone Oct 7, 2019
@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 Oct 7, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.7.4, 5.7.5 Oct 7, 2019
@vkarpov15
Copy link
Collaborator

vkarpov15 commented Oct 11, 2019

I'm unable to repro this. Below script prints out 'disconnected' in 5.7.3 when I shut down my local mongod instance.

'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

mongoose.set('debug', true);

run().catch(error => console.log(error));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    autoReconnect: false,
    bufferMaxEntries: 0
  });

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

  console.log('Connected');
}

We also have test cases for this, and they pass on travis:

it('disconnected (gh-5498) (gh-5524)', function(done) {
this.timeout(60000);
let numConnected = 0;
let numDisconnected = 0;
let numReconnected = 0;
let numReconnect = 0;
let numClose = 0;
const conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest', {
useNewUrlParser: true,
useUnifiedTopology: true
});
conn.on('connected', function() {
++numConnected;
});
conn.on('disconnected', function() {
++numDisconnected;
});
conn.on('reconnect', function() {
++numReconnect;
});
// Same as `reconnect`, just for backwards compat
conn.on('reconnected', function() {
++numReconnected;
});
conn.on('close', function() {
++numClose;
});
.

What docker image are you using? Are you using docker compose? Also, does this issue happen without Docker?

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Oct 11, 2019
@vkarpov15 vkarpov15 removed this from the 5.7.5 milestone Oct 11, 2019
@xinyangyuan
Copy link

The connection events also not firing when running node locally (mongoose version 5.7.5) and using MongoDB atlas. Simulate connection lost by manual computer internet connection, and none of the following events would fire:

mongoose.connection.on('disconnected', function(e) {
  console.log('Disconnected: ' + e);
});
mongoose.connection.on('error', function(e) {
  console.log('Error: ' + e);
});
mongoose.connection.on('timeout', function(e) {
  console.log('Timeout: ' + e);
});

ps: not sure manual disconnect internet is the right way to simulate, but also experience connection lost without any error when the nodejs is hosted in ec2.

@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Oct 25, 2019
@vkarpov15 vkarpov15 added this to the 5.7.8 milestone Oct 25, 2019
@vkarpov15
Copy link
Collaborator

@xinyangyuan the below script fires 'disconnected' correctly for me when I turn off wifi on my macbook, it just takes maybe 30 seconds or so.

'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

run().catch(error => console.log(error));

async function run() {
  await mongoose.connect('mongodb+srv://USER:PASS@cluster0-OMITTED.mongodb.net/test?retryWrites=true&w=majority', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    autoReconnect: false,
    bufferMaxEntries: 0
  });
mongoose.connection.on('disconnected', function(e) {
  console.log('Disconnected: ' + e);
});
mongoose.connection.on('error', function(e) {
  console.log('Error: ' + e);
});
mongoose.connection.on('timeout', function(e) {
  console.log('Timeout: ' + e);
});

  console.log('Connected');
}

Please make sure you give it some time to fire the disconnected event, it won't happen immediately.

@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Oct 26, 2019
@vkarpov15 vkarpov15 removed this from the 5.7.8 milestone Oct 26, 2019
@igorz24
Copy link

igorz24 commented Oct 31, 2019

@shaneguayCanopy I had problems with some other thing and came here looking for answer. Even though that thread happened to be unrelated to what I was having troubles with, I reproduced in the process both local and remote situations with mongoose 5.7.7

  1. Connecting to Atlas M0 Cluster with MongoDB 4.0
async function runDB() {
  await mongoose.connect('mongodb+srv://USER:PASSWORD@cluster0-biziw.mongodb.net/DB_NAME?retryWrites=true&w=majority', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    autoReconnect: false,
    bufferMaxEntries: 0
  });
}

const db = mongoose.connection;

db.on('connecting', () => {
  console.log('connecting to MongoDB...');
});
db.on('connected', () => {
  console.log('MongoDB connected!');
});
db.on('reconnectedFailed', () => {
  console.log('reconnectedFailed');
});
db.on('reconnected', () => {
  console.log('MongoDB reconnected!');
});
db.on('disconnecting', () => {
  console.log('disconnecting MongoDB...');
});
db.on('disconnected', () => {
  console.log('MongoDB disconnected!');
});
db.on('error', (error: Error) => {
  console.log('error');
});
db.on('timeout', () => {
  console.log('timeout');
});
db.on('open', () => {
  console.log('MongoDB connection opened!');
});
db.on('close', () => {
  console.log('MongoDB connection closed!');
});

runDB().catch(error => console.log(error));

On starting the app I get following output:

connecting to MongoDB...
MongoDB connected!
MongoDB connection opened!

Then I disconnected my laptop from the Internet and didn't get any disconnected event (readyState == 1), what is more interesting when I queried for data during that time, the request was waiting to be resolved and after I turned on my wifi again, no event was fired and the request got processed after a while, so it seems like the connection was never broken in the first place, which is weird.


  1. Using local container with image mongo:4.0.4
    I wanted to make sure if that behavior would be similar if I connected to local DB, so I run the container:
    docker run -d -p 27017-27019:27017-27019 --name mongodb mongo:4.0.4

Then I run the app changing only the cocnnection string to:
'mongodb://127.0.0.1:27017/test?retryWrites=true&w=majority'
and got the following output, as expected:

connecting to MongoDB...
MongoDB connected!
MongoDB connection opened!

Then I stopped the container:
docker stop mongodb

And got the follwoing output:
MongoDB disconnected!

And again I started the container:
docker container start mongodb

Output:

MongoDB connected!
MongoDB reconnected!

So while using the local container everything works as expected.

It's kind of weird that these 2 situations give different results, but maybe it's because of the way the internet connection is being broken (even though I think it shouldn't matter).

I didn't have time to dive in into this and find the reason why these situations behave differently, but I hope the reproduction process may be helpful to you.
Cheers!

@vkarpov15
Copy link
Collaborator

@igorz24 did you wait for 30 seconds? With useUnifiedTopology, you won't get a 'disconnected' error until then.

@xinyangyuan
Copy link

@vkarpov15 thank you so much, the disconnected does trigger for MongoAtlas cluster connection when I close wifi connection for 30 seconds (mongoose 5.7.8).

@igorz24 also tried your code with mongo atlas cluster, disconnected was triggered as well, but reconnected was not fired after I turn the wifi back on.

Output:

connecting to MongoDB...
MongoDB connected!
MongoDB connection opened!

# after internet disconnected for 30sec
MongoDB disconnected!

What connection options should I pass to the mongoose, if I want the mongo driver to retry connection? Have tried to change autoRecoonect and reconnectTries, but it does not seem to work:

async function run() {
  await mongoose.connect('mongodb+srv://xxx', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    bufferMaxEntries: 0,
    autoReconnect: true,
    reconnectTries: Number.MAX_VALUE
  });
  console.log('Connected');
}

Thanks!

@igorz24
Copy link

igorz24 commented Nov 8, 2019

@vkarpov15 You're right, I forgot to wait.

@xinyangyuan Try setting:
useUnifiedTopology: false

It works for me when I do. I think in general it shouldn't be the case, but right now there is a bug with Mongo driver for Node and it seems like it might be connected.
See the issue -> #8180

@Automattic Automattic locked as resolved and limited conversation to collaborators Nov 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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

4 participants