Make sure Futures are cleaned up correctly #96
Merged
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.
As maintainer of the
imap
integration in Home Assistant we noticed users see Errors in their logs like:Error doing job: Task was destroyed but it is pending
This is caused when a future of task is not held in memory and Garbage collection cancels the job. at a few places in the code
asyncio.ensure_future()
is used without storing a reference to the Future. When executions takes longer it can be garbage collected before it is done or canceled causing the error.So calling
asyncio.ensure_future
starts a background task, but because this task is not stored, linked or referenced, the garbage collection might pick up the task and cancell it. According to https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task we need to make sure we remove or discard the task when it is done.See also logs linked with:
home-assistant/core#95239