Skip to content

Commit

Permalink
Avoid tracking tasks that finish right away (#111690)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Feb 28, 2024
1 parent 470d121 commit b9718fe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,8 @@ def async_create_task(
task = hass.async_create_task(
target, f"{name} {self.title} {self.domain} {self.entry_id}", eager_start
)
if task.done():
return task
self._tasks.add(task)
task.add_done_callback(self._tasks.remove)

Expand All @@ -949,6 +951,8 @@ def async_create_background_task(
target: target to call.
"""
task = hass.async_create_background_task(target, name, eager_start)
if task.done():
return task
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.remove)
return task
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ def async_create_task(
"""
if eager_start:
task = create_eager_task(target, name=name, loop=self.loop)
if task.done():
return task
else:
task = self.loop.create_task(target, name=name)
self._tasks.add(task)
Expand All @@ -657,6 +659,8 @@ def async_create_background_task(
"""
if eager_start:
task = create_eager_task(target, name=name, loop=self.loop)
if task.done():
return task
else:
task = self.loop.create_task(target, name=name)
self._background_tasks.add(task)
Expand Down

0 comments on commit b9718fe

Please sign in to comment.