Title: When making client calls from a sync env (with `asyncio.run()`) the loop gets closed after the first request, causing subsequent ones to fail · Issue #366 · microsoftgraph/msgraph-sdk-python · GitHub
Open Graph Title: When making client calls from a sync env (with `asyncio.run()`) the loop gets closed after the first request, causing subsequent ones to fail · Issue #366 · microsoftgraph/msgraph-sdk-python
X Title: When making client calls from a sync env (with `asyncio.run()`) the loop gets closed after the first request, causing subsequent ones to fail · Issue #366 · microsoftgraph/msgraph-sdk-python
Description: Whenever I make requests via the GraphServiceClient from a sync environment using asyncio.run() or asgiref.sync.async_to_sync() the first call succeeds, however, all subsequent calls fail with an error: Traceback (most recent call last):...
Open Graph Description: Whenever I make requests via the GraphServiceClient from a sync environment using asyncio.run() or asgiref.sync.async_to_sync() the first call succeeds, however, all subsequent calls fail with an e...
X Description: Whenever I make requests via the GraphServiceClient from a sync environment using asyncio.run() or asgiref.sync.async_to_sync() the first call succeeds, however, all subsequent calls fail with an e...
Opengraph URL: https://github.com/microsoftgraph/msgraph-sdk-python/issues/366
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"When making client calls from a sync env (with `asyncio.run()`) the loop gets closed after the first request, causing subsequent ones to fail","articleBody":"Whenever I make requests via the `GraphServiceClient` from a sync environment using `asyncio.run()` or `asgiref.sync.async_to_sync()` the first call succeeds, however, all subsequent calls fail with an error:\r\n\r\n```python\r\nTraceback (most recent call last):\r\n File \"/app/some_path/some_file.py\", line 15, in get_drives\r\n drives = asyncio.run(client.drives.get())\r\n File \"/usr/local/lib/python3.11/asyncio/base_events.py\", line 653, in run_until_complete\r\n return future.result()\r\n File \"/usr/local/lib/python3.11/site-packages/msgraph/generated/drives/drive_request_builder.py\", line 66, in get\r\n return await self.request_adapter.send_async(request_info, DriveItemCollectionResponse, error_mapping)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/httpx_request_adapter.py\", line 166, in send_async\r\n response = await self.get_http_response_message(request_info, parent_span)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/httpx_request_adapter.py\", line 508, in get_http_response_message\r\n resp = await self._http_client.send(request)\r\n File \"/usr/local/lib/python3.11/site-packages/httpx/_client.py\", line 1617, in send\r\n response = await self._send_handling_auth(\r\n File \"/usr/local/lib/python3.11/site-packages/httpx/_client.py\", line 1645, in _send_handling_auth\r\n response = await self._send_handling_redirects(\r\n File \"/usr/local/lib/python3.11/site-packages/httpx/_client.py\", line 1682, in _send_handling_redirects\r\n response = await self._send_single_request(request)\r\n File \"/usr/local/lib/python3.11/site-packages/httpx/_client.py\", line 1719, in _send_single_request\r\n response = await transport.handle_async_request(request)\r\n File \"/usr/local/lib/python3.11/site-packages/msgraph_core/middleware/async_graph_transport.py\", line 21, in handle_async_request\r\n response = await self.pipeline.send(request)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/middleware.py\", line 38, in send\r\n return await self._first_middleware.send(request, self._transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/redirect_handler.py\", line 77, in send\r\n response = await super().send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/middleware.py\", line 65, in send\r\n return await self.next.send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/retry_handler.py\", line 83, in send\r\n response = await super().send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/middleware.py\", line 65, in send\r\n return await self.next.send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/parameters_name_decoding_handler.py\", line 55, in send\r\n response = await super().send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/middleware.py\", line 65, in send\r\n return await self.next.send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/url_replace_handler.py\", line 44, in send\r\n response = await super().send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/middleware.py\", line 65, in send\r\n return await self.next.send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/user_agent_handler.py\", line 29, in send\r\n return await super().send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/middleware.py\", line 65, in send\r\n return await self.next.send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/msgraph_core/middleware/telemetry.py\", line 48, in send\r\n response = await super().send(request, transport)\r\n File \"/usr/local/lib/python3.11/site-packages/kiota_http/middleware/middleware.py\", line 62, in send\r\n response = await transport.handle_async_request(request)\r\n File \"/usr/local/lib/python3.11/site-packages/msgraph_core/middleware/async_graph_transport.py\", line 24, in handle_async_request\r\n response = await self.transport.handle_async_request(request)\r\n File \"/usr/local/lib/python3.11/site-packages/httpx/_transports/default.py\", line 353, in handle_async_request\r\n resp = await self._pool.handle_async_request(req)\r\n File \"/usr/local/lib/python3.11/site-packages/httpcore/_async/connection_pool.py\", line 226, in handle_async_request\r\n await self._close_expired_connections()\r\n File \"/usr/local/lib/python3.11/site-packages/httpcore/_async/connection_pool.py\", line 194, in _close_expired_connections\r\n await connection.aclose()\r\n File \"/usr/local/lib/python3.11/site-packages/httpcore/_async/connection.py\", line 166, in aclose\r\n await self._connection.aclose()\r\n File \"/usr/local/lib/python3.11/site-packages/httpcore/_async/http11.py\", line 241, in aclose\r\n await self._network_stream.aclose()\r\n File \"/usr/local/lib/python3.11/site-packages/httpcore/_backends/anyio.py\", line 54, in aclose\r\n await self._stream.aclose()\r\n File \"/usr/local/lib/python3.11/site-packages/anyio/streams/tls.py\", line 195, in aclose\r\n await self.transport_stream.aclose()\r\n File \"/usr/local/lib/python3.11/site-packages/anyio/_backends/_asyncio.py\", line 1161, in aclose\r\n self._transport.close()\r\n File \"/usr/local/lib/python3.11/asyncio/selector_events.py\", line 860, in close\r\n self._loop.call_soon(self._call_connection_lost, None)\r\n File \"/usr/local/lib/python3.11/asyncio/base_events.py\", line 761, in call_soon\r\n self._check_closed()\r\n File \"/usr/local/lib/python3.11/asyncio/base_events.py\", line 519, in _check_closed\r\n raise RuntimeError('Event loop is closed')\r\nRuntimeError: Event loop is closed\r\n```\r\n\r\nThe simplest example that reproduces this is:\r\n\r\n```python\r\nimport asyncio\r\nfrom azure.identity import ClientSecretCredential\r\nfrom msgraph import GraphServiceClient\r\n\r\ncredential = ClientSecretCredential(\r\n 'TENANT_ID',\r\n 'CLIENT_ID',\r\n 'CLIENT_SECRET')\r\nscopes = [\"https://graph.microsoft.com/.default\"]\r\nclient = GraphServiceClient(credential, scopes)\r\n\r\ndrives_1 = asyncio.run(client.drives.get())\r\ndrives_2 = asyncio.run(client.drives.get())\r\n```\r\n\r\nI use the GraphServiceClient inside a task run in a background worker (Django Q) alongside my Django app. So the first time the background task runs it succeeds, while the second time it fails.\r\n\r\nAs a workaround this does the trick:\r\n\r\n```python\r\ndef get_loop():\r\n try:\r\n loop = asyncio.get_event_loop()\r\n except RuntimeError:\r\n loop = asyncio.new_event_loop()\r\n asyncio.set_event_loop(loop)\r\n return loop\r\n\r\n# inside the background task\r\ndrives = get_loop().run_until_complete(client.drives.get())\r\n```","author":{"url":"https://github.com/aberezh","@type":"Person","name":"aberezh"},"datePublished":"2023-09-24T19:50:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":14},"url":"https://github.com/366/msgraph-sdk-python/issues/366"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bd0f0cbf-c9a7-494a-927a-1eb0c669c82b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D762:240939:309E53:41DD69:69696A1F |
| html-safe-nonce | a6903b14b212335751ca462e3c3f6dd4252dac9ba47c54db567ad69116145f4f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzYyOjI0MDkzOTozMDlFNTM6NDFERDY5OjY5Njk2QTFGIiwidmlzaXRvcl9pZCI6Ijg1MTQ0MTc1NDEwMTI2MTM2NjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f544e41fb8f495ef163958c3ece4b3b3d045b4c0e1cf8212e4b83d0bbd0f28eb |
| hovercard-subject-tag | issue:1910358017 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoftgraph/msgraph-sdk-python/366/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3f21dabf2186955fdd35c7c039bbcc71e3c2aab425fc1c266a4fb23f83182685/microsoftgraph/msgraph-sdk-python/issues/366 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3f21dabf2186955fdd35c7c039bbcc71e3c2aab425fc1c266a4fb23f83182685/microsoftgraph/msgraph-sdk-python/issues/366 |
| og:image:alt | Whenever I make requests via the GraphServiceClient from a sync environment using asyncio.run() or asgiref.sync.async_to_sync() the first call succeeds, however, all subsequent calls fail with an e... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aberezh |
| hostname | github.com |
| expected-hostname | github.com |
| None | c6f193beb8ff08443adc07685d75302ab8aaf0a135f6e251c3ff3112c8deb881 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoftgraph/msgraph-sdk-python git https://github.com/microsoftgraph/msgraph-sdk-python.git |
| octolytics-dimension-user_id | 17304259 |
| octolytics-dimension-user_login | microsoftgraph |
| octolytics-dimension-repository_id | 534665999 |
| octolytics-dimension-repository_nwo | microsoftgraph/msgraph-sdk-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 534665999 |
| octolytics-dimension-repository_network_root_nwo | microsoftgraph/msgraph-sdk-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ec4d88fbe7ed62446d90b0a6d8e6db4248a8f8b8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width