mongodb/node-mongodb-native · error · MongoOperationTimeoutError

Timed out during server selection

Error message

Timed out during server selection

What it means

Thrown when CSOT (Client-Side Operation Timeout, `timeoutMS`) is enabled and server selection cannot find a suitable server within the operation's remaining time budget; it wraps the underlying `MongoServerSelectionError` as the cause (topology.ts:659). It signals that no server matching the read preference / write requirements became available before the deadline.

Source

Thrown at src/sdam/topology.ts:660

        if (
          this.client.mongoLogger?.willLog(
            MongoLoggableComponent.SERVER_SELECTION,
            SeverityLevel.DEBUG
          )
        ) {
          this.client.mongoLogger?.debug(
            MongoLoggableComponent.SERVER_SELECTION,
            new ServerSelectionFailedEvent(
              selector,
              this.description,
              timeoutError,
              options.operationName
            )
          );
        }

        if (options.timeoutContext?.csotEnabled()) {
          throw new MongoOperationTimeoutError('Timed out during server selection', {
            cause: timeoutError
          });
        }
        throw timeoutError;
      }
      // Other server selection error
      throw error;
    } finally {
      abortListener?.[kDispose]();
      if (!options.timeoutContext || options.timeoutContext.clearServerSelectionTimeout) {
        timeout?.clear();
      }
    }
  }
  /**
   * Update the internal TopologyDescription with a ServerDescription
   *
   * @param serverDescription - The server to update in the internal list of server descriptions

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Verify cluster connectivity and health (mongosh, network, DNS) and wait out any failover.
  2. Ensure a server exists for your read preference (use 'primary'/'primaryPreferred' if only a primary is available).
  3. Raise `timeoutMS` / `serverSelectionTimeoutMS` to survive transient outages.
  4. Confirm the hosts in the connection string are reachable and not firewalled.

Example fix

// before
const client = new MongoClient(uri, { timeoutMS: 1000 });
// after
const client = new MongoClient(uri, { timeoutMS: 30000 });
Defensive patterns

Strategy: retry

Try / catch

try {
  await coll.insertOne(doc, { timeoutMS: 5000 });
} catch (e) {
  if (e instanceof MongoOperationTimeoutError && /server selection/i.test(e.message)) {
    await new Promise(r => setTimeout(r, 1000));
    return retry();
  }
  throw e;
}

Prevention

When it happens

Trigger: An operation with `timeoutMS` set runs while the cluster is unreachable, has no primary (for writes), or has no secondaries (for secondary reads); network partition; all matching servers are deprioritized or unknown.

Common situations: Replica set with no elected primary during failover; secondary read preference but only a primary exists; firewall/DNS blocking the hosts; load-balanced mode with no ready service nodes; timeoutMS too small for a slow-starting cluster.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/4fdb2fbfb25d235d.json. Report an issue: GitHub.