{"record":{"id":"7d1fc1f639ca5ef5","repo":"mongodb/node-mongodb-native","slug":"connectionpool-clear-called-in-load-balanced-mod","errorCode":null,"errorMessage":"ConnectionPool.clear() called in load balanced mode with no serviceId.","messagePattern":"ConnectionPool\\.clear\\(\\) called in load balanced mode with no serviceId\\.","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/cmap/connection_pool.ts","lineNumber":426,"sourceCode":"    queueMicrotask(() => this.processWaitQueue());\n  }\n\n  /**\n   * Clear the pool\n   *\n   * Pool reset is handled by incrementing the pool's generation count. Any existing connection of a\n   * previous generation will eventually be pruned during subsequent checkouts.\n   */\n  clear(options: { serviceId?: ObjectId; interruptInUseConnections?: boolean } = {}): void {\n    if (this.closed) {\n      return;\n    }\n\n    // handle load balanced case\n    if (this.loadBalanced) {\n      const { serviceId } = options;\n      if (!serviceId) {\n        throw new MongoRuntimeError(\n          'ConnectionPool.clear() called in load balanced mode with no serviceId.'\n        );\n      }\n      const sid = serviceId.toHexString();\n      const generation = this.serviceGenerations.get(sid);\n      // Only need to worry if the generation exists, since it should\n      // always be there but typescript needs the check.\n      if (generation == null) {\n        throw new MongoRuntimeError('Service generations are required in load balancer mode.');\n      } else {\n        // Increment the generation for the service id.\n        this.serviceGenerations.set(sid, generation + 1);\n      }\n      this.emitAndLog(\n        ConnectionPool.CONNECTION_POOL_CLEARED,\n        new ConnectionPoolClearedEvent(this, { serviceId })\n      );\n      return;","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/cmap/connection_pool.ts#L408-L444","documentation":"A MongoRuntimeError thrown in ConnectionPool.clear when the pool is in load-balanced mode and clear() is called without a serviceId. In load-balanced deployments the pool tracks per-service generations, so clearing requires identifying which backend service to reset. Omitting serviceId is an internal-driver error: the SDAM layer should always pass it on pool clear.","triggerScenarios":"Internal call path where ConnectionPool.clear({ serviceId }) is invoked in loadBalanced mode but serviceId is undefined. For end users this surfaces when using loadBalancer=true and the driver's internal clear logic loses track of the service id — typically after a hello/sdam error that prevents serviceId capture.","commonSituations":"Misconfigured load-balanced deployment (loadBalancer=true against a non-load-balanced endpoint); SDAM failing to capture the serviceId from a malformed server hello; driver version bugs in the load-balanced clear path; a proxy that strips the serviceId field from hello responses.","solutions":["Verify the deployment genuinely supports load-balanced mode (mongos behind a LB).","Ensure the driver version supports the load-balanced spec fully (4.x+).","Check that hello responses include the serviceId field (server 5.x+ behind a LB).","If not using load balancing, remove loadBalancer=true from the connection string."],"exampleFix":"// before\nconst client = new MongoClient('mongodb://lb-host:27017/?loadBalancer=true');\n\n// after (ensure LB front-ends mongos with serviceId; or drop the flag)\nconst client = new MongoClient('mongodb://rs-host:27017/?replicaSet=rs0');","handlingStrategy":"validation","validationCode":"// Only use loadBalancer=true with a deployment that advertises serviceId\nif (uri.includes('loadBalancer=true')) {\n  // ensure the endpoint is a mongos behind an L4 LB; otherwise remove the flag\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use loadBalancer=true only with supported mongos+LB topologies.","Remove loadBalancer=true for standard replica-set or direct connections.","Keep the driver version current for load-balanced SDAM fixes."],"tags":["load-balanced","connection-pool","sdam","internal"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}