From 9ad1e76ea8483237b63bedc19a2ae4548ac147f1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 22 May 2024 19:43:56 -1000 Subject: [PATCH] Avoid creating async_register_repository tasks to register_unknown_repositories (#3536) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * debug * fix * tweak * lint * fix refactoring error * 50 was probably too small --------- Co-authored-by: Joakim Sørensen --- custom_components/hacs/utils/data.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/custom_components/hacs/utils/data.py b/custom_components/hacs/utils/data.py index 0e53761b9b1..057be4031bc 100644 --- a/custom_components/hacs/utils/data.py +++ b/custom_components/hacs/utils/data.py @@ -250,22 +250,28 @@ async def restore(self): return False return True - async def register_unknown_repositories(self, repositories, category: str | None = None): + async def register_unknown_repositories( + self, repositories: dict[str, dict[str, Any]], category: str | None = None + ): """Registry any unknown repositories.""" - register_tasks = [ - self.hacs.async_register_repository( + for repo_idx, (entry, repo_data) in enumerate(repositories.items()): + # async_register_repository is awaited in a loop + # since its unlikely to ever suspend at startup + if ( + entry == "0" + or repo_data.get("category", category) is None + or self.hacs.repositories.is_registered(repository_id=entry) + ): + continue + await self.hacs.async_register_repository( repository_full_name=repo_data["full_name"], category=repo_data.get("category", category), check=False, repository_id=entry, ) - for entry, repo_data in repositories.items() - if entry != "0" - and not self.hacs.repositories.is_registered(repository_id=entry) - and repo_data.get("category", category) is not None - ] - if register_tasks: - await asyncio.gather(*register_tasks) + if repo_idx % 100 == 0: + # yield to avoid blocking the event loop + await asyncio.sleep(0) @callback def async_restore_repository(self, entry: str, repository_data: dict[str, Any]):