azure-kusto-python
azure-kusto-python copied to clipboard
Unclosed client session
Code Sample
async with KustoClient(kcsb) as client:
response = await client.execute(dbname, query,properties=clientReqProperties)
Problem description
I am initializing client using 'with' but still getting below errors:
[Error] Unclosed client sessionclient_session: <aiohttp.client.ClientSession object at 0x7ffa30df8310> 2022-09-07T10:10:42.288 [Error] Unclosed connectorconnections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7ffa30e136e0>, 946021.36840352)]']connector: <aiohttp.connector.TCPConnector object at 0x7ffa30df84d0>
I tried adding below code but still getting error:
await client._session.close()
Output of pip freeze
azure-functions==1.11.2 pyodbc==4.0.32 pandas==1.3.4 azure-identity==1.6.1 azure-kusto-data[aio]==3.1.0 azure-storage-file-datalake==12.5.0 packaging==21.2 stringcase==1.2.0 aiohttp==3.8.1
Can't recreate - can you give a more full code sample, including what reactor you use to run the async method?
Closing as there is no response in a few months.
@AsafMah Would you please re-open this?
I think the missing context here is that it does not happen with all authentication methods.
I am currently experiencing exactly this issue with managed identity (workload identity) authentication. Could you see if you could reproduce with that authentication method?
I also see in my logs:
/opt/venv/lib/python3.11/site-packages/azure/kusto/data/_token_providers.py:396: RuntimeWarning: coroutine 'ManagedIdentityCredential.close' was never awaited\n self._msi_auth_context_async.close()
So I suspect this is closer to the root cause - the close method on the ManagedIdentityCredential is never being called, and that's where the aiohttp session should be de-allocated as well.
https://github.com/Azure/azure-sdk-for-python/blob/d7b3abc69e3303eb1ee93aa2fbec701d9eb243ab/sdk/identity/azure-identity/azure/identity/aio/_credentials/managed_identity.py#L111
Here is the problem
The close method does not await when an async auth context is utilized
https://github.com/Azure/azure-kusto-python/blob/7cc9023452496d7700cd99d4544f71d01570d170/azure-kusto-data/azure/kusto/data/_token_providers.py#L392
Same problem technically occurs with AzCli authentication, but you only see the "coroutine was not awaited" message, not the other two, because the token is coming from the CLI, not requested by the application. Still, same root cause, just fewer logged warnings.
You end up with an aio ManagedIdentityCredential object
https://github.com/Azure/azure-sdk-for-python/blob/d7b3abc69e3303eb1ee93aa2fbec701d9eb243ab/sdk/identity/azure-identity/azure/identity/aio/_credentials/managed_identity.py#L21
And of course the close() method being called (and not awaited) above is a coroutine
https://github.com/Azure/azure-sdk-for-python/blob/d7b3abc69e3303eb1ee93aa2fbec701d9eb243ab/sdk/identity/azure-identity/azure/identity/aio/_credentials/managed_identity.py#L111
Thank you for the in depth research!
I'll work on a fix
Fixed in 4.3.0 - let us know if the issue persists