{"record":{"id":"4a27a2873fd61995","repo":"mongodb/node-mongodb-native","slug":"clientsession-requires-a-serversessionpool","errorCode":null,"errorMessage":"ClientSession requires a ServerSessionPool","messagePattern":"ClientSession requires a ServerSessionPool","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/sessions.ts","lineNumber":173,"sourceCode":"   * @param clientOptions - Optional settings provided when creating a MongoClient\n   */\n  constructor(\n    client: MongoClient,\n    sessionPool: ServerSessionPool,\n    options: ClientSessionOptions,\n    clientOptions: MongoOptions\n  ) {\n    super();\n    this.on('error', noop);\n\n    if (client == null) {\n      // TODO(NODE-3483)\n      throw new MongoRuntimeError('ClientSession requires a MongoClient');\n    }\n\n    if (sessionPool == null || !(sessionPool instanceof ServerSessionPool)) {\n      // TODO(NODE-3483)\n      throw new MongoRuntimeError('ClientSession requires a ServerSessionPool');\n    }\n\n    options = options ?? {};\n\n    this.snapshotEnabled = options.snapshot === true;\n    if (options.causalConsistency === true && this.snapshotEnabled) {\n      throw new MongoInvalidArgumentError(\n        'Properties \"causalConsistency\" and \"snapshot\" are mutually exclusive'\n      );\n    }\n\n    this.client = client;\n    this.sessionPool = sessionPool;\n    this.hasEnded = false;\n    this.clientOptions = clientOptions;\n    this.timeoutMS = options.defaultTimeoutMS ?? client.s.options?.timeoutMS;\n\n    this.explicit = !!options.explicit;","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/sessions.ts#L155-L191","documentation":"The ClientSession constructor also requires a valid `ServerSessionPool` instance; if `sessionPool` is null or not `instanceof ServerSessionPool` it throws MongoRuntimeError. Like the client check this guards an internal constructor that application code never calls directly.","triggerScenarios":"Direct `new ClientSession(client, null, ...)` or passing a plain object/fake instead of a real ServerSessionPool; coupling tests to constructor internals.","commonSituations":"Unit-test fakes that substitute a hand-rolled pool object; forks of the driver; reflection-based construction.","solutions":["Use `client.startSession()` so the driver supplies its own pool.","In tests, mock the session at the public boundary instead of the constructor.","Avoid importing `ServerSessionPool` from internal paths; it is `@internal` and its shape can change."],"exampleFix":"// before\nconst session = new ClientSession(client, {}, {}, clientOptions);\n\n// after\nconst session = client.startSession();","handlingStrategy":"validation","validationCode":"// Never construct ClientSession; let the driver wire its own ServerSessionPool.\nconst session = client.startSession();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not import or instantiate `ServerSessionPool`; it is `@internal`.","Mock sessions at the `startSession` boundary in tests.","Avoid reflection that bypasses constructor argument checks."],"tags":["sessions","internal","validation","misuse"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}