Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.55k stars 2.77k forks source link

Making aio requests results on a "Event loop is closed" exception #37545

Open flixman opened 21 hours ago

flixman commented 21 hours ago

Describe the bug In the following code, within a flask app:

async def _get_vm_id(vm, owner, compute_client, network_client):
    vm = await compute_client.virtual_machines.get(resource_group, vm.name, expand='instanceView')
    if not _check_owner(vm, owner, raise_=False):
        return None

    nic_id = vm.network_profile.network_interfaces[0].id.split('/')
    name, rg = nic_id[-1], nic_id[4]
    status = vm.instance_view.statuses[-1].display_status
    nic = await network_client.network_interfaces.get(rg, name)
    return dict(name=name, ip=nic.ip_configurations[0].private_ip_address, status=status)

async def myendpoint():
    async with AIOComputeManagementClient(async_cred, subscription) as compute_client, \
            AIONetworkManagementClient(async_cred, subscription) as network_client:
        user_vms = await asyncio.gather(*[_get_vm_id(vm, owner, compute_client, network_client) for vm in vms])

I am getting the following exception:

2024-09-24T08:19:34.400890901Z Traceback (most recent call last):
2024-09-24T08:19:34.400896701Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/flask/app.py", line 1473, in wsgi_app
2024-09-24T08:19:34.400900901Z     response = self.full_dispatch_request()
2024-09-24T08:19:34.400904601Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.400908801Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/flask/app.py", line 882, in full_dispatch_request
2024-09-24T08:19:34.400913001Z     rv = self.handle_user_exception(e)
2024-09-24T08:19:34.400916901Z          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.400920601Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/flask/app.py", line 880, in full_dispatch_request
2024-09-24T08:19:34.400924601Z     rv = self.dispatch_request()
2024-09-24T08:19:34.400928201Z          ^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.400931702Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/flask/app.py", line 865, in dispatch_request
2024-09-24T08:19:34.400935602Z     return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
2024-09-24T08:19:34.400939402Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.400943002Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/asgiref/sync.py", line 254, in __call__
2024-09-24T08:19:34.400947402Z     return call_result.result()
2024-09-24T08:19:34.400951402Z            ^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.400955202Z   File "/opt/python/3.12.2/lib/python3.12/concurrent/futures/_base.py", line 449, in result
2024-09-24T08:19:34.400959302Z     return self.__get_result()
2024-09-24T08:19:34.400963102Z            ^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.400966702Z   File "/opt/python/3.12.2/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
2024-09-24T08:19:34.400985002Z     raise self._exception
2024-09-24T08:19:34.400988902Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/asgiref/sync.py", line 331, in main_wrap
2024-09-24T08:19:34.400993202Z     result = await self.awaitable(*args, **kwargs)
2024-09-24T08:19:34.400997402Z              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401001702Z   File "/tmp/8dcdbd2dc3a398b/backend/blueprints/infrastructure.py", line 238, in get_list_iod_resources
2024-09-24T08:19:34.401005802Z     user_vms = await asyncio.gather(*[_get_vm_id(vm, owner, compute_client, network_client) for vm in tagged_vms])
2024-09-24T08:19:34.401010202Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401014002Z   File "/tmp/8dcdbd2dc3a398b/backend/blueprints/infrastructure.py", line 147, in _get_vm_id
2024-09-24T08:19:34.401019303Z     vm = await compute_client.virtual_machines.get(current_app.config['AZURE_AT_SHELL']['RG'], vm.name, expand='instanceView')
2024-09-24T08:19:34.401023203Z          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401027303Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/tracing/decorator_async.py", line 94, in wrapper_use_tracer
2024-09-24T08:19:34.401031403Z     return await func(*args, **kwargs)
2024-09-24T08:19:34.401035303Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401039303Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/mgmt/compute/v2024_03_01/aio/operations/_operations.py", line 9993, in get
2024-09-24T08:19:34.401043603Z     pipeline_response: PipelineResponse = await self._client._pipeline.run(  # pylint: disable=protected-access
2024-09-24T08:19:34.401047403Z                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401051203Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 219, in run
2024-09-24T08:19:34.401055503Z     return await first_node.send(pipeline_request)
2024-09-24T08:19:34.401059703Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401063403Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 68, in send
2024-09-24T08:19:34.401067903Z     response = await self.next.send(request)
2024-09-24T08:19:34.401072103Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401076203Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 68, in send
2024-09-24T08:19:34.401080603Z     response = await self.next.send(request)
2024-09-24T08:19:34.401084803Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401088403Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 68, in send
2024-09-24T08:19:34.401097003Z     response = await self.next.send(request)
2024-09-24T08:19:34.401100903Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401104304Z   [Previous line repeated 2 more times]
2024-09-24T08:19:34.401108404Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/mgmt/core/policies/_base_async.py", line 44, in send
2024-09-24T08:19:34.401112504Z     response = await self.next.send(request)
2024-09-24T08:19:34.401116204Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401120704Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/policies/_redirect_async.py", line 73, in send
2024-09-24T08:19:34.401125004Z     response = await self.next.send(request)
2024-09-24T08:19:34.401128604Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401132204Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/policies/_retry_async.py", line 180, in send
2024-09-24T08:19:34.401136404Z     response = await self.next.send(request)
2024-09-24T08:19:34.401140404Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401144404Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/policies/_authentication_async.py", line 92, in send
2024-09-24T08:19:34.401148304Z     await await_result(self.on_request, request)
2024-09-24T08:19:34.401152004Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_tools_async.py", line 56, in await_result
2024-09-24T08:19:34.401156404Z     return await result
2024-09-24T08:19:34.401160604Z            ^^^^^^^^^^^^
2024-09-24T08:19:34.401164704Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/policies/_authentication_async.py", line 63, in on_request
2024-09-24T08:19:34.401168804Z     await self._request_token(*self._scopes)
2024-09-24T08:19:34.401172904Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/policies/_authentication_async.py", line 188, in _request_token
2024-09-24T08:19:34.401176904Z     self._token = await await_result(cast(AsyncTokenCredential, self._credential).get_token, *scopes, **kwargs)
2024-09-24T08:19:34.401180504Z                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401184304Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_tools_async.py", line 56, in await_result
2024-09-24T08:19:34.401188305Z     return await result
2024-09-24T08:19:34.401191605Z            ^^^^^^^^^^^^
2024-09-24T08:19:34.401195405Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/identity/aio/_internal/get_token_mixin.py", line 93, in get_token
2024-09-24T08:19:34.401199505Z     token = await self._request_token(
2024-09-24T08:19:34.401203405Z             ^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401207105Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/identity/aio/_credentials/client_secret.py", line 67, in _request_token
2024-09-24T08:19:34.401214305Z     return await self._client.obtain_token_by_client_secret(scopes, self._secret, **kwargs)
2024-09-24T08:19:34.401217805Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401222305Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/identity/aio/_internal/aad_client.py", line 49, in obtain_token_by_client_secret
2024-09-24T08:19:34.401226205Z     return await self._run_pipeline(request, **kwargs)
2024-09-24T08:19:34.401229905Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401234105Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/identity/aio/_internal/aad_client.py", line 93, in _run_pipeline
2024-09-24T08:19:34.401238705Z     response = await self._pipeline.run(request, retry_on_methods=self._POST, **kwargs)
2024-09-24T08:19:34.401242605Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401246205Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 219, in run
2024-09-24T08:19:34.401250405Z     return await first_node.send(pipeline_request)
2024-09-24T08:19:34.401254005Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401257505Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 68, in send
2024-09-24T08:19:34.401261605Z     response = await self.next.send(request)
2024-09-24T08:19:34.401265405Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401269105Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 68, in send
2024-09-24T08:19:34.401273105Z     response = await self.next.send(request)
2024-09-24T08:19:34.401276806Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401280406Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 68, in send
2024-09-24T08:19:34.401284106Z     response = await self.next.send(request)
2024-09-24T08:19:34.401287606Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401291506Z   [Previous line repeated 1 more time]
2024-09-24T08:19:34.401295506Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/policies/_retry_async.py", line 180, in send
2024-09-24T08:19:34.401299606Z     response = await self.next.send(request)
2024-09-24T08:19:34.401303206Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401307206Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 68, in send
2024-09-24T08:19:34.401310806Z     response = await self.next.send(request)
2024-09-24T08:19:34.401314306Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401322406Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 68, in send
2024-09-24T08:19:34.401326606Z     response = await self.next.send(request)
2024-09-24T08:19:34.401330106Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401333706Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 68, in send
2024-09-24T08:19:34.401337306Z     response = await self.next.send(request)
2024-09-24T08:19:34.401341006Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401344606Z   [Previous line repeated 1 more time]
2024-09-24T08:19:34.401348306Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/_base_async.py", line 104, in send
2024-09-24T08:19:34.401352706Z     await self._sender.send(request.http_request, **request.context.options),
2024-09-24T08:19:34.401356706Z     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401360707Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/azure/core/pipeline/transport/_aiohttp.py", line 303, in send
2024-09-24T08:19:34.401364907Z     result = await self.session.request(  # type: ignore
2024-09-24T08:19:34.401368907Z              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401372907Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/aiohttp/client.py", line 657, in _request
2024-09-24T08:19:34.401377407Z     conn = await self._connector.connect(
2024-09-24T08:19:34.401381407Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401385407Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/aiohttp/connector.py", line 564, in connect
2024-09-24T08:19:34.401389607Z     proto = await self._create_connection(req, traces, timeout)
2024-09-24T08:19:34.401393907Z             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401398607Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/aiohttp/connector.py", line 975, in _create_connection
2024-09-24T08:19:34.401403207Z     _, proto = await self._create_direct_connection(req, traces, timeout)
2024-09-24T08:19:34.401407307Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401411707Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/aiohttp/connector.py", line 1301, in _create_direct_connection
2024-09-24T08:19:34.401416207Z     hosts = await self._resolve_host(host, port, traces=traces)
2024-09-24T08:19:34.401420507Z             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401424807Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/aiohttp/connector.py", line 911, in _resolve_host
2024-09-24T08:19:34.401428707Z     return await asyncio.shield(resolved_host_task)
2024-09-24T08:19:34.401432707Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401440907Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/aiohttp/connector.py", line 948, in _resolve_host_with_throttle
2024-09-24T08:19:34.401445708Z     addrs = await self._resolver.resolve(host, port, family=self._family)
2024-09-24T08:19:34.401449908Z             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401454008Z   File "/tmp/8dcdbd2dc3a398b/antenv/lib/python3.12/site-packages/aiohttp/resolver.py", line 37, in resolve
2024-09-24T08:19:34.401458308Z     infos = await self._loop.getaddrinfo(
2024-09-24T08:19:34.401462208Z             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401466208Z   File "/opt/python/3.12.2/lib/python3.12/asyncio/base_events.py", line 899, in getaddrinfo
2024-09-24T08:19:34.401470108Z     return await self.run_in_executor(
2024-09-24T08:19:34.401474208Z                  ^^^^^^^^^^^^^^^^^^^^^
2024-09-24T08:19:34.401477708Z   File "/opt/python/3.12.2/lib/python3.12/asyncio/base_events.py", line 848, in run_in_executor
2024-09-24T08:19:34.401481808Z     self._check_closed()
2024-09-24T08:19:34.401485508Z   File "/opt/python/3.12.2/lib/python3.12/asyncio/base_events.py", line 540, in _check_closed
2024-09-24T08:19:34.401489608Z     raise RuntimeError('Event loop is closed')
2024-09-24T08:19:34.401493308Z RuntimeError: Event loop is closed

Expected behavior The application runs without issues.

xiangyan99 commented 16 hours ago

Thanks for the feedback, we’ll investigate asap.

msyyc commented 4 hours ago

@ChenxiJiang333 please help on this issue.

ChenxiJiang333 commented 4 hours ago

got it