MongoDB "connection pool ... cleared" / pool errors in CI
The driver cleared its connection pool for a server after a network or handshake failure, then reconnects. Occasional clears during a blip are normal; frequent clears in CI point to an unstable server or too many concurrent connections.
What this error means
Logs show "connection pool for <host> was cleared because of another operation failure" or "connection pool paused", sometimes alongside slow or failing queries.
[connection] connection pool for 127.0.0.1:27017 was cleared
because of another operation failure
[connection] connection pool for 127.0.0.1:27017 is now pausedCommon causes
A transient server error triggers a pool reset
When one operation fails at the network layer, the driver clears the pool for that server to drop stale sockets, then reconnects.
Too many concurrent connections for a small test mongod
A parallel test suite opens more connections than maxPoolSize or the server allows, causing contention and clears.
How to fix it
Reuse one client and bound the pool
- Create a single MongoClient for the whole test run instead of one per test.
- Set a reasonable
maxPoolSizefor the runner size. - Ensure tests do not leak clients that keep opening connections.
const client = new MongoClient(process.env.MONGODB_URI, {
maxPoolSize: 10,
serverSelectionTimeoutMS: 10000,
});Stabilize the server before load
Wait for the mongo healthcheck and give the container enough memory so it does not stall under the suite.
How to prevent it
- Share one client across the suite; do not open a client per test.
- Set maxPoolSize appropriate to the runner and server.
- Connect only after the server is healthy.