[issue40986] Async generators are not garbage collected
New submission from Vytautas Liuolia : Hello! I am having issues with asynchronous generators not being garbage collected at least until the current loop has completed. In the attached test case (test.py), one starts iterating over an asynchronous generator, then breaks and returns the first element. After each call, gc.collect() is invoked for illustration purposes. It seems that no memory is freed until the whole test() coroutine is done. The for-loop could obviously be extended to more iterations, or swapped out to a while-loop to easily run out of available memory. I have then removed all async stuff, producing test_sync.py (also attached). In the sync case, everything is garbage-collected as I would expect. -- components: asyncio files: test.py messages: 371550 nosy: asvetlov, vytas, yselivanov priority: normal severity: normal status: open title: Async generators are not garbage collected type: resource usage versions: Python 3.8 Added file: https://bugs.python.org/file49231/test.py ___ Python tracker <https://bugs.python.org/issue40986> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue40986] Async generators are not garbage collected
Change by Vytautas Liuolia : Added file: https://bugs.python.org/file49232/test_sync.py ___ Python tracker <https://bugs.python.org/issue40986> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue40986] Async generators are not garbage collected
Vytautas Liuolia added the comment: Just for the heck of it, I've tried running my test case with Trio (FWIW, I've never used trio before), and it does seem to work as expected. -- Added file: https://bugs.python.org/file49233/test_trio.py ___ Python tracker <https://bugs.python.org/issue40986> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue40986] Async generators are not garbage collected
Vytautas Liuolia added the comment: I've now skimmed through the relevant Trio issue https://github.com/python-trio/trio/issues/265 and the currently deferred https://www.python.org/dev/peps/pep-0533/ and, as I understand, the issue is not trivial and subject to subtle edge cases. Getting back to the issue, it looks like a misunderstanding on my side, sorry! As per PEP-525: The asyncio event loop will use sys.set_asyncgen_hooks() API to maintain a weak set of all scheduled asynchronous generators, and to schedule their aclose() coroutine methods when it is time for generators to be GCed. It seems that my original test case just doesn't give these aclose() coroutines a chance to run. Awaiting an asyncio.sleep() "fixes" that. -- ___ Python tracker <https://bugs.python.org/issue40986> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue40986] Async generators are not garbage collected
Change by Vytautas Liuolia : -- resolution: -> not a bug stage: -> resolved status: open -> closed ___ Python tracker <https://bugs.python.org/issue40986> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com