Opened 16 months ago

Last modified 16 months ago

#34747 closed Bug

Django hangs on async views with asycio.gather and an async ORM call — at Initial Version

Reported by: rasca Owned by: nobody
Component: Database layer (models, ORM) Version: 4.2
Severity: Normal Keywords: async asyncio.gather
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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!

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top