{"record":{"id":"9cab7e9773bf0ebd","repo":"mongodb/node-mongodb-native","slug":"unexpected-null-selectedserver-a-cursor-creating","errorCode":null,"errorMessage":"Unexpected null selectedServer. A cursor creating command should have set this","messagePattern":"Unexpected null selectedServer\\. A cursor creating command should have set this","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"critical","filePath":"src/cursor/abstract_cursor.ts","lineNumber":866,"sourceCode":"  /**\n   * Returns a new uninitialized copy of this cursor, with options matching those that have been set on the current instance\n   */\n  abstract clone(): AbstractCursor<TSchema>;\n\n  /** @internal */\n  protected abstract _initialize(\n    session: ClientSession | undefined\n  ): Promise<InitialCursorResponse>;\n\n  /** @internal */\n  async getMore(): Promise<CursorResponse> {\n    if (this.cursorId == null) {\n      throw new MongoRuntimeError(\n        'Unexpected null cursor id. A cursor creating command should have set this'\n      );\n    }\n    if (this.selectedServer == null) {\n      throw new MongoRuntimeError(\n        'Unexpected null selectedServer. A cursor creating command should have set this'\n      );\n    }\n\n    if (this.cursorSession == null) {\n      throw new MongoRuntimeError(\n        'Unexpected null session. A cursor creating command should have set this'\n      );\n    }\n    const getMoreOptions = {\n      ...this.cursorOptions,\n      session: this.cursorSession,\n      batchSize: this.cursorOptions.batchSize\n    };\n\n    const getMoreOperation = new GetMoreOperation(\n      this.cursorNamespace,\n      this.cursorId,","sourceCodeStart":848,"sourceCodeEnd":884,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/cursor/abstract_cursor.ts#L848-L884","documentation":"Thrown as a MongoRuntimeError from getMore() when selectedServer is null/undefined at getMore time. selectedServer is populated during cursorInit() from the InitialCursorResponse returned by _initialize(); a null value here means the cursor creating command never stored the server it selected, which violates the documented contract of the @internal InitialCursorResponse interface. Like the other getMore() guards, this is a driver-internal invariant and indicates a bug rather than user misconfiguration.","triggerScenarios":"A subclass's _initialize() returns an InitialCursorResponse without a server; the operation object failed to store its selected server (the TODO(NODE-2882) comment in the file describes this contract); test code constructs a response object manually without a server.","commonSituations":"Custom cursor subclasses that return incomplete InitialCursorResponse objects; driver bugs where server selection state is lost between operations; instrumentation that strips fields off the response.","solutions":["If subclassing, ensure _initialize() returns { server, session, response } with a non-null Server.","Report suspected driver bugs with a reproduction and the driver version.","Avoid calling the @internal getMore() directly; use the public iteration API.","Upgrade to the latest patch release."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  for await (const doc of cursor) handle(doc);\n} catch (err) {\n  if (err instanceof MongoRuntimeError && /null selectedServer/.test(err.message)) {\n    // recreate the cursor and retry the read\n    cursor = collection.find(filter, opts);\n  } else {\n    throw err;\n  }\n}","preventionTips":["Use only the public iteration API so server selection state is managed internally.","If subclassing, return a non-null Server from _initialize().","Report suspected driver bugs and keep current with patch releases."],"tags":["cursor","internal-invariant","runtime-error","bug","server-selection"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}