-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Comments
We've tracked the issue down to the use of the When we remove this option it works in the latest Mongoose version 5.7.3.
|
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: mongoose/test/connection.test.js Lines 183 to 211 in cdfb507
What docker image are you using? Are you using docker compose? Also, does this issue happen without Docker? |
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. |
@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. |
@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
On starting the app I get following output:
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.
Then I run the app changing only the cocnnection string to:
Then I stopped the container: And got the follwoing output: And again I started the container: Output:
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. |
@igorz24 did you wait for 30 seconds? With |
@vkarpov15 thank you so much, the @igorz24 also tried your code with mongo atlas cluster, Output:
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! |
@vkarpov15 You're right, I forgot to wait. @xinyangyuan Try setting: 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. |
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!'); });
The text was updated successfully, but these errors were encountered: