question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I get TypeError: StopIteration interacts badly with generators and cannot be raised into a Future

See original GitHub issue

Hi,

lately I’ve started to see in the logs this error:

[2020-02-10 12:34:40 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-02-10 12:34:40 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2020-02-10 12:34:40 +0000] [1] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2020-02-10 12:34:40 +0000] [13] [INFO] Booting worker with pid: 13
[2020-02-10 12:34:41 +0000] [13] [INFO] Started server process [13]
[2020-02-10 12:34:41 +0000] [13] [INFO] Waiting for application startup.
[2020-02-10 12:34:41 +0000] [13] [INFO] Application startup complete.
Exception in callback <function _chain_future.<locals>._set_state at 0x7fdd9e719f70>
handle: <Handle _chain_future.<locals>._set_state>
Traceback (most recent call last):
  File "uvloop/cbhandles.pyx", line 70, in uvloop.loop.Handle._run
  File "/usr/local/lib/python3.8/asyncio/futures.py", line 356, in _set_state
    _copy_future_state(other, future)
  File "/usr/local/lib/python3.8/asyncio/futures.py", line 332, in _copy_future_state
    dest.set_exception(_convert_future_exc(exception))
TypeError: StopIteration interacts badly with generators and cannot be raised into a Future

It doesn’t show up straight away, but after a few queries. After a few more queries I get:

[2020-02-10 12:12:49 +0000] [13] [ERROR] Exception in ASGI application
....
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3355, in scalar
    ret = self.one()
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3325, in one
    ret = self.one_or_none()
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3294, in one_or_none
    ret = list(self)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3367, in __iter__
    return self._execute_and_instances(context)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3388, in _execute_and_instances
    conn = self._get_bind_args(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3403, in _get_bind_args
    return fn(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3382, in _connection_from_session
    conn = self.session.connection(**kw)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1130, in connection
    return self._connection_for_bind(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1138, in _connection_for_bind
    return self.transaction._connection_for_bind(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 432, in _connection_for_bind
    conn = bind._contextual_connect()
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2242, in _contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2276, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 363, in connect
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 127, in _do_get
    raise exc.TimeoutError(
sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30 (Background on this error at: http://sqlalche.me/e/3o7r)

Any idea what could be happening?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
maximmarakovcommented, Jan 20, 2021

I got the same traceback, and found that any StopIteration exception will kill your request handling. For example, this server:

from fastapi import FastAPI
app = FastAPI()

@app.get("/")
def root():
    next(a for a in [])
    return {"message": "Hello World"}

will raise

Exception in callback <function _chain_future.<locals>._set_state at 0x7fb9a5ab9c10>
handle: <Handle _chain_future.<locals>._set_state>
Traceback (most recent call last):
  File "uvloop/cbhandles.pyx", line 70, in uvloop.loop.Handle._run
  File "/usr/lib/python3.8/asyncio/futures.py", line 353, in _set_state
    _copy_future_state(other, future)
  File "/usr/lib/python3.8/asyncio/futures.py", line 329, in _copy_future_state
    dest.set_exception(_convert_future_exc(exception))
TypeError: StopIteration interacts badly with generators and cannot be raised into a Future

and request will look like “infinity loading”.

So, if you had this exception “into Future” , check if there was an unhandled StopIteration exception in your code. Wish you luck 😄

1reaction
tiangolocommented, Nov 7, 2022

This actually goes deep down to asyncio. It was recently fixed in AnyIO: https://github.com/agronholm/anyio/pull/477

The fix will be available in AnyIO 4.0.0 (not yet released).

Meanwhile, you could install from the git repo. But you would have to monkey patch a removed attribute used in Starlette.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python PEP479 Change StopIteration handling inside ...
This PEP proposes a change to generators: when StopIteration is raised inside a generator, it is replaced it with RuntimeError. (More precisely, ...
Read more >
Python 3.7: StopIteration could be your next headache
I upgraded the python interpreter in one of the components I ran in production from 3.5 to 3.7. Main reason for that was...
Read more >
PEP 479 – Change StopIteration handling inside generators
This PEP proposes a change to generators: when StopIteration is raised inside a generator, it is replaced with RuntimeError . (More precisely, this...
Read more >
Getting async error, and everything broken, since update
updated to 113.2 last night, and now every time i start up i get the following, ... badly with generators and cannot be...
Read more >
5. Built-in Exceptions - Python 3.7.0a2 documentation
If a generator function defined in the presence of a from __future__ import generator_stop directive raises StopIteration , it will be converted into...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found