Restarting After Spurious Connection Timeouts (NodeJS Client)

Long time MongoDB user here, first time posting on this forum.

I have several NodeJS servers that maintain long-running connections to our MongoDB backend. From time to time I’ve noticed that they seem to drop this connection, throwing a MongoServerSelection error. I assume that perhaps this is due to load, or network fluctuations, or whatever.

Honestly, I don’t really care. What I want to happen is for the service to exit so that Kubernetes can restart it. But instead it just seems to hang until I notice and restart the container manually. I have process.on handlers for both “uncaughtException” and “unhandledRejection” which call process.exit, but these don’t seem to be working.

So what’s the best way in a node.js app to either (1) reconnect across timeouts (the option to do this seems to have been removed recently) or (2) just exit the server in this situation? I’m using the latest version of the NodeJS MongoDB library.

As an update, I figured out at least part of what the issue was here, in that my driver script wasn’t restarted properly.

However, I’m still seeing these spurious connection drops, even in my local development setup with a local database where they should be no connectivity issues. Is there an option to handle these in the driver through a retry? That would be nice.

Hi @Geoffrey_Challen have you found a solution? We’re experiencing the same issues.

thanks