We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.
Already on GitHub?
to your account
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
assignee = None
closed_at = <Date 2022-01-28.22:26:00.502>
created_at = <Date 2016-03-13.11:46:10.277>
labels = ['easy', 'type-bug', '3.9', '3.10', '3.11', 'expert-asyncio']
title = 'Failing ensure_future still creates a Task'
updated_at = <Date 2022-01-29.06:57:25.632>
user = 'https://bugs.python.org/gordon'
activity = <Date 2022-01-29.06:57:25.632>
actor = 'gvanrossum'
assignee = 'none'
closed = True
closed_date = <Date 2022-01-28.22:26:00.502>
closer = 'gvanrossum'
components = ['asyncio']
creation = <Date 2016-03-13.11:46:10.277>
creator = 'gordon'
dependencies = 
files = 
hgrepos = 
issue_num = 26552
keywords = ['patch', 'easy']
message_count = 7.0
messages = ['261696', '261701', '407294', '412036', '412038', '412039', '412057']
nosy_count = 6.0
nosy_names = ['gvanrossum', 'yselivanov', 'gordon', 'dmzz', 'iritkatriel', 'kumaraditya']
pr_nums = ['30287', '30288', '31003']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue26552'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']
The text was updated successfully, but these errors were encountered:
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:
l = asyncio.get_event_loop()
async def foo():
# Since the exception raises here, fut is never set
# so we can't call fut.cancel()
fut = asyncio.ensure_future(foo())
# aio.py:12: RuntimeWarning: coroutine 'foo' was never awaited
Sorry, something went wrong.
Sounds like an easy fix. Could you submit a patch?
Reproduced on 3.11.
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.
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)
Thanks Kumar for the fix!
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)
No branches or pull requests