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

Task created by StreamReaderProtocol gets garbage collected. #90467

Closed
simwr872 mannequin opened this issue Jan 8, 2022 · 4 comments
Closed

Task created by StreamReaderProtocol gets garbage collected. #90467

simwr872 mannequin opened this issue Jan 8, 2022 · 4 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@simwr872
Copy link
Mannequin

simwr872 mannequin commented Jan 8, 2022

BPO 46309
Nosy @asvetlov, @1st1, @kumaraditya303, @simwr872
PRs
  • bpo-46309: Added reference to task created by StreamReaderProtocol #30505
  • Files
  • example.py
  • 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 = None
    created_at = <Date 2022-01-08.23:35:55.417>
    labels = ['3.11', 'type-bug', '3.9', '3.10', 'expert-asyncio']
    title = 'Task created by StreamReaderProtocol gets garbage collected.'
    updated_at = <Date 2022-01-11.15:54:38.380>
    user = 'https://github.com/simwr872'

    bugs.python.org fields:

    activity = <Date 2022-01-11.15:54:38.380>
    actor = 'AlexWaygood'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['asyncio']
    creation = <Date 2022-01-08.23:35:55.417>
    creator = 'simwr872'
    dependencies = []
    files = ['50552']
    hgrepos = []
    issue_num = 46309
    keywords = ['patch']
    message_count = 3.0
    messages = ['410124', '410134', '410310']
    nosy_count = 4.0
    nosy_names = ['asvetlov', 'yselivanov', 'kumaraditya', 'simwr872']
    pr_nums = ['30505']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46309'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @simwr872
    Copy link
    Mannequin Author

    simwr872 mannequin commented Jan 8, 2022

    Documentation states that a reference must be kept when creating a task, https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task.

    This is not done in StreamReaderProtocol, https://github.com/python/cpython/blob/main/Lib/asyncio/streams.py#L244.

    I've provided a simple example to force garbage collection of this task which results in Task was destroyed but it is pending!. Uncommenting the commented code of the example shows that the task is not destroyed when a reference is kept.

    Am I missing something or using the library incorrectly? I've followed the examples at https://docs.python.org/3/library/asyncio-stream.html#tcp-echo-server-using-streams.

    @simwr872 simwr872 mannequin added 3.10 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error labels Jan 8, 2022
    @asvetlov
    Copy link
    Contributor

    asvetlov commented Jan 9, 2022

    Thanks for the bug report.

    Seems like your analysis is correct.

    Could you make a pull request which adds a task reference?

    @simwr872 simwr872 mannequin added 3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes 3.11 only security fixes labels Jan 11, 2022
    @AlexWaygood
    Copy link
    Member

    I'm removing 3.7 and 3.8 from the "versions" field, since those branches are old enough that they're now only accepting security-related patches.

    @AlexWaygood AlexWaygood removed 3.7 (EOL) end of life 3.8 (EOL) end of life labels Jan 11, 2022
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @ezio-melotti ezio-melotti moved this to Todo in asyncio Jul 17, 2022
    @python273
    Copy link
    Contributor

    As a workaround, it's possible to keep the reference in a global variable

    background_tasks = set()
    def create_connection_task(*args, **kwargs):
        task = asyncio.create_task(handle_connection(*args, **kwargs))
        background_tasks.add(task)
        task.add_done_callback(background_tasks.discard)
    
    # asyncio.start_server(create_connection_task, ..)

    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 27, 2022
    pablogsal pushed a commit that referenced this issue Aug 30, 2022
    miss-islington added a commit that referenced this issue Sep 8, 2022
    Repository owner moved this from Todo to Done in asyncio Sep 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
    Projects
    Status: Done
    Development

    No branches or pull requests

    4 participants