Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak when importing a platform fails #114602

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions homeassistant/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,9 +750,7 @@ def __init__(
self._import_futures: dict[str, asyncio.Future[ModuleType]] = {}
cache: dict[str, ModuleType | ComponentProtocol] = hass.data[DATA_COMPONENTS]
self._cache = cache
missing_platforms_cache: dict[str, ImportError] = hass.data[
DATA_MISSING_PLATFORMS
]
missing_platforms_cache: dict[str, bool] = hass.data[DATA_MISSING_PLATFORMS]
self._missing_platforms_cache = missing_platforms_cache
self._top_level_files = top_level_files or set()
_LOGGER.info("Loaded %s from %s", self.domain, pkg_path)
Expand Down Expand Up @@ -1085,8 +1083,7 @@ async def async_get_platforms(
import_futures: list[tuple[str, asyncio.Future[ModuleType]]] = []

for platform_name in platform_names:
full_name = f"{domain}.{platform_name}"
if platform := self._get_platform_cached_or_raise(full_name):
if platform := self._get_platform_cached_or_raise(platform_name):
platforms[platform_name] = platform
continue

Expand All @@ -1095,6 +1092,7 @@ async def async_get_platforms(
in_progress_imports[platform_name] = future
continue

full_name = f"{domain}.{platform_name}"
if (
self.import_executor
and full_name not in self.hass.config.components
Expand Down Expand Up @@ -1166,14 +1164,18 @@ async def async_get_platforms(

return platforms

def _get_platform_cached_or_raise(self, full_name: str) -> ModuleType | None:
def _get_platform_cached_or_raise(self, platform_name: str) -> ModuleType | None:
"""Return a platform for an integration from cache."""
full_name = f"{self.domain}.{platform_name}"
if full_name in self._cache:
# the cache is either a ModuleType or a ComponentProtocol
# but we only care about the ModuleType here
return self._cache[full_name] # type: ignore[return-value]
if full_name in self._missing_platforms_cache:
raise self._missing_platforms_cache[full_name]
bdraco marked this conversation as resolved.
Show resolved Hide resolved
raise ModuleNotFoundError(
f"Platform {full_name} not found",
name=f"{self.pkg_path}.{platform_name}",
)
return None

def platforms_are_loaded(self, platform_names: Iterable[str]) -> bool:
Expand All @@ -1189,9 +1191,7 @@ def get_platform_cached(self, platform_name: str) -> ModuleType | None:

def get_platform(self, platform_name: str) -> ModuleType:
"""Return a platform for an integration."""
if platform := self._get_platform_cached_or_raise(
f"{self.domain}.{platform_name}"
):
if platform := self._get_platform_cached_or_raise(platform_name):
return platform
return self._load_platform(platform_name)

Expand All @@ -1212,10 +1212,7 @@ def platforms_exists(self, platform_names: Iterable[str]) -> list[str]:
):
existing_platforms.append(platform_name)
continue
missing_platforms[full_name] = ModuleNotFoundError(
f"Platform {full_name} not found",
name=f"{self.pkg_path}.{platform_name}",
)
missing_platforms[full_name] = True

return existing_platforms

Expand All @@ -1233,11 +1230,12 @@ def _load_platform(self, platform_name: str) -> ModuleType:
cache: dict[str, ModuleType] = self.hass.data[DATA_COMPONENTS]
try:
cache[full_name] = self._import_platform(platform_name)
except ImportError as ex:
except ModuleNotFoundError:
if self.domain in cache:
# If the domain is loaded, cache that the platform
# does not exist so we do not try to load it again
self._missing_platforms_cache[full_name] = ex
self._missing_platforms_cache[full_name] = True
except ImportError:
raise
except RuntimeError as err:
# _DeadlockError inherits from RuntimeError
Expand Down
Loading