django / asgiref

ASGI specification and utilities
https://asgi.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
1.48k stars 210 forks source link

What are potential downfalls of making async_to_sync call inside async_to_sync #380

Closed suhjohn closed 1 year ago

suhjohn commented 1 year ago

Uvicorn -> Asgi -> synchronous view` -> sync_to_async wrapped function that issues asyncio.gather to 3rd party api.

I'm seeing a lot of 500 errors on the asyncio.gather due to aiohttp calls failing with timeouts. None of this happens when I perform the same using Gunicorn -> Wsgi.

Is there some magic that I might have to know that might be causing this issue?

andrewgodwin commented 1 year ago

There is a lot of magic inside those two functions - when you wrap them this way, the inside async_to_sync function will attempt to break out and use the outside event loop, so it will behave differently than if you just wrapped it inside one level.

Unless you can create a very simple test case that shows a bug in asgiref, though, this is likely just a bad async interaction/deadlock in your code, so I'm going to close this.