René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:bd0f0cbf-c9a7-494a-927a-1eb0c669c82b
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD762:240939:309E53:41DD69:69696A1F
html-safe-noncea6903b14b212335751ca462e3c3f6dd4252dac9ba47c54db567ad69116145f4f
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzYyOjI0MDkzOTozMDlFNTM6NDFERDY5OjY5Njk2QTFGIiwidmlzaXRvcl9pZCI6Ijg1MTQ0MTc1NDEwMTI2MTM2NjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacf544e41fb8f495ef163958c3ece4b3b3d045b4c0e1cf8212e4b83d0bbd0f28eb
hovercard-subject-tagissue:1910358017
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoftgraph/msgraph-sdk-python/366/issue_layout
twitter:imagehttps://opengraph.githubassets.com/3f21dabf2186955fdd35c7c039bbcc71e3c2aab425fc1c266a4fb23f83182685/microsoftgraph/msgraph-sdk-python/issues/366
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/3f21dabf2186955fdd35c7c039bbcc71e3c2aab425fc1c266a4fb23f83182685/microsoftgraph/msgraph-sdk-python/issues/366
og:image:altWhenever 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameaberezh
hostnamegithub.com
expected-hostnamegithub.com
Nonec6f193beb8ff08443adc07685d75302ab8aaf0a135f6e251c3ff3112c8deb881
turbo-cache-controlno-preview
go-importgithub.com/microsoftgraph/msgraph-sdk-python git https://github.com/microsoftgraph/msgraph-sdk-python.git
octolytics-dimension-user_id17304259
octolytics-dimension-user_loginmicrosoftgraph
octolytics-dimension-repository_id534665999
octolytics-dimension-repository_nwomicrosoftgraph/msgraph-sdk-python
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id534665999
octolytics-dimension-repository_network_root_nwomicrosoftgraph/msgraph-sdk-python
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseec4d88fbe7ed62446d90b0a6d8e6db4248a8f8b8
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/microsoftgraph/msgraph-sdk-python/issues/366#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoftgraph%2Fmsgraph-sdk-python%2Fissues%2F366
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoftgraph%2Fmsgraph-sdk-python%2Fissues%2F366
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoftgraph%2Fmsgraph-sdk-python
Reloadhttps://github.com/microsoftgraph/msgraph-sdk-python/issues/366
Reloadhttps://github.com/microsoftgraph/msgraph-sdk-python/issues/366
Reloadhttps://github.com/microsoftgraph/msgraph-sdk-python/issues/366
microsoftgraph https://github.com/microsoftgraph
msgraph-sdk-pythonhttps://github.com/microsoftgraph/msgraph-sdk-python
Notifications https://github.com/login?return_to=%2Fmicrosoftgraph%2Fmsgraph-sdk-python
Fork 84 https://github.com/login?return_to=%2Fmicrosoftgraph%2Fmsgraph-sdk-python
Star 561 https://github.com/login?return_to=%2Fmicrosoftgraph%2Fmsgraph-sdk-python
Code https://github.com/microsoftgraph/msgraph-sdk-python
Issues 89 https://github.com/microsoftgraph/msgraph-sdk-python/issues
Pull requests 17 https://github.com/microsoftgraph/msgraph-sdk-python/pulls
Discussions https://github.com/microsoftgraph/msgraph-sdk-python/discussions
Actions https://github.com/microsoftgraph/msgraph-sdk-python/actions
Projects 0 https://github.com/microsoftgraph/msgraph-sdk-python/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/microsoftgraph/msgraph-sdk-python/security
Please reload this pagehttps://github.com/microsoftgraph/msgraph-sdk-python/issues/366
Insights https://github.com/microsoftgraph/msgraph-sdk-python/pulse
Code https://github.com/microsoftgraph/msgraph-sdk-python
Issues https://github.com/microsoftgraph/msgraph-sdk-python/issues
Pull requests https://github.com/microsoftgraph/msgraph-sdk-python/pulls
Discussions https://github.com/microsoftgraph/msgraph-sdk-python/discussions
Actions https://github.com/microsoftgraph/msgraph-sdk-python/actions
Projects https://github.com/microsoftgraph/msgraph-sdk-python/projects
Security https://github.com/microsoftgraph/msgraph-sdk-python/security
Insights https://github.com/microsoftgraph/msgraph-sdk-python/pulse
New issuehttps://github.com/login?return_to=https://github.com/microsoftgraph/msgraph-sdk-python/issues/366
New issuehttps://github.com/login?return_to=https://github.com/microsoftgraph/msgraph-sdk-python/issues/366
When making client calls from a sync env (with asyncio.run()) the loop gets closed after the first request, causing subsequent ones to failhttps://github.com/microsoftgraph/msgraph-sdk-python/issues/366#top
https://github.com/shemogumbe
bugSomething isn't workinghttps://github.com/microsoftgraph/msgraph-sdk-python/issues?q=state%3Aopen%20label%3A%22bug%22
https://github.com/aberezh
https://github.com/aberezh
aberezhhttps://github.com/aberezh
on Sep 24, 2023https://github.com/microsoftgraph/msgraph-sdk-python/issues/366#issue-1910358017
shemogumbehttps://github.com/shemogumbe
bugSomething isn't workinghttps://github.com/microsoftgraph/msgraph-sdk-python/issues?q=state%3Aopen%20label%3A%22bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.