{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/sessions.ts#L155-L191","documentation":"The `ClientSession` constructor requires `sessionPool` to be a `ServerSessionPool` instance (sessions.ts:171). The server session pool manages reusable server-side logical sessions; without it the client cannot acquire or return sessions. This is an internal invariant — `startSession()` always supplies a valid pool.","triggerScenarios":"Manually constructing ClientSession with a null or wrong-typed pool; passing a mock object that is not a ServerSessionPool; an internal wiring mistake.","commonSituations":"Test doubles that substitute the pool; refactors that bypass `client.startSession()`.","solutions":["Obtain sessions via `client.startSession()` which provides the correct internal pool.","In tests, mock at the MongoClient level rather than replacing the session pool.","If writing internal tooling, source the pool from `client.s.sessionPool`."],"exampleFix":"// before\nconst session = new ClientSession(client, {} as any, {});\n// after\nconst session = client.startSession();","handlingStrategy":"validation","validationCode":null,"typeGuard":"function isServerSessionPool(p) {\n  return p instanceof ServerSessionPool;\n}","tryCatchPattern":null,"preventionTips":["Always obtain sessions via client.startSession().","Mock at the MongoClient boundary, not the session pool.","Source internal pools from client.s.sessionPool."],"tags":["sessions","internal"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}