{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/connection_pool.ts#L508-L544","documentation":"Thrown by ConnectionPool.reauthenticate() when the target connection has no authContext attached. The authContext holds the credentials, server, and handshake state needed to re-run an auth handshake; without it the driver cannot reauthenticate in response to the server's reauthentication request. Its absence indicates the connection never completed (or never stored) its auth setup. Surfaced as MongoRuntimeError during the reauth flow triggered by the server.","triggerScenarios":"The server sends a reauthentication-required error (e.g. after a session/token expiry) and the driver calls connection.pool.reauthenticate(connection), but connection.authContext is undefined. Encountered with auth mechanisms that periodically expire (AWS IAM, OIDC, Kerberos/GSSAPI, x.509) when the connection's authContext was not established or was cleared. Internal invariant; not directly user-invoked.","commonSituations":"Using short-lived credential mechanisms (MONGODB-AWS, MONGODB-OIDC) where token refresh / reauth happens but the connection skipped or lost its authContext. Driver bugs in versions that failed to attach authContext on certain code paths. Connections checked out before auth completed in a race during heavy concurrent connect.","solutions":["Upgrade the driver; authContext attachment bugs across reauth paths are addressed in recent patches.","For OIDC/AWS mechanisms, ensure the credentials/provider callbacks supplied to MongoClient are valid and that the machine's clock and tokens are current.","If using Kerberos or x.509, confirm the auth mechanism completed the initial handshake (check that other operations succeed before reauth is attempted).","Recycle the MongoClient if the error appears after a long-lived client runs into token expiry; a fresh pool re-establishes authContext on all connections."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Reauth path internal error; surface and recycle client.\ntry {\n  await collection.insertOne(doc);\n} catch (err) {\n  if (err instanceof MongoRuntimeError && /No auth context/.test(err.message)) {\n    await client.close();\n    client = new MongoClient(uri);\n    await client.connect();\n  }\n  throw err;\n}","preventionTips":["Upgrade the driver for authContext attachment fixes in the reauth path.","Ensure the auth mechanism completes its initial handshake before heavy concurrent use.","Recycle clients when using short-lived credential mechanisms (OIDC/AWS/Kerberos)."],"tags":["authentication","connection-pool","reauth","internal"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}