{"record":{"id":"b537f8b948ced5a7","repo":"mongodb/node-mongodb-native","slug":"no-auth-context-found-on-connection","errorCode":null,"errorMessage":"No auth context found on connection.","messagePattern":"No auth context found on connection\\.","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/cmap/connection_pool.ts","lineNumber":526,"sourceCode":"    for (const conn of this.connections) {\n      this.emitAndLog(\n        ConnectionPool.CONNECTION_CLOSED,\n        new ConnectionClosedEvent(this, conn, 'poolClosed')\n      );\n      conn.destroy();\n    }\n    this.connections.clear();\n    this.emitAndLog(ConnectionPool.CONNECTION_POOL_CLOSED, new ConnectionPoolClosedEvent(this));\n  }\n\n  /**\n   * @internal\n   * Reauthenticate a connection\n   */\n  async reauthenticate(connection: Connection): Promise<void> {\n    const authContext = connection.authContext;\n    if (!authContext) {\n      throw new MongoRuntimeError('No auth context found on connection.');\n    }\n    const credentials = authContext.credentials;\n    if (!credentials) {\n      throw new MongoMissingCredentialsError(\n        'Connection is missing credentials when asked to reauthenticate'\n      );\n    }\n\n    const resolvedCredentials = credentials.resolveAuthMechanism(connection.hello);\n    const provider = this.server.topology.client.s.authProviders.getOrCreateProvider(\n      resolvedCredentials.mechanism,\n      resolvedCredentials.mechanismProperties\n    );\n\n    if (!provider) {\n      throw new MongoMissingCredentialsError(\n        `Reauthenticate failed due to no auth provider for ${credentials.mechanism}`\n      );","sourceCodeStart":508,"sourceCodeEnd":544,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/cmap/connection_pool.ts#L508-L544","documentation":"A MongoRuntimeError thrown in ConnectionPool.reauthenticate when the connection's authContext is null/undefined. The authContext holds credentials, the hello document, and the reauth state; reauthentication is impossible without it. This indicates the connection was never fully handshaked/authenticated or the context was cleared — an internal invariant failure rather than a user config issue.","triggerScenarios":"The driver attempts to reauthenticate a connection (after a session expiration / SASL reauth signal from the server) but the connection has no authContext. Occurs on connections whose handshake was interrupted or where authContext was not populated during connection establishment.","commonSituations":"Server-side auth session expiry (e.g., MongoDB 5.0+ logical session timeout for authenticated users) triggering reauth on a connection whose handshake was incomplete; driver bug clearing authContext prematurely; connection reused after a failed auth handshake.","solutions":["Upgrade the driver — reauth paths have been refined across releases.","Ensure credentials are supplied consistently so the handshake populates authContext.","Verify the server is not aggressively expiring sessions faster than the driver can reauth.","Report persistent occurrences with driver version + server version to maintainers."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await operation();\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /No auth context/.test(e.message)) {\n    // recreate the client to force a fresh handshake\n  } else throw e;\n}","preventionTips":["Provide consistent credentials so handshakes complete fully.","Keep the driver current for reauth-path fixes.","Avoid reusing connections after a failed handshake."],"tags":["auth","reauthentication","internal","handshake"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}