Azure / Azure/azure-sdk-for-python
Asyncio Unclosed client session when creating role assignments
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 3.4k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 193
Description
- **Package Name**: azure-mgmt-authorization
- **Package Version**: 5.0.0b1
- **Operating System**: MacOS
- **Python Version**: 3.14.0
**Describe the bug**
When trying to create a Azure role assignment on an Azure Storage container in a Fastapi web service, we are observing an error bring thrown by asyncio informing us that there was an unclosed client session. It looks similar to https://github.com/Azure/azure-sdk-for-python/issues/43021. For reference, here are some other important package versions:
- aiohttp==3.12.3
- azure-identity==1.26.0b1
- fastapi==0.121.2
```
async with ClientSecretCredential(
tenant_id=tenant_id,
client_id=client_id,
client_secret=client_secret
) as credential:
async with AuthorizationManagementClient(credential=credential, subscription_id=subscription_id) as client:
await client.role_assignments.create(
scope=f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}/blobServices/default/containers/{container_name}",
parameters={
"role_definition_id": "/providers/Microsoft.Authorization/roleDefinitions/ba92f5b4-2d11-453d-a403-e96b0029c9fe",
"principal_id": str(principal_oid),
"principal_type": "ServicePrincipal",
},
role_assignment_name=f"{uuid4()}",
)
```
**To Reproduce**
Steps to reproduce the behavior:
1. Create and open a ClientSecretCredential.
2. Create and open an AuthorizationManagementClient.
3. Use the `role_assignments` attribute of the AuthorizationManagementClient to create a role assignment on a storage container to a enterprise service principal.
These errors are present in the logs:
```
{"@timestamp": "2025-12-12T14:35:44.398Z", "ecs": {"version": "8.11"}, "log": {"level": "ERROR", "logger": "asyncio", "origin": {"function": "default_exception_handler", "file": {"line": 1875, "name": "base_events.py", "path": "/Users/dmardini/.pyenv/versions/3.14.0/lib/python3.14/asyncio/base_events.py"}}}, "message": "Unclosed client session\nclient_session: ", "process": {"pid": 90710, "name": "SpawnProcess-1", "thread": {"id": 123145478258688, "name": "asyncio_0"}}, "transaction": {"id": "3a6f5b7d-ea8e-4d8a-b230-ac358af17233"}}
{"@timestamp": "2025-12-12T14:35:44.399Z", "ecs": {"version": "8.11"}, "log": {"level": "ERROR", "logger": "asyncio", "origin": {"function": "default_exception_handler", "file": {"line": 1875, "name": "base_events.py", "path": "/Users/dmardini/.pyenv/versions/3.14.0/lib/python3.14/asyncio/base_events.py"}}}, "message": "Unclosed connector\nconnections: ['deque([(, 332050.53871552)])']\nconnector: ", "process": {"pid": 90710, "name": "SpawnProcess-1", "thread": {"id": 123145478258688, "name": "asyncio_0"}}, "transaction": {"id": "3a6f5b7d-ea8e-4d8a-b230-ac358af17233"}}
```
**Expected behavior**
No error logs should be present.
**Screenshots**
If applicable, add screenshots to help explain your problem.
**Additional context**
Add any other context about the problem here.
Contributor guide
Assessment
This issue has not been assessed yet.