[New-bugs-announce] [issue34730] aclose() doesn't stop raise StopAsyncIteration / GeneratorExit to __anext__()

Devin Fee report at bugs.python.org
Tue Sep 18 20:27:26 EDT 2018


New submission from Devin Fee <devin at devinfee.com>:

I expected an async-generator's aclose() method to cancel it's __anext__(). Instead, the task isn't cancelled, and (it seems) manually cleanup is necessary.


@pytest.mark.asyncio
async def test_aclose_cancels():
    done = False
    event = asyncio.Event()
    agen = None

    async def make_agen():
        try:
            await event.wait()
            raise NotImplementedError()
            yield  # pylint: disable=W0101, unreachable
        finally:
            nonlocal done
            done = True

    async def run():
        nonlocal agen
        agen = make_agen()
        await agen.__anext__()

    task = asyncio.ensure_future(run())
    await asyncio.sleep(.01)

    await agen.aclose()
    await asyncio.sleep(.01)

    assert done
    assert task.done() and task.exception()


Looking at the tests for CPython, the implementation seems to expect it, but I'm unclear if PEP-525 actually intends for this to be the case: https://github.com/python/cpython/blob/e42b705188271da108de42b55d9344642170aa2b/Lib/test/test_asyncgen.py#L657

Alternatively, maybe I shouldn't be calling aclose, and instead I should be cancelling the task:

@pytest.mark.asyncio
async def test_cancel_finalizes():
    done = False
    event = asyncio.Event()
    agen = None

    async def make_agen():
        try:
            await event.wait()
            raise NotImplementedError()
            yield  # pylint: disable=W0101, unreachable
        finally:
            nonlocal done
            done = True

    async def run():
        nonlocal agen
        agen = make_agen()
        await agen.__anext__()

    task = asyncio.ensure_future(run())
    await asyncio.sleep(.01)
    task.cancel()
    await asyncio.sleep(.01)

    assert done
    assert task.done()

So the "bug" here is one of PEP-525 clarification.

----------
components: asyncio
messages: 325699
nosy: asvetlov, dfee, yselivanov
priority: normal
severity: normal
status: open
title: aclose() doesn't stop raise StopAsyncIteration / GeneratorExit to __anext__()
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34730>
_______________________________________


More information about the New-bugs-announce mailing list