Reproduction
Create an AsyncEngine using the documented integration:
engine = create_async_engine(
database_url,
connect_args={"credential": DefaultAzureCredential()},
)
enable_entra_authentication_async(engine)
Open a connection, dispose or otherwise force the pool to create a second physical connection, then open another connection.
Actual behavior
The first connection succeeds. The next physical connection can fail with:
CredentialValueError: credential is required and must be a TokenCredential.
enable_entra_authentication_async registers a do_connect listener that removes credential from cparams after the token is resolved. SQLAlchemy retains the connect_args dictionary used by the pool creator, so a later connection no longer has the credential. The resolved password can also remain and become stale.
Expected behavior
Every newly created physical pooled connection should receive a TokenCredential and resolve a current Entra token without callers having to re-inject the credential.
Proposed fix
Avoid mutating the persistent connection arguments. Resolve credentials from a per-connection copy, or retain the helper credential while removing only it from the arguments passed to the DBAPI connection.
Environment
azure-postgresql-auth 1.0.2
- SQLAlchemy 2.0.44
- psycopg 3
- AsyncEngine with connection pooling
Reproduction
Create an
AsyncEngineusing the documented integration:Open a connection, dispose or otherwise force the pool to create a second physical connection, then open another connection.
Actual behavior
The first connection succeeds. The next physical connection can fail with:
enable_entra_authentication_asyncregisters ado_connectlistener that removescredentialfromcparamsafter the token is resolved. SQLAlchemy retains theconnect_argsdictionary used by the pool creator, so a later connection no longer has the credential. The resolvedpasswordcan also remain and become stale.Expected behavior
Every newly created physical pooled connection should receive a
TokenCredentialand resolve a current Entra token without callers having to re-inject the credential.Proposed fix
Avoid mutating the persistent connection arguments. Resolve credentials from a per-connection copy, or retain the helper credential while removing only it from the arguments passed to the DBAPI connection.
Environment
azure-postgresql-auth1.0.2