{"record":{"id":"9dff9944b1138092","repo":"mongodb/node-mongodb-native","slug":"serversessionpool-requires-a-mongoclient","errorCode":null,"errorMessage":"ServerSessionPool requires a MongoClient","messagePattern":"ServerSessionPool requires a MongoClient","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/sessions.ts","lineNumber":1093,"sourceCode":"      ((calculateDurationInMs(this.lastUse) % 86400000) % 3600000) / 60000\n    );\n\n    return idleTimeMinutes > sessionTimeoutMinutes - 1;\n  }\n}\n\n/**\n * Maintains a pool of Server Sessions.\n * For internal use only\n * @internal\n */\nexport class ServerSessionPool {\n  client: MongoClient;\n  sessions: List<ServerSession>;\n\n  constructor(client: MongoClient) {\n    if (client == null) {\n      throw new MongoRuntimeError('ServerSessionPool requires a MongoClient');\n    }\n\n    this.client = client;\n    this.sessions = new List<ServerSession>();\n  }\n\n  /**\n   * Acquire a Server Session from the pool.\n   * Iterates through each session in the pool, removing any stale sessions\n   * along the way. The first non-stale session found is removed from the\n   * pool and returned. If no non-stale session is found, a new ServerSession is created.\n   */\n  acquire(): ServerSession {\n    const sessionTimeoutMinutes = this.client.topology?.logicalSessionTimeoutMinutes ?? 10;\n\n    let session: ServerSession | null = null;\n\n    // Try to obtain from session pool","sourceCodeStart":1075,"sourceCodeEnd":1111,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/sessions.ts#L1075-L1111","documentation":"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()`.","triggerScenarios":"Direct `new ServerSessionPool(null)`; reflection or fakes that bypass the MongoClient; coupling tests to internal constructors.","commonSituations":"Unit-test doubles that instantiate the pool directly; forks or internal-path imports; outdated code targeting an older driver API.","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."],"exampleFix":"// before\nconst pool = new ServerSessionPool(null);\n\n// after\nconst client = new MongoClient(uri);\nconst session = client.startSession();","handlingStrategy":"validation","validationCode":"import { MongoClient } from 'mongodb';\n\nfunction assertClient(client) {\n  if (!(client instanceof MongoClient)) {\n    throw new TypeError('a MongoClient instance is required');\n  }\n}","typeGuard":"import { MongoClient } from 'mongodb';\nfunction isMongoClient(v) {\n  return v instanceof MongoClient;\n}","tryCatchPattern":null,"preventionTips":["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."],"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"}