Skip to content

Task created by StreamReaderProtocol gets garbage collected. #90467

Description

@simwr872
mannequin
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']

    Activity

    1. simwr872 commented on Jan 8, 2022

      simwr872mannequin
      MannequinAuthor

      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.

    2. asvetlov commented on Jan 9, 2022

      @asvetlov
      Contributor

      Thanks for the bug report.

      Seems like your analysis is correct.

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

    3. 3 remaining items

    4. transferred this issue fromon Apr 10, 2022
    5. python273 commented on Aug 26, 2022

      @python273
      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, ..)
    6. added a commit that references this issue on Aug 26, 2022
    7. added 2 commits that reference this issue on Aug 27, 2022
    8. added a commit that references this issue on Aug 27, 2022
    9. added a commit that references this issue on Aug 30, 2022
    10. added a commit that references this issue on Sep 8, 2022
    11. Repository owner moved this from Todo to Done in asyncioon Sep 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      Projects

      • Status
        Done

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions