Skip to content

Commit 61b3787

Browse files
authored
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.
1 parent e72c5a0 commit 61b3787

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

supervisor/api/backups.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,18 @@ async def release_on_freeze(new_state: CoreState):
296296
BusEvent.SUPERVISOR_STATE_CHANGE, release_on_freeze
297297
)
298298
try:
299-
await asyncio.wait(
299+
event_task = self.sys_create_task(event.wait())
300+
_, pending = await asyncio.wait(
300301
(
301302
backup_task,
302-
self.sys_create_task(event.wait()),
303+
event_task,
303304
),
304305
return_when=asyncio.FIRST_COMPLETED,
305306
)
307+
# It seems backup returned early (error or something), make sure to cancel
308+
# the event task to avoid "Task was destroyed but it is pending!" errors.
309+
if event_task in pending:
310+
event_task.cancel()
306311
return (backup_task, job.uuid)
307312
finally:
308313
self.sys_bus.remove_listener(listener)

0 commit comments

Comments
 (0)