python-trio/async_generator

`asynccontextmanager` fails in `3.7-dev`

mhchia opened this issue · 1 comments

Python version: Python 3.7.6+ (heads/3.7:b0a6ec256b)
OS: macOS 10.14.6

async_generator.asynccontextmanager started to fail in python 3.7-dev(which is also the 3.7-dev in pyenv).
If we change to use contextlib.asynccontextmanager, it works. It's why I'm thinking something wrong in async_generator when an async-generator is being closed. I'm sorry that I haven't dug into the cause.

This issue seems to happen since python/cpython@b76d5e9.

# example_code.py

import asyncio

from async_generator import asynccontextmanager


@asynccontextmanager
async def async_iterator():
    yield 1


async def run():
    async with async_iterator():
        ...


asyncio.run(run())
# Output of the executed code

an error occurred during closing of asynchronous generator <async_generator object async_iterator at 0x108632290>
asyncgen: <async_generator object async_iterator at 0x108632290>
Traceback (most recent call last):
  File "/Users/mhchia/.pyenv/versions/3.7-dev/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/Users/mhchia/.pyenv/versions/3.7-dev/lib/python3.7/asyncio/base_events.py", line 587, in run_until_complete
    return future.result()
  File "/Users/mhchia/projects/practice/python/async-generator/await-twice.py", line 13, in run
    ...
  File "/Users/mhchia/.pyenv/versions/3.7-dev/envs/py-libp2p-3.7-dev/lib/python3.7/site-packages/async_generator/_util.py", line 84, in __aexit__
    raise
  File "/Users/mhchia/.pyenv/versions/3.7-dev/envs/py-libp2p-3.7-dev/lib/python3.7/site-packages/async_generator/_util.py", line 14, in __aexit__
    await self._aiter.aclose()
RuntimeError: cannot reuse already awaited aclose()/athrow()

During handling of the above exception, another exception occurred:

RuntimeError: cannot reuse already awaited aclose()/athrow()
Traceback (most recent call last):
  File "/Users/mhchia/projects/practice/python/async-generator/await-twice.py", line 16, in <module>
    asyncio.run(run())
  File "/Users/mhchia/.pyenv/versions/3.7-dev/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/Users/mhchia/.pyenv/versions/3.7-dev/lib/python3.7/asyncio/base_events.py", line 587, in run_until_complete
    return future.result()
  File "/Users/mhchia/projects/practice/python/async-generator/await-twice.py", line 13, in run
    ...
  File "/Users/mhchia/.pyenv/versions/3.7-dev/envs/py-libp2p-3.7-dev/lib/python3.7/site-packages/async_generator/_util.py", line 84, in __aexit__
    raise
  File "/Users/mhchia/.pyenv/versions/3.7-dev/envs/py-libp2p-3.7-dev/lib/python3.7/site-packages/async_generator/_util.py", line 14, in __aexit__
    await self._aiter.aclose()
RuntimeError: cannot reuse already awaited aclose()/athrow()

Huh, weird. @oremanj, this seems like the kind of weird issue you enjoy digging into, any chance you want to take a look? :-)