Having MongoNetworkError: connect ETIMEDOUT

Hello,

We have been using mongo:6 official build image from mongodb in docker hub. We are recently upgraded our node js driver and our database server to mongo:6.0.

Our application seems to be working fine with database connections, but sometimes we are seeing an error of

MongoNetworkError: read ECONNRESET 

    at connectionFailureError (C:\app\node_modules\mongodb\lib\cmap\connect.js:357:20) 

    at TLSSocket.<anonymous> (C:\app\node_modules\mongodb\lib\cmap\connect.js:268:44) 

    at Object.onceWrapper (node:events:633:26) 

    at TLSSocket.emit (node:events:518:28) 

    at emitErrorNT (node:internal/streams/destroy:169:8) 

    at emitErrorCloseNT (node:internal/streams/destroy:128:3) 

    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { 

  connectionGeneration: 2, 

  [Symbol(errorLabels)]: Set(1) { 'ResetPool' }, 

  [cause]: Error: read ECONNRESET 

      at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) { 

    errno: -4077, 

    code: 'ECONNRESET', 

    syscall: 'read' 

  } 

} 

Does any one knows why this is occurring. We have the mongodb server deployed in aks cluster, within a service. And we use the database using native-mongodb-driver for nodejs, we are using node version 20.11.1.

When large database operations are done asynchronously, we get the errors sometimes. As per the docs, we are setting the socket timeout values as 0, so no socket timeout errors should occur for connections but we see this error in logs. We suspect that something is wrong with the docker image that we are using, please advise on how to fix this?

I don’t understand. From what did you upgrade to what? You wrote you used mongo:6 and not mongo:6.0. Those point to the same image.

Please, share how you set the timeout and what docs you are referring to.

Hi,

We are using mongodb server mongo:6.0. When we upgrade the native nodejs driver for mongodb from 5.3 to 6.4, we run into MongoNetworkErrors.

node version: 20.11.1, As per the official doc of mongodb, we initialize our client instance as following.
We are using MongoClient to connect to the Azure AKS pod in a cluster that has mongodb server mongo:6.0 image deployed. We are using connection pools and application wide single connection. We are using following object for making connections,

const URL = `mongodb://db1.domain.com,db2.domain.com,db3.domain.com`;
const _mongodb = new MongoClient(URL, {
      retryWrites: true,
      w: 'majority',
      replicaSet: 'rs0',
      auth: {
        username: process.env.MONGODB_USER,
        password: process.env.MONGODB_PASS
      },
      tls: true,
      tlsCertificateKeyFile: "file for tls certificate key",
      tlsCAFile: "certificate file here"
      retryReads: true,
      retryWrites: true,
      socketTimeoutMS: 0,
      connectTimeoutMS: 0,
      monitorCommands: true,
      heartbeatFrequencyMS: 1000,
      localThresholdMS: 1000,
      maxPoolSize: 100,
    });
  }
    console.log(`Attempting to connect to MongoDB: ${URL}`);
    const start = new Date();
    const client = await _mongodb.connect();
    const durationMs = new Date().valueOf() - start.valueOf();
    _database = _mongodb.db("v1"); //database version 1 in use
    const MONGO_CLIENT_EVENTS = [ "connectionClosed",  "error", "timeout", "close"];
    //Adding connection closed event listners
    MONGO_CLIENT_EVENTS.forEach(event => {
      client.on(event, (e) => {
        console.log('event', e);
      })
    });

Our application does thousands of database operations. The connection and the database operations were successful and worked in production environment without the network errors previously.

But When we upgraded the mongodb npm package that we use for database communications to version 6.4, the following error is coming up randomly.

MongoNetworkError: read ECONNRESET 

    at connectionFailureError (C:\app\node_modules\mongodb\lib\cmap\connect.js:357:20) 

    at TLSSocket.<anonymous> (C:\app\node_modules\mongodb\lib\cmap\connect.js:268:44) 

    at Object.onceWrapper (node:events:633:26) 

    at TLSSocket.emit (node:events:518:28) 

    at emitErrorNT (node:internal/streams/destroy:169:8) 

    at emitErrorCloseNT (node:internal/streams/destroy:128:3) 

    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { 

  connectionGeneration: 2, 

  [Symbol(errorLabels)]: Set(1) { 'ResetPool' }, 

  [cause]: Error: read ECONNRESET 

      at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) { 

    errno: -4077, 

    code: 'ECONNRESET', 

    syscall: 'read' 

  } 

} 

We have set the socketTimeoutMS: 0, and connectTimeoutMS: 0. So the socket won’t have timeout but the error indicates that mongodb connection is timed out.
This issue only occurs in mongodb Node.Js 6.0 and above.

Does anyone know about this issue/fixes?

We have tries to tune the connection options, 
      retryReads: true,
      retryWrites: true,
      socketTimeoutMS: 0,
      connectTimeoutMS: 0,
      monitorCommands: true,
      heartbeatFrequencyMS: 1000,
      localThresholdMS: 1000,
      maxPoolSize: 100

We were expecting that we won’t see any MongoNetworkError: read ECONNRESET errors coming from mongodb when we upgrade the driver from 5.3 to 6.4.

Reaching out the community to see if anyone knows about any configs that we can set to mitigate such issues with mongo:6.0 or the native mongo driver from nodejs?

Is the MongoDB connection working without Docker?

Yes it works without docker as well as in docker image of the application. All operations are getting executed but sometimes the error get’s thrown without any specifics.