Skip to content

azure-postgresql-auth mutates connect_args, breaking subsequent pooled SQLAlchemy connections #48382

Description

@pabloacan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ClientThis issue points to a problem in the data-plane of the library.PostgreSQL AuthService AttentionWorkflow: This issue is responsible by Azure service team.customer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions