Skip to content

Async AzureIdentityAccessTokenProvider closes credential after every get_token breaking concurrent requests #528

@AlexPetul

Description

@AlexPetul

Under concurrency (e.g. asyncio.gather), token acquisition fails with errors such as “HTTP transport has already been closed” / “Session closed”. Example:

from azure.identity.aio import ClientSecretCredential
from msgraph import GraphServiceClient

import asyncio


credential = ClientSecretCredential(...)

client = GraphServiceClient(
    credentials=credential,
    scopes=["https://graph.microsoft.com/.default"],
)


async def fetch_users():
    await client.users.get()


async def main():
    await asyncio.gather(*[fetch_users() for i in range(5)])


if __name__ == "__main__":
    asyncio.run(main())

I guess the lifecycle of credentials object belongs to the app and we shouldn't implicitly close the transport. Suggested fix is to remove this line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Needs Triage 🔍

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions