{"id":"9605308f4cf45512","repo":"mongodb/node-mongodb-native","slug":"unexpected-null-serversession-for-an-ended-implici","errorCode":null,"errorMessage":"Unexpected null serverSession for an ended implicit session","messagePattern":"Unexpected null serverSession for an ended implicit session","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/sessions.ts","lineNumber":221,"sourceCode":"    this.operationTime = undefined;\n    this.owner = options.owner;\n    this.defaultTransactionOptions = { ...options.defaultTransactionOptions };\n    this.transaction = new Transaction();\n  }\n\n  /** The server id associated with this session */\n  get id(): ServerSessionId | undefined {\n    return this.serverSession?.id;\n  }\n\n  get serverSession(): ServerSession {\n    let serverSession = this._serverSession;\n    if (serverSession == null) {\n      if (this.explicit) {\n        throw new MongoRuntimeError('Unexpected null serverSession for an explicit session');\n      }\n      if (this.hasEnded) {\n        throw new MongoRuntimeError('Unexpected null serverSession for an ended implicit session');\n      }\n      serverSession = this.sessionPool.acquire();\n      this._serverSession = serverSession;\n    }\n    return serverSession;\n  }\n\n  get loadBalanced(): boolean {\n    return this.client.topology?.description.type === TopologyType.LoadBalanced;\n  }\n\n  /** @internal */\n  pin(conn: Connection): void {\n    if (this.pinnedConnection) {\n      throw TypeError('Cannot pin multiple connections to the same session');\n    }\n\n    this.pinnedConnection = conn;","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/sessions.ts#L203-L239","documentation":"Thrown by the `serverSession` getter when an implicit (non-explicit) session that has already ended is accessed again (sessions.ts:220). Implicit sessions are created internally per-operation and ended automatically; accessing the server session after `end()` has run means the session is no longer usable.","triggerScenarios":"Using an implicit session (e.g. one attached to a cursor) after it was ended; re-running an operation on a cursor whose session was cleaned up; holding a reference to an implicit session past its lifecycle.","commonSituations":"Iterating a cursor after its owning implicit session was ended; sharing an implicit session handle across async boundaries that outlive the operation; manual `endSession()` on an implicit session followed by further use.","solutions":["Do not retain or reuse implicit sessions; let the driver manage their lifecycle.","If you need a long-lived session, create an explicit one with `client.startSession()` and end it yourself when done.","Ensure cursors are fully consumed or closed before their session ends."],"exampleFix":"// before\nconst cursor = coll.find({}, { session: implicitSession });\nimplicitSession.endSession();\nawait cursor.next(); // throws\n// after\nawait cursor.next();\nimplicitSession.endSession();","handlingStrategy":"validation","validationCode":"function useImplicitSession(session, fn) {\n  if (session.hasEnded) throw new Error('session has ended; obtain a new one');\n  return fn();\n}","typeGuard":"function isSessionUsable(session) {\n  return session != null && !session.hasEnded;\n}","tryCatchPattern":null,"preventionTips":["Do not retain implicit sessions across operations.","Fully consume or close cursors before ending their session.","Use explicit sessions when you need control over the lifecycle."],"tags":["sessions","lifecycle","cursors"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}