Mongoose doesn't shut down gracefully
Mongoose never lets the node event loop clear, even after disconnect is called. This stops node from exiting gracefully (i.e. without calling process.exit()) Other server modules allow node to shutdown after it's respective .close() or .shutdown() is called, including Restify, Express, etc. Mongoose, however, holds the application open.
This application never exits:
//Boilerplate
var mongoose = require('mongoose');
var db = mongoose.connection;
db.on('error', function(err) {
console.log('Mongoose connection error: ' + err);
});
//connect
mongoose.connect("localhost");
//disconnect
mongoose.disconnect();
on mobile right now, but can confirm. just a quick notice: ur snippet is synchronous. disconnecting at the end of a find callback produces same result though. are we missing sth ?
Which version of mongoose and node?
Node 10.22.
Mongoose 3.8.1
Sorry - back from a vacation.
Just logged in again today to try to troubleshoot and noticed that this is only happening when mongo isn't running. I believe that in the past it would hang node in all cases, including successful connections.
Here is an updated snippet, and a sample run:
var mongoose = require('mongoose');
var db = mongoose.connection;
db.on('error', function(err) {
console.log('Mongoose connection error: ' + err);
mongoose.disconnect();
});
db.once('open', function () {
console.log("Opened mongoose");
mongoose.disconnect();
});
db.once('close', function () {
console.log("Closed mongoose");
});
//Connect to the database
mongoose.connect("localhost");
Run:
ryansteckler]---[MobileRyan]---[test]--$ node app.js
Mongoose connection error: Error: failed to connect to [localhost:27017]
^C--[ryansteckler]---[MobileRyan]---[test]--$
Note the ctrl-c to end the app
I can confirm this.
This is not ideal in many cases.
Confirm on this side too. Node: 10.24 Mongoose: 3.8.15
Hmm that's strange, the program specified by @rsteckler 's original code exits for me successfully on 3.8.1, 3.8.15, and 3.8.20, with node 0.10.24 and node 0.10.30. Can one of y'all confirm that disconnect() works as expected now, and tell me what OS you're using? I've only tested on Mint and OSX.
I just tried this again on node version 0.10.30 and mongoose 3.8.20 and this doesn't appear to happen for myself anymore with @rsteckler 's code either
Node: 0.10.33 Mongoose: 3.8.20
Ran @rsteckler 's code and it keeps logging the error message. You have to stop the mongo server while the app is running:
var mongoose = require('mongoose');
var db = mongoose.connection;
db.on('error', function(err) {
console.log('Mongoose connection error: ' + err);
mongoose.disconnect();
});
db.once('open', function () {
//Kill the MongoDB server here
setTimeout(function() {
mongoose.disconnect();
},3000);
});
db.once('close', function () {
console.log("Closed mongoose");
});
//Connect to the database
mongoose.connect("localhost");
This might be useful for narrowing down the problem:
var mongoose = require('mongoose');
var os = require('os');
console.log('mongoose ' + mongoose.version);
console.log('mongodb ' + mongoose.mongo.version);
console.log('node ' + process.version);
console.log('os.type ' + os.type());
console.log('os.platform ' + os.platform());
console.log('os.arch ' + os.arch());
console.log('os.release ' + os.release());
var db = mongoose.connection;
db.on('error', function(err) {
console.log('Mongoose connection error: ' + err);
mongoose.disconnect();
});
db.once('open', function () {
//Kill the MongoDB server here
setTimeout(function() {
mongoose.disconnect();
},3000);
});
db.once('close', function () {
console.log("Closed mongoose");
});
//Connect to the database
mongoose.connect("localhost");
Unsurprisingly I found that 3.6.17 was causing the problem, while 3.8.20+ was not, regardless of OS, which was the reason why I first reported having the issue myself.
Hmmm @jjn2009 the above code shuts down properly for me on 3.6.17 as well. But if it works for you on 3.8.20 I'd recommend upgrading, the 3.6.x branch hasn't been maintained for a while.
This is still an issue for me. Mongoose 5.0.4, Node 4.2.1.
After running disconnect(), the process keeps running. Is this the same issue as https://github.com/Automattic/mongoose/issues/3358?
@jbach are you running against a replica set? Or a standalone?
I have the similar issue. Using Node.JS 6.3.0 and Mongoose 4.5.3 I couldn't catch the case when I stop mongod process - the only result is the exit from a running app. I tried server options (auto_reconnect etc.) and handlers for disconnected, close etc.
Please provide code samples
This doesn't seem to be fixed. I am starting up a DB connection in tests and the test process is never ending. I am calling mongoose.disconnect() in my teardown but it doesn't exit. The callback function is never run.
@Smilebags can you please open up a separate issue with code samples please?