Repository navigation
Task created by StreamReaderProtocol gets garbage collected. #90467
Description
Activity
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.
- added3.10 (EOL)end of lifeend of lifetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jan 8, 2022 Thanks for the bug report.
Seems like your analysis is correct.
Could you make a pull request which adds a task reference?
- added3.7 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life3.11only security fixesonly security fixes
on Jan 11, 2022 3 remaining items
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, ..)
- added a commit that references this issue
on Aug 27, 2022 - added a commit that references this issue
on Aug 30, 2022 - added a commit that references this issue
on Sep 8, 2022
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
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:
bugs.python.org fields: