Const error = this.closed ? new errors_1.PoolClosedError(this) : new errors_1.PoolClearedError(this);

When I am doing the insert to Mongodb in the while loop, the error suddenly prompt out:

                const error = this.closed ? new errors_1.PoolClosedError(this) : new errors_1.PoolClearedError(this);

PoolClearedError [MongoPoolClearedError]: Connection pool for was cleared because another operation failed with: "connection <monitor> to closed"
    at ConnectionPool.processWaitQueue (/Users/tung/project/sk/mongo-db-embedded/node_modules/mongodb/lib/cmap/connection_pool.js:520:82)
    at ConnectionPool.clear (/Users/tung/project/sk/mongo-db-embedded/node_modules/mongodb/lib/cmap/connection_pool.js:251:14)
    at updateServers (/Users/tung/project/sk/mongo-db-embedded/node_modules/mongodb/lib/sdam/topology.js:461:29)
    at Topology.serverUpdateHandler (/Users/tung/project/sk/mongo-db-embedded/node_modules/mongodb/lib/sdam/topology.js:332:9)
    at Server.<anonymous> (/Users/tung/project/sk/mongo-db-embedded/node_modules/mongodb/lib/sdam/topology.js:444:77)
    at Server.emit (node:events:513:28)
    at markServerUnknown (/Users/tung/project/sk/mongo-db-embedded/node_modules/mongodb/lib/sdam/server.js:286:12)
    at Monitor.<anonymous> (/Users/tung/project/sk/mongo-db-embedded/node_modules/mongodb/lib/sdam/server.js:58:46)
    at Monitor.emit (node:events:513:28)
    at failureHandler (/Users/tung/project/sk/mongo-db-embedded/node_modules/mongodb/lib/sdam/monitor.js:153:17) {
  address: '',
  [Symbol(errorLabels)]: Set(1) { 'RetryableWriteError' }

I have no idea where the error come out, and why?


Welcome to the MongoDB Community!

The PoolClearedError can occur due to two common scenarios:

  • Intermittent network outages that cause the driver to lose connectivity.
  • Re-election of the PRIMARY node in your cluster, which leads to lost connections as the topology changes.

Essentially, this error happens when the driver believes the server associated with a connection pool is no longer available. The pool gets cleared and closed so that operations waiting for a connection can retry on a different server.

To address this:

  • Enable retryability in your application if you haven’t already. This allows operations to retry seamlessly on another server.
  • Look into SDAM (Server Discovery and Monitoring) monitoring to understand why servers are being marked unknown.
  • Similarly, enable connection-level monitoring to see network errors and events.
  • Check the cluster’s logs for elections, failovers, and step-downs that could be disrupting connectivity.
  • Ensure the cluster itself is healthy and members are communicating properly.

Let me know if you have any other questions!


For me this happened when I forgot to put await before the collection.insertOne(obj) command. Any reason as to why this was happening?

1 Like