mongodb/node-mongodb-native · error · MongoRuntimeError
No auth context found on connection.
Error message
No auth context found on connection.
What it means
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.
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.
Defensive patterns
Strategy: try-catch
Try / catch
try {
await operation();
} catch (e) {
if (e instanceof MongoRuntimeError && /No auth context/.test(e.message)) {
// recreate the client to force a fresh handshake
} else throw e;
} Prevention
- Provide consistent credentials so handshakes complete fully.
- Keep the driver current for reauth-path fixes.
- Avoid reusing connections after a failed handshake.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Connection is missing credentials when asked to…
- Connection must have host and port and credentials defined.
- Reauthenticate failed due to no auth provider for
- Reauthentication already in progress.
- A collection name must be determined before getMore
AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11).
Data as JSON: /api/errors/b537f8b948ced5a7.
Report an issue: GitHub.
Appendix: 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 dce7939f86)