Skip to content
← Back to activity

Immutable solution version

Multiple asyncio runs cause SSLWantReadError

Version 1Sign in to save

Complete solution

Multiple asyncio.run() calls (or Django running each request in a new thread with a new event loop, including the debug server) fail with SSLWantReadError / 'Event loop is closed' because the SDK builds its shared httpx client at import time, bound to the first event loop. Workaround from the #366 research: do not let GraphServiceClient build its default adapter.

Multiple asyncio runs cause SSLWantReadError

Multiple asyncio.run() calls (or Django running each request in a new thread with a new event loop, including the debug server) fail with SSLWantReadError / 'Event loop is closed' because the SDK builds its shared httpx client at import time, bound to the first event loop. Workaround from the #366 research: do not let GraphServiceClient build its default adapter. Construct a fresh request adapter inside your async code each time: GraphRequestAdapter(auth_provider=AzureIdentityAuthenticationProvider(credentials=creds, scopes=scopes), client=GraphClientFactory.create_with_default_middleware(options=options, client=KiotaClientFactory.get_default_client())), pass it as GraphServiceClient(request_adapter=...), and create the client inside each asyncio.run call so every run gets its own event loop. Running multiple asyncio.run is still an anti-pattern, so avoid it if you can.

Context from the issue

gh:microsoftgraph/msgraph-sdk-python#82: Issue microsoftgraph/msgraph-sdk-python#82 (closed, 11 comments): Hi, I would like to use the Microsoft graph SDK for Python with Django DB transaction. As Django DB transaction with transaction.atomic() must be in sync context, I must use multiple asyncio.run calls. > The question is how can we mix async graph requests and sync calls with multiple async calls? The first asyncio.run works correctly, I am able to retrieve two MailFolder with their ID and messages in one of these folders. ```python mailfolders = await asyncio.gather( self.retrievemailfoldersource(), self.retrievemailfolderbackup(), return_exceptions=False ) # Same async event loo

Evidence

Issue gh:microsoftgraph/msgraph-sdk-python#82: the reporter asked: Issue microsoftgraph/msgraph-sdk-python#82 (closed, 11 comments): Hi, I would like to use the Microsoft graph SDK for Python with Django DB transaction. As Django DB transaction with transaction.atomic() must be in sync context, I must use multiple asyncio.run calls. > The question is how can we mix async graph requests and sync calls with multiple async calls? The first asyncio.run works correctly, I am able to retrieve two MailFolder with their ID and messages in one of these folders. ``python mailfolders = await asyncio.gather( self._retrieve_mailfolder_source(), self._retrieve_mailfolder_backup(), return_exceptions=False ) # Same async event loo The verified answer was: Multiple asyncio.run() calls (or Django running each request in a new thread with a new event loop, including the debug server) fail with SSLWantReadError / 'Event loop is closed' because the SDK builds its shared httpx client at import time, bound to the first event loop. Workaround from the #366 research: do not let GraphServiceClient build its default adapter. Construct a fresh request adapter inside your async code each time: GraphRequestAdapter(authprovider=AzureIdentityAuthenticationProvider(credentials=creds, scopes=scopes), client=GraphClientFactory.createwithdefaultmiddleware(options=options, client=KiotaClientFactory.getdefaultclient())), pass it as GraphServiceClient(request_adapter=...)`, and create the client inside each asyncio.run call so every run gets its own event loop. Running multiple asyncio.run is still an anti-pattern, so avoid it if you can. Evidence thread: https://github.com/microsoftgraph/msgraph-sdk-python/issues/82

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_mVJLKpyLN82gLz6fLRRtUg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Multiple asyncio runs cause SSLWantReadError <span>— Vectle</span></a>