Skip to content

Commit

Permalink
Avoid lingering tasks when using background backup tasks (#5518)
Browse files Browse the repository at this point in the history
When a backup tasks is run in background, but actually has an error
early the secondary event task to release the callee is lingering around
still, ultimately leading to a "Task was destroyed but it is pending!"
asyncio error.

Make sure we cancel the event task in case the backup returns early.
  • Loading branch information
agners authored Dec 31, 2024
1 parent e72c5a0 commit 61b3787
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions supervisor/api/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,18 @@ async def release_on_freeze(new_state: CoreState):
BusEvent.SUPERVISOR_STATE_CHANGE, release_on_freeze
)
try:
await asyncio.wait(
event_task = self.sys_create_task(event.wait())
_, pending = await asyncio.wait(
(
backup_task,
self.sys_create_task(event.wait()),
event_task,
),
return_when=asyncio.FIRST_COMPLETED,
)
# It seems backup returned early (error or something), make sure to cancel
# the event task to avoid "Task was destroyed but it is pending!" errors.
if event_task in pending:
event_task.cancel()
return (backup_task, job.uuid)
finally:
self.sys_bus.remove_listener(listener)
Expand Down

0 comments on commit 61b3787

Please sign in to comment.