Author samfrances
Recipients asvetlov, samfrances, yselivanov
Date 2019-06-30.23:53:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1561938788.3.0.655729732728.issue37455@roundup.psfhosted.org>
In-reply-to
Content
The documentation for `asyncio.run()` states:

"This function runs the passed coroutine, taking care of managing the asyncio event loop and finalizing asynchronous generators."

However, the following example seems to indicate that async generators are not being cleared up correctly.

```
import asyncio


async def count():
    try:
        i = 0
        while True:
            yield i
            i += 1
    finally:
        print("count() cleanup")


async def double(source):
    try:
        async for n in source:
            yield n * 2
    finally:
        print("double() cleanup")


async def anext(aiter):
    return await aiter.__anext__()


async def main():
    async for i in double(count()):
        if i > 10:
            return
        print(i)

asyncio.run(main())

```

The result is:

```
$ python example.py 
0
2
4
6
8
10
unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-2' coro=<<async_generator_athrow without __name__>()> exception=RuntimeError("can't send non-None value to a just-started coroutine")>
RuntimeError: can't send non-None value to a just-started coroutine
count() cleanup

```

The above error is from Python 3.8.0b1+, but the exact same error occurs in Python 3.7.

I'm not sure if this is a bug or if I am misunderstanding the intended behaviour of `asyncio.run()`, but the behaviour was certainly surprising to me.
History
Date User Action Args
2019-06-30 23:53:08samfrancessetrecipients: + samfrances, asvetlov, yselivanov
2019-06-30 23:53:08samfrancessetmessageid: <1561938788.3.0.655729732728.issue37455@roundup.psfhosted.org>
2019-06-30 23:53:08samfranceslinkissue37455 messages
2019-06-30 23:53:07samfrancescreate