mongodb/node-mongodb-native · error · MongoRuntimeError
No auth context found on connection.
Error message
No auth context found on connection.
What it means
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.
Source
Thrown at src/cmap/connection_pool.ts:526
for (const conn of this.connections) {
this.emitAndLog(
ConnectionPool.CONNECTION_CLOSED,
new ConnectionClosedEvent(this, conn, 'poolClosed')
);
conn.destroy();
}
this.connections.clear();
this.emitAndLog(ConnectionPool.CONNECTION_POOL_CLOSED, new ConnectionPoolClosedEvent(this));
}
/**
* @internal
* Reauthenticate a connection
*/
async reauthenticate(connection: Connection): Promise<void> {
const authContext = connection.authContext;
if (!authContext) {
throw new MongoRuntimeError('No auth context found on connection.');
}
const credentials = authContext.credentials;
if (!credentials) {
throw new MongoMissingCredentialsError(
'Connection is missing credentials when asked to reauthenticate'
);
}
const resolvedCredentials = credentials.resolveAuthMechanism(connection.hello);
const provider = this.server.topology.client.s.authProviders.getOrCreateProvider(
resolvedCredentials.mechanism,
resolvedCredentials.mechanismProperties
);
if (!provider) {
throw new MongoMissingCredentialsError(
`Reauthenticate failed due to no auth provider for ${credentials.mechanism}`
);View on GitHub (pinned to 3366c21a63)
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.
Defensive patterns
Strategy: try-catch
Try / catch
// Reauth path internal error; surface and recycle client.
try {
await collection.insertOne(doc);
} catch (err) {
if (err instanceof MongoRuntimeError && /No auth context/.test(err.message)) {
await client.close();
client = new MongoClient(uri);
await client.connect();
}
throw err;
} Prevention
- 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).
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Connection is missing credentials when asked to reauthentica
- ConnectionPool.clear() called in load balanced mode with no
- Service generations are required in load balancer mode.
- Reauthenticate failed due to no auth provider for ${credenti
- Operation passed in cannot be an Array
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/b537f8b948ced5a7.json.
Report an issue: GitHub.