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.

Author serhiy.storchaka
Recipients Mark.Shannon, asvetlov, gvanrossum, serhiy.storchaka, yselivanov
Date 2021-06-27.09:11:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1624785092.65.0.844977946345.issue44518@roundup.psfhosted.org>
In-reply-to
Content
In the following example:

def gen():
    try:
        yield 1
    finally:
        print('finalize inner')

def func():
    try:
        for x in gen():
            break
    finally:
        print('finalize outer')

func()
print('END')

the output in CPython is:

finalize inner
finalize outer
END


But in similar example for asynchronous generator:

async def gen():
    try:
        yield 1
    finally:
        print('finalize inner')

async def func():
    try:
        async for x in gen():
            break
    finally:
        print('finalize outer')

import asyncio
asyncio.run(func())
print('END')

the output in CPython is:

finalize outer
finalize inner
END

There is a strong link somewhere which prevents finalization of the asynchronous generator object until leaving the outer function.

Tested on CPython 3.7-3.11. In PyPy "finalize inner" is not printed at all. Using closing() and aclosing() is the right way to get deterministic finalization, but in any case it would be better to get rid of strong link which prevents finalization of the asynchronous generator object.
History
Date User Action Args
2021-06-27 09:11:32serhiy.storchakasetrecipients: + serhiy.storchaka, gvanrossum, asvetlov, Mark.Shannon, yselivanov
2021-06-27 09:11:32serhiy.storchakasetmessageid: <1624785092.65.0.844977946345.issue44518@roundup.psfhosted.org>
2021-06-27 09:11:32serhiy.storchakalinkissue44518 messages
2021-06-27 09:11:32serhiy.storchakacreate