Avoid lingering tasks when using background backup tasks (#5518)

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.
This commit is contained in:
Stefan Agner 2024-12-31 13:16:18 +01:00 committed by GitHub
parent e72c5a037b
commit 61b37877be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -296,13 +296,18 @@ class APIBackups(CoreSysAttributes):
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)