use background_tasks.create over bare asyncio.create_task #4551
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Should fix #4500
TL-DR: As in title. Should avoid GC messing up asyncio tasks since hard reference is kept.
Read specifically #4500 (comment) for the affected line in NiceGUI 2.13.0
Apparently, from articles in #4500 (comment), since asyncio does not itself keep a hard reference to the task created by
asyncio.create_task
, it can easily by GC-ed and causeTask was destroyed but it is pending!
Sitting in front of us (literally, it's 4 lines before the change in #4466), we just used
background_tasks.create
, which keeps a hard reference by putting all tasks intorunning_tasks
. It has never caused trouble, or at least if it would, it should have surfaced a long long time ago (too lazy to git blame, but I bet this function introduced several versions before NiceGUI 2.13.0)nicegui/nicegui/background_tasks.py
Lines 14 to 27 in 6ad4241