Clean shutdown of express.js + mongodb server (using node.js)

I have node.js webserver using express.js and mongodb as a datastore. This server is being controlled by runit, and I am trying to implement a way to gracefully shut down the server.

I am implementing signal handlers for SIGINT and SIGTERM, and I am aware that you can stop listening for new connections by calling .close() on the object returned by createServer(). So far so good.

However, even when no more requests are forthcoming, there may be a number of requests already in the system that needs to finish before I can close the database.

I am using a mongodb ReplicaSet, and I figure that if I just call db.close() right away, that some of these requests may fail in some manner. Is there some way to close the database in a way that allows pending database queries to finish or do I have to manually have a +/- counter to see how many "active" queries are pending, and then wait to shutdown until it reaches 0?

You would have to manually ensure that you are in a clean state to shut down as the driver does not wait flush out the remaining operations before shutting down.

Feel free to log a ticket for the feature on

https://github.com/mongodb/node-mongodb-native/issues/