From 61b37877becf314617e0aa1aa7fd28ec0d89bdbb Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Tue, 31 Dec 2024 13:16:18 +0100 Subject: [PATCH] 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. --- supervisor/api/backups.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/supervisor/api/backups.py b/supervisor/api/backups.py index 0a5ca5aecb9..b37671bd9e3 100644 --- a/supervisor/api/backups.py +++ b/supervisor/api/backups.py @@ -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)