{"id":"333c1ca9548f8997","repo":"mongodb/node-mongodb-native","slug":"client-connect-did-not-create-a-topology-but-also","errorCode":null,"errorMessage":"client.connect did not create a topology but also did not throw","messagePattern":"client\\.connect did not create a topology but also did not throw","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"critical","filePath":"src/operations/execute_operation.ts","lineNumber":153,"sourceCode":"      await session.endSession();\n    }\n  }\n}\n\n/**\n * Connects a client if it has not yet been connected\n * @internal\n */\nexport async function autoConnect(client: MongoClient): Promise<Topology> {\n  if (client.topology == null) {\n    if (client.s.hasBeenClosed) {\n      throw new MongoNotConnectedError('Client must be connected before running operations');\n    }\n    client.s.options.__skipPingOnConnect = true;\n    try {\n      await client.connect();\n      if (client.topology == null) {\n        throw new MongoRuntimeError(\n          'client.connect did not create a topology but also did not throw'\n        );\n      }\n      return client.topology;\n    } finally {\n      delete client.s.options.__skipPingOnConnect;\n    }\n  }\n  return client.topology;\n}\n\n/** @internal */\ntype RetryOptions = {\n  session: ClientSession | undefined;\n  readPreference: ReadPreference;\n  topology: Topology;\n  timeoutContext: TimeoutContext;\n};","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/execute_operation.ts#L135-L171","documentation":"autoConnect (src/operations/execute_operation.ts:153) throws a MongoRuntimeError when client.connect() resolved without error but client.topology is still null. This is an internal invariant violation indicating a driver bug or an extremely abnormal connect path — connect() should either establish a topology or throw.","triggerScenarios":"This should not occur under normal conditions. It would only surface if a custom topology plugin, a monkey-patch, or a driver bug causes connect() to succeed without populating the topology.","commonSituations":"Driver regression after an internal refactor, a third-party module patching MongoClient.connect, or a very exotic runtime environment. Practically unseen in production.","solutions":["If you encounter this, report it as a driver bug with reproduction steps and driver/server versions.","Verify you are not monkey-patching MongoClient or its connect method.","Upgrade to the latest driver patch release in case the issue was already fixed."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await collection.find({}).toArray();\n} catch (err) {\n  if (err instanceof MongoRuntimeError && /did not create a topology/.test(err.message)) {\n    // report driver bug; create a fresh client\n  } else throw err;\n}","preventionTips":["Report this as a driver bug with full version information if encountered.","Avoid monkey-patching MongoClient or its connect method.","Keep the driver up to date."],"tags":["internal","invariant","connection","bug"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}