From 3aae280de50a92cdfcf5c04b088fe9178fba0b2e Mon Sep 17 00:00:00 2001 From: elmurato <1382097+elmurato@users.noreply.github.com> Date: Tue, 8 Apr 2025 18:52:26 +0200 Subject: [PATCH] Fix blocking call in Pterodactyl (#142518) * Fix blocking call * Group blocking calls into a single executor job, catch StopIteration --- homeassistant/components/pterodactyl/api.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/pterodactyl/api.py b/homeassistant/components/pterodactyl/api.py index 40ede9de103..2aac359a5c6 100644 --- a/homeassistant/components/pterodactyl/api.py +++ b/homeassistant/components/pterodactyl/api.py @@ -63,15 +63,24 @@ class PterodactylAPI: self.pterodactyl = None self.identifiers = [] + def get_game_servers(self) -> list[str]: + """Get all game servers.""" + paginated_response = self.pterodactyl.client.servers.list_servers() # type: ignore[union-attr] + + return paginated_response.collect() + async def async_init(self): """Initialize the Pterodactyl API.""" self.pterodactyl = PterodactylClient(self.host, self.api_key) try: - paginated_response = await self.hass.async_add_executor_job( - self.pterodactyl.client.servers.list_servers - ) - except (BadRequestError, PterodactylApiError, ConnectionError) as error: + game_servers = await self.hass.async_add_executor_job(self.get_game_servers) + except ( + BadRequestError, + PterodactylApiError, + ConnectionError, + StopIteration, + ) as error: raise PterodactylConnectionError(error) from error except HTTPError as error: if error.response.status_code == 401: @@ -79,7 +88,6 @@ class PterodactylAPI: raise PterodactylConnectionError(error) from error else: - game_servers = paginated_response.collect() for game_server in game_servers: self.identifiers.append(game_server["attributes"]["identifier"])