mongodb/node-mongodb-native · error · MongoRuntimeError

ServerSessionPool requires a MongoClient

Error message

ServerSessionPool requires a MongoClient

What it means

The internal `ServerSessionPool` constructor (sessions.ts:1081, `@internal`) requires a MongoClient; if `client == null` it throws MongoRuntimeError. Application code never constructs ServerSessionPool directly — the MongoClient creates one and uses it for `startSession()`.

Solutions

  1. Never construct ServerSessionPool; let `new MongoClient(uri)` own it.
  2. In tests, mock at the `client.startSession` boundary rather than the internal pool.
  3. Drop internal-path imports (`src/sessions`) — they are not part of the public API.

Example fix

// before
const pool = new ServerSessionPool(null);

// after
const client = new MongoClient(uri);
const session = client.startSession();
Defensive patterns

Strategy: validation

Validate before calling

import { MongoClient } from 'mongodb';

function assertClient(client) {
  if (!(client instanceof MongoClient)) {
    throw new TypeError('a MongoClient instance is required');
  }
}

Type guard

import { MongoClient } from 'mongodb';
function isMongoClient(v) {
  return v instanceof MongoClient;
}

Prevention

When it happens

Trigger: Direct `new ServerSessionPool(null)`; reflection or fakes that bypass the MongoClient; coupling tests to internal constructors.

Common situations: Unit-test doubles that instantiate the pool directly; forks or internal-path imports; outdated code targeting an older driver API.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/9dff9944b1138092. Report an issue: GitHub.

Appendix: source

Thrown at src/sessions.ts:1093

      ((calculateDurationInMs(this.lastUse) % 86400000) % 3600000) / 60000
    );

    return idleTimeMinutes > sessionTimeoutMinutes - 1;
  }
}

/**
 * Maintains a pool of Server Sessions.
 * For internal use only
 * @internal
 */
export class ServerSessionPool {
  client: MongoClient;
  sessions: List<ServerSession>;

  constructor(client: MongoClient) {
    if (client == null) {
      throw new MongoRuntimeError('ServerSessionPool requires a MongoClient');
    }

    this.client = client;
    this.sessions = new List<ServerSession>();
  }

  /**
   * Acquire a Server Session from the pool.
   * Iterates through each session in the pool, removing any stale sessions
   * along the way. The first non-stale session found is removed from the
   * pool and returned. If no non-stale session is found, a new ServerSession is created.
   */
  acquire(): ServerSession {
    const sessionTimeoutMinutes = this.client.topology?.logicalSessionTimeoutMinutes ?? 10;

    let session: ServerSession | null = null;

    // Try to obtain from session pool

View on GitHub (pinned to dce7939f86)