Skip to content
Latchkey

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.

node
[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 paused

Common 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

  1. Create a single MongoClient for the whole test run instead of one per test.
  2. Set a reasonable maxPoolSize for the runner size.
  3. Ensure tests do not leak clients that keep opening connections.
db.js
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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →