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

Risk of websocket ping tasks being garbage collected before they finish #8614

Closed
bdraco opened this issue Aug 6, 2024 · 1 comment · Fixed by #8641
Closed

Risk of websocket ping tasks being garbage collected before they finish #8614

bdraco opened this issue Aug 6, 2024 · 1 comment · Fixed by #8641
Assignees
Milestone

Comments

@bdraco
Copy link
Member

bdraco commented Aug 6, 2024

          These are at risk of being garbage collected before they finish. 

This is an existing problem to be solved in a future PR
https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task

Important Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done. For reliable “fire-and-forget” background tasks, gather them in a collection:

Originally posted by @bdraco in #8608 (comment)

@bdraco bdraco self-assigned this Aug 6, 2024
@bdraco bdraco changed the title These are at risk of websocket ping tasks being garbage collected before they finish. These are at risk of websocket ping tasks being garbage collected before they finish Aug 6, 2024
@bdraco bdraco added this to the 3.10.2 milestone Aug 7, 2024
@bdraco bdraco changed the title These are at risk of websocket ping tasks being garbage collected before they finish Risk of websocket ping tasks being garbage collected before they finish Aug 7, 2024
@bdraco
Copy link
Member Author

bdraco commented Aug 7, 2024

I was really hoping to work on this tonight but I'm a lot more tired than I had hoped. Maybe will be able to rally after dinner.

bdraco added a commit that referenced this issue Aug 8, 2024
The event loop only keeps weak references to tasks, we need to
hold a strong reference to ensure that the ping task is not
prematurely garbage collected.
https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task

In almost all cases the ping can be done synchronously if
the task is created eagerly which avoids scheduling the ping
task on the event loop.

fixes #8614
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 a pull request may close this issue.

1 participant