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
- Never construct ServerSessionPool; let `new MongoClient(uri)` own it.
- In tests, mock at the `client.startSession` boundary rather than the internal pool.
- 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
- Never construct ServerSessionPool directly; let MongoClient own it.
- Do not import internal classes from src paths.
- Mock sessions at the public startSession boundary in tests.
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
- ClientSession requires a MongoClient
- ClientSession requires a ServerSessionPool
- Argument "size" must be a non-negative number
- Cannot call abortTransaction after calling commitTransaction
- Cannot call abortTransaction twice
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 poolView on GitHub (pinned to dce7939f86)