You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
New changeset 24cc641 by Kumar Aditya in branch 'main': bpo-26552: Fixed case where failing asyncio.ensure_future did not close the coroutine (bpo-30288) 24cc641
New changeset a5451c9 by Kumar Aditya in branch '3.10': bpo-26552: Fixed case where failing asyncio.ensure_future did not close the coroutine (bpo-30288) (bpo-31003) a5451c9
Activity
gordon commentedon Mar 13, 2016
When calling asyncio.ensure_future() on a coroutine, and if the loop is closed, ensure_future() will raise a RuntimeError.
However, it still creates a Task, which will generate a RuntimeWarning that we can’t fix since there is no way to cancel the Task.
Here is the code to reproduce the bug:
try:
# Since the exception raises here, fut is never set
# so we can't call fut.cancel()
fut = asyncio.ensure_future(foo())
except RuntimeError:
pass
# stderr:
# aio.py:12: RuntimeWarning: coroutine 'foo' was never awaited
# pass
gvanrossum commentedon Mar 13, 2016
Sounds like an easy fix. Could you submit a patch?
iritkatriel commentedon Nov 29, 2021
Reproduced on 3.11.
gvanrossum commentedon Jan 28, 2022
So I just realized that the OP's description is slightly misleading. (Their code is spot on though!)
The code does not create an unwaited-for *task*, assuming that "task" refers to the asyncio.Task class.
What is created is a *coroutine* object that's never awaited (as the quoted RuntimeWarning message says: "coroutine 'foo' was never awaited").
So the thing that needs to be closed in the bowels of _ensure_future() is indeed the argument (coro_or_future), in case it is a coroutine object.
gvanrossum commentedon Jan 28, 2022
New changeset 24cc641 by Kumar Aditya in branch 'main':
bpo-26552: Fixed case where failing
asyncio.ensure_futuredid not close the coroutine (bpo-30288)24cc641
gvanrossum commentedon Jan 28, 2022
Thanks Kumar for the fix!
gvanrossum commentedon Jan 29, 2022
New changeset a5451c9 by Kumar Aditya in branch '3.10':
bpo-26552: Fixed case where failing
asyncio.ensure_futuredid not close the coroutine (bpo-30288) (bpo-31003)a5451c9