Title: Failing ensure_future still creates a Task
Type: behavior Stage:
Components: asyncio Versions: Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gordon, gvanrossum, vstinner, yselivanov
Priority: normal Keywords:

Created on 2016-03-13 11:46 by gordon, last changed 2016-03-13 17:40 by gvanrossum.

Messages (2)
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()

async def foo():

    # Since the exception raises here, fut is never set
    # so we can't call fut.cancel()
    fut = asyncio.ensure_future(foo())
except RuntimeError:

# stderr:
# 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?
Date User Action Args
2016-03-13 17:40:29gvanrossumsetmessages: + msg261701
2016-03-13 11:46:10gordoncreate