Raising StopIteration causes Django to become completely unresponsive

26 views
Skip to first unread message

Shaheed Haque

unread,
Dec 19, 2023, 11:25:15 AM12/19/23
to django...@googlegroups.com
Hi,

I'm running Django 4.2.7 with Python 3.11 on Ubuntu on my development
setup (so no gunicorn, nginx etc, just standalone Django). I just ran
into the following issue which, once it occurs, causes Django to
become completely unresponsive:

=============
2023-12-19 16:07:02,763 [ERROR] asyncio: Exception in callback
_chain_future.<locals>._set_state(<Future pendi...ask_wakeup()]>,
<Future at 0x...StopIteration>) at /usr/lib/python3.11/async
io/futures.py:381
handle: <Handle _chain_future.<locals>._set_state(<Future
pendi...ask_wakeup()]>, <Future at 0x...StopIteration>) at
/usr/lib/python3.11/asyncio/futures.py:381>
Traceback (most recent call last):
File "/usr/lib/python3.11/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/usr/lib/python3.11/asyncio/futures.py", line 383, in _set_state
_copy_future_state(other, future)
File "/usr/lib/python3.11/asyncio/futures.py", line 359, in _copy_future_state
dest.set_exception(_convert_future_exc(exception))
TypeError: StopIteration interacts badly with generators and cannot be
raised into a Future
=============

Once this happens, Django becomes unresponsive (which is what I guess
the dire wording of the exception is hinting at). A trivial reproducer
looks like this:

=============
def post(self, request: HttpRequest, *args, **kwargs):
x = next(iter({}.items()))
...
=============

Obviously, with an empty dictionary, the next() on the iter()
immediately raises StopIteration, which in turn causes the traceback
above.

In the particular case at hand, the empty dictionary arises for
artificial reasons, and so is not a big concern. However, my code does
have scattered references to next() and my concern is that Django
itself becomes unresponsive.

Am I doing something silly? Is it expected that Django will actually
become unresponsive in such cases?

Advice appreciated.

Thanks, Shaheed
Reply all
Reply to author
Forward
0 new messages