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

Use set instead of WeakSet in TaskGroup #60

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from

Conversation

heckad
Copy link

@heckad heckad commented Aug 4, 2023

Fix bug with tasks leaking from a TaskGroup.

@achimnol
Copy link
Owner

Um.. could you explain more context on this change?
Because weakref.WeakSet is a deliberate choice to prevent memory leaks by allowing task objects garbage-collected right after they finish.

@heckad
Copy link
Author

heckad commented Nov 27, 2023

Sorry for the late response. We just updated to python3.11 to solve the problem but here's a minimal MRE

import asyncio
import gc
import weakref

import aiotools


all_features = weakref.WeakSet()


async def inf_loop(future):
    while True:
        print("Message")
        await future


async def inner_scope(tg: aiotools.TaskGroup):
    future = asyncio.Future()

    tg.create_task(inf_loop(future))

    all_features.add(future)


async def main():
    try:
        async with aiotools.TaskGroup() as tg:
            await inner_scope(tg)
            await asyncio.sleep(1)
            gc.collect()
            print("collected")

            raise asyncio.CancelledError
    except asyncio.CancelledError:
        pass

asyncio.run(main(), debug=True)

With standard task group all good but on python3.10 with task group from aiotools we got
Task was destroyed but it is pending!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants