{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/sessions.ts#L1075-L1111","documentation":"Thrown by the ServerSessionPool constructor when the client argument is null or undefined. ServerSessionPool is an @internal class used by the driver to manage logical server sessions; users do not construct it directly. The guard prevents the driver from later dereferencing a null topology. It is a MongoRuntimeError.","triggerScenarios":"Internally constructing new ServerSessionPool(undefined) or new ServerSessionPool(null); reaching the constructor via a test stub or subclass that passes no client; a MongoClient that failed to fully initialize before the pool was built.","commonSituations":"Driver-internal bugs; unit tests that instantiate ServerSessionPool without a real client; custom MongoClient subclasses or mocks that break the expected initialization order.","solutions":["Do not construct ServerSessionPool yourself; obtain sessions via client.startSession() which uses the client's internal pool.","If writing driver internals/tests, pass a fully-constructed MongoClient instance to the constructor.","Ensure the MongoClient has finished connecting/topology init before the pool is accessed."],"exampleFix":"// before (internal/test code)\nconst pool = new ServerSessionPool(undefined); // throws\n\n// after\nconst client = new MongoClient(uri);\nawait client.connect();\nconst pool = new ServerSessionPool(client);","handlingStrategy":"validation","validationCode":"// internal only - do not construct ServerSessionPool directly\nif (client != null) {\n  const pool = new ServerSessionPool(client);\n}\n// users: just call client.startSession()","typeGuard":"import { MongoClient } from 'mongodb';\nfunction isMongoClient(v: unknown): v is MongoClient {\n  return v instanceof MongoClient;\n}","tryCatchPattern":null,"preventionTips":["Never instantiate ServerSessionPool in application code; use client.startSession().","In driver tests, always pass a fully-constructed MongoClient.","Ensure client.connect() resolved before any session-related internal access."],"tags":["sessions","internal","runtime","initialization"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}