Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async generators are not garbage collected #85158

Closed
vytas7 mannequin opened this issue Jun 15, 2020 · 3 comments
Closed

Async generators are not garbage collected #85158

vytas7 mannequin opened this issue Jun 15, 2020 · 3 comments
Labels
3.8 only security fixes performance Performance or resource usage topic-asyncio

Comments

@vytas7
Copy link
Mannequin

vytas7 mannequin commented Jun 15, 2020

BPO 40986
Nosy @asvetlov, @1st1, @vytas7
Files
  • test.py: Async test case
  • test_sync.py: Sync test case
  • test_trio.py: Async test case (w/ trio)
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-06-24.20:26:26.145>
    created_at = <Date 2020-06-15.14:27:50.640>
    labels = ['invalid', 'expert-asyncio', '3.8', 'performance']
    title = 'Async generators are not garbage collected'
    updated_at = <Date 2020-06-24.20:26:26.144>
    user = 'https://github.com/vytas7'

    bugs.python.org fields:

    activity = <Date 2020-06-24.20:26:26.144>
    actor = 'vytas'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-06-24.20:26:26.145>
    closer = 'vytas'
    components = ['asyncio']
    creation = <Date 2020-06-15.14:27:50.640>
    creator = 'vytas'
    dependencies = []
    files = ['49231', '49232', '49233']
    hgrepos = []
    issue_num = 40986
    keywords = []
    message_count = 3.0
    messages = ['371550', '371560', '372277']
    nosy_count = 3.0
    nosy_names = ['asvetlov', 'yselivanov', 'vytas']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'resource usage'
    url = 'https://bugs.python.org/issue40986'
    versions = ['Python 3.8']

    @vytas7
    Copy link
    Mannequin Author

    vytas7 mannequin commented Jun 15, 2020

    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.

    @vytas7 vytas7 mannequin added 3.8 only security fixes topic-asyncio performance Performance or resource usage labels Jun 15, 2020
    @vytas7
    Copy link
    Mannequin Author

    vytas7 mannequin commented Jun 15, 2020

    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.

    @vytas7
    Copy link
    Mannequin Author

    vytas7 mannequin commented Jun 24, 2020

    I've now skimmed through the relevant Trio issue python-trio/trio#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.

    @vytas7 vytas7 mannequin closed this as completed Jun 24, 2020
    @vytas7 vytas7 mannequin added the invalid label Jun 24, 2020
    @vytas7 vytas7 mannequin closed this as completed Jun 24, 2020
    @vytas7 vytas7 mannequin added the invalid label Jun 24, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes performance Performance or resource usage topic-asyncio
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants