I have a health check endpoint in my application which simply pings a mongo reserve cluster. But for some reason I frequently get this error log on the health check endpoint.
MongoNetworkError: AggregateError has an empty errors array. Please check the cause property for more information. [cause]: AggregateError at emitErrorCloseNT (node:internal/streams/destroy:116:3) at emitErrorNT (node:internal/streams/destroy:151:8)
I’m also getting other errors as
PoolClearedError [MongoPoolClearedError]: Connection pool for <server__> was cleared because another operation failed with: "connection <monitor> to <some IP __> closed"
PoolClearedError [MongoPoolClearedError]: Connection pool for <server__> was cleared because another operation failed with: "AggregateError has an empty errors array. Please check the cause property for more information."
I don’t understand why I’m getting this error, I’m not even using an aggregation query and technically if there was a connection error mongoDB throws Network timeout errors. Can someone please help me understand why this error is coming and how to fix this.
Below is the ping code
const { db } = await mongoDbClient.connectToDatabase2();
const result = db.command({ ping: 1 });
Below is the mongoclient class
import { Db, MongoClient, MongoClientOptions } from 'mongodb';
export default class MongoDbClient {
cachedClient2: MongoClient | undefined;
cachedDb2: Db | undefined;
constructor() {
this.connectToDatabase2();
}
async connectToDatabase2() {
if (this.cachedClient2 && this.cachedDb2) {
return {
client: this.cachedClient2,
db: this.cachedDb2,
};
}
const opts: MongoClientOptions = {
minPoolSize: 10,
maxPoolSize: 50,
};
const client = new MongoClient(process.env.MONGODB_URI2, opts);
await client.connect();
const db = client.db(process.env.MONGODB_DB);
this.cachedClient2 = client;
this.cachedDb2 = db;
return {
client: this.cachedClient2,
db: this.cachedDb2,
};
}
}
2
Answers
Seems like it was an issue with my node servers running out of cpu and memory limits due to a recurring dependency call. Eventually due to which the pods were restarting and abruptly dropping connections.
It’s not Mongodb aggregation error. emitErrorCloseNT is from TCP/socket space – something like ECONNREFUSED.
Check Mongo side if you hit limit of open connections.
Apart from that you have some broken promises – you are using async connectToDatabase2 in sync constructor.