Execution of Unit Tests Dead Locked After Upgrade to Python 3.10.7
silenum opened this issue · 3 comments
I experienced a problem described on StackOverflow.
Obviously, nest_asyncio
does not properly stop the event loop in Python 3.10.7 as it used to do in Python 3.8.3 when executing unit tests. I am using FastAPI and within the event loop of FastAPI, I called another API with asyncio
calls. Therefore I used nested_asyncio
to achieve those calls. After the Python upgrade the nested event loop did not stop and so pytest
did not stop either.
Do you have an isolated test case that demonstrates the problem.
Hi @erdewit
Yes sure, see Demo Project.
It's the minimal setup I created for demonstrating the mentioned behaviour.
Thank you for the very clear test setup.
It appears FastAPI uses a threadpool for servicing requests and that the actual deadlock is in one of the threads:
Exception ignored in: <module 'threading' from '/usr/lib64/python3.11/threading.py'>
Traceback (most recent call last):
File "/usr/lib64/python3.11/threading.py", line 1574, in _shutdown
lock.acquire()
My guess is that nest_asyncio changes the order of how cleanup is done in FastAPI. This is an issue within FastAPI itself.
The deadlock can be circumvented by not using Depends
in main.py:
import nest_asyncio
from fastapi import FastAPI, Depends
from application.dependencies import get_service
from application.service import Service
app = FastAPI()
nest_asyncio.apply()
service: Service = get_service()
@app.get("/asyncio-demo")
async def get_cat_facts(): # service: Service = Depends(get_service)):
return {"facts": (service.collect_cat_facts())}