﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34747	Django hangs on async views with asycio.gather and an async ORM call	rasca	nobody	"This simple view:

{{{
import asyncio

from django.http import HttpResponse

from myapp.models import MyModel


async def test_hang(request):
    await asyncio.gather(MyModel.objects.acreate())
    return HttpResponse('OK')
}}}

Hangs when called from daphne or uvicorn. When called from the django shell (with ipdb) like `await test_hang(None)` it works with no problem.

I created this minimal view to reproduce the issue, but in my project I'm creating a lot of tasks that have long external API requests saving the results to the db. Notice that with only one task it already hangs.

I haven't found anything in the async documentation / channels / daphne stating that I cannot do something like this.

When setting a trace with ipdb I found that the line that gets stuck is:

{{{
> /usr/local/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/selectors.py:561
kev_list = self._selector.control(None, max_ev, timeout)
}}}

And when running with python -m ipdb and then ctrl c it gets stuck in this line:

{{{
File ""/usr/local/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py"", line 1132, in _wait_for_tstate_lock
    if lock.acquire(block, timeout):
}}}

Shouldn't this simple view be working? If not, we should document it somehow. Any pointers much appreciated!
"	Bug	new	Database layer (models, ORM)	4.2	Normal		async asyncio.gather		Unreviewed	0	0	0	0	0	0
