This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Failing ensure_future still creates a Task
Type: behavior Stage: resolved
Components: asyncio Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: dmzz, gordon, gvanrossum, iritkatriel, kumaraditya, yselivanov
Priority: normal Keywords: easy, patch

Created on 2016-03-13 11:46 by gordon, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30287 closed dmzz, 2021-12-29 13:04
PR 30288 merged kumaraditya, 2021-12-29 13:40
PR 31003 merged kumaraditya, 2022-01-29 05:15
Messages (7)
msg261696 - (view) Author: Damien Nicolas (gordon) Date: 2016-03-13 11:46
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:


import asyncio

l = asyncio.get_event_loop()
l.close()

async def foo():
    pass

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
msg261701 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-03-13 17:40
Sounds like an easy fix. Could you submit a patch?
msg407294 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-29 16:44
Reproduced on 3.11.
msg412036 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-28 22:16
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.
msg412038 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-28 22:24
New changeset 24cc6411adbfe5555ecd8901f1ea50caa414c908 by Kumar Aditya in branch 'main':
bpo-26552: Fixed case where failing `asyncio.ensure_future` did not close the coroutine (#30288)
https://github.com/python/cpython/commit/24cc6411adbfe5555ecd8901f1ea50caa414c908
msg412039 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-28 22:26
Thanks Kumar for the fix!
msg412057 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-01-29 06:57
New changeset a5451c96a14ac0c3ee7cef7b5c52ab1d783ec613 by Kumar Aditya in branch '3.10':
bpo-26552: Fixed case where failing `asyncio.ensure_future` did not close the coroutine (#30288) (#31003)
https://github.com/python/cpython/commit/a5451c96a14ac0c3ee7cef7b5c52ab1d783ec613
History
Date User Action Args
2022-04-11 14:58:28adminsetgithub: 70739
2022-01-29 06:57:25gvanrossumsetmessages: + msg412057
2022-01-29 05:15:27kumaradityasetpull_requests: + pull_request29185
2022-01-28 22:26:00gvanrossumsetstatus: open -> closed
resolution: fixed
messages: + msg412039

stage: patch review -> resolved
2022-01-28 22:24:42gvanrossumsetmessages: + msg412038
2022-01-28 22:16:17gvanrossumsetmessages: + msg412036
2021-12-29 13:40:59kumaradityasetnosy: + kumaraditya
pull_requests: + pull_request28502
2021-12-29 13:04:14dmzzsetkeywords: + patch
nosy: + dmzz

pull_requests: + pull_request28501
stage: patch review
2021-11-29 18:25:25gvanrossumsetkeywords: + easy
2021-11-29 16:54:57vstinnersetnosy: - vstinner
2021-11-29 16:44:37iritkatrielsetnosy: + iritkatriel

messages: + msg407294
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.5
2016-03-13 17:40:29gvanrossumsetmessages: + msg261701
2016-03-13 11:46:10gordoncreate