Repository navigation
"RuntimeError: cannot reuse already awaited coroutine" since 08bc03ff (under PYTHONASYNCIODEBUG) #143939
Copy link
Copy link
Closed
Labels
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jan 16, 2026 - changed the title
[-]Puzzling "RuntimeError: cannot reuse already awaited coroutine" since 08bc03ff (under PYTHONASYNCIODEBUG)[/-][+]"RuntimeError: cannot reuse already awaited coroutine" since 08bc03ff (under PYTHONASYNCIODEBUG)[/+]on Jan 16, 2026 Smaller repro, still with Falcon:
import asyncio from falcon import testing from falcon.asgi import App async def test_multiple_events() -> None: class SomeResource: async def on_get(self, _, resp): async def emitter(): yield await asyncio.sleep(0) yield resp.sse = emitter() app = App() app.add_route("/", SomeResource()) async with testing.TestClient(app) as tc: await tc.simulate_get() asyncio.run(test_multiple_events())
I've observed that changing the emitter to yield only once or to not sleep avoids the issue.
Maybe some task step interleaved withemitter()causes the failure.Perhaps this could be helpful (this is right before the failing
await):❯ python -m asyncio pstree 705111 └── (T) Task-1 └── test_multiple_events /home/bswck/Python/cpython/main/falc/falcon/../../t.py:20 └── (T) Task-2 └── App.__call__ /home/bswck/Python/cpython/main/falc/falcon/falcon/asgi/app.py:499 └── App._call_lifespan_handlers /home/bswck/Python/cpython/main/falc/falcon/falcon/asgi/app.py:1127 └── ASGILifespanEventEmitter.emit /home/bswck/Python/cpython/main/falc/falcon/falcon/testing/helpers.py:106 └── Condition.wait /home/bswck/Python/cpython/main/Lib/asyncio/locks.py:272 └── (T) Task-3 └── App.__call__ /home/bswck/Python/cpython/main/falc/falcon/falcon/asgi/app.py:459Thanks for the bug report. Is this using Cython and if so, is there a way to run it without Cython?
Never mind -- I understand the bug now and will have a fix shortly.
Reacted by Vytautas LiuoliaThis is fixed in main now. Thanks again for the bug report!
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Bug report
Bug description:
When running Falcon's CI on 3.15, we started experiencing a strange error that I cannot really understand.
See, e.g., https://github.com/falconry/falcon/actions/runs/21010040119/job/60402427099:
task_reqis created above usingasyncio.create_task(...), and normally it can beawaited as many times as desired.Printing it just before the failure gives:
Interestingly, the error is only raised when running under
PYTHONASYNCIODEBUG=1. Otherwise it passes just fine.I was unable to isolate an independent MRE... Using Falcon's source tree @ falconry/falcon@6432f40, the following is enough to reproduce, given a venv using CPython dev version:
I ran a similar script under
git bisect(first compiling the whole CPython source tree from scratch in a container), and bisected it down to this commit:commit 08bc03f
Author: Sam Gross colesbury@gmail.com
Date: Fri Dec 19 14:10:37 2025 -0500
cc @colesbury @hugovk
CPython versions tested on:
3.15, CPython main branch
Operating systems tested on:
Linux
Other
I'm not sure whether this is a bug in CPython, or just sloppy code on our end...
But I find the error itself, and the fact that it only happens under
PYTHONASYNCIODEBUG=1, puzzling.Thanks in advance for any ideas!
Linked PRs
_PyThreadStateImpl.generator_return_kind#143951