mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Mongoose doesn't shut down gracefully

Open rsteckler opened this issue 13 years ago • 18 comments

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();

rsteckler avatar Nov 27 '13 00:11 rsteckler

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 ?

maxwellium avatar Nov 27 '13 13:11 maxwellium

Which version of mongoose and node?

aheckmann avatar Nov 27 '13 17:11 aheckmann

Node 10.22.

Mongoose 3.8.1

rsteckler avatar Nov 27 '13 20:11 rsteckler

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

rsteckler avatar Dec 02 '13 20:12 rsteckler

I can confirm this.

This is not ideal in many cases.

jjn2009 avatar Sep 07 '14 22:09 jjn2009

Confirm on this side too. Node: 10.24 Mongoose: 3.8.15

nabito avatar Nov 17 '14 12:11 nabito

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.

vkarpov15 avatar Dec 05 '14 22:12 vkarpov15

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

jjn2009 avatar Dec 14 '14 04:12 jjn2009

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");

metasansana avatar Dec 16 '14 02:12 metasansana

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.

jjn2009 avatar Jan 10 '15 01:01 jjn2009

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.

vkarpov15 avatar Feb 04 '15 19:02 vkarpov15

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 avatar Dec 02 '15 20:12 jbach

@jbach are you running against a replica set? Or a standalone?

vkarpov15 avatar Dec 03 '15 17:12 vkarpov15

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.

pkantsedalov avatar Jul 26 '16 12:07 pkantsedalov

Please provide code samples

vkarpov15 avatar Jul 30 '16 14:07 vkarpov15

vkarpov15 it's here

pkantsedalov avatar Jul 30 '16 14:07 pkantsedalov

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 avatar Dec 28 '17 11:12 Smilebags

@Smilebags can you please open up a separate issue with code samples please?

vkarpov15 avatar Dec 28 '17 16:12 vkarpov15