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

Use longer timeouts for API checks before trigger a rollback #4658

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
6 changes: 4 additions & 2 deletions supervisor/homeassistant/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async def get_core_state(self) -> dict[str, Any]:
"""Return Home Assistant core state."""
return await self._get_json("api/core/state")

async def check_api_state(self) -> bool:
async def check_api_state(self, check_running: bool = True) -> bool:
"""Return True if Home Assistant up and running."""
# Skip check on landingpage
if (
Expand All @@ -157,7 +157,9 @@ async def check_api_state(self) -> bool:
else:
data = await self.get_config()
# Older versions of home assistant does not expose the state
if data and data.get("state", "RUNNING") == "RUNNING":
if data:
if check_running:
return data.get("state", "RUNNING") == "RUNNING"
return True

return False
24 changes: 19 additions & 5 deletions supervisor/homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
_LOGGER: logging.Logger = logging.getLogger(__name__)

SECONDS_BETWEEN_API_CHECKS: Final[int] = 5
STARTUP_API_CHECK_TIMEOUT: Final[timedelta] = timedelta(minutes=5)
# Core Stage 1 and some wiggle room
STARTUP_API_RESPONSE_TIMEOUT: Final[timedelta] = timedelta(minutes=3)
# All stages plus event start timeout and some wiggle rooom
STARTUP_API_CHECK_RUNNING_TIMEOUT: Final[timedelta] = timedelta(minutes=15)
RE_YAML_ERROR = re.compile(r"homeassistant\.util\.yaml")


Expand Down Expand Up @@ -440,8 +443,9 @@ async def _block_till_run(self, version: AwesomeVersion) -> None:
return
_LOGGER.info("Wait until Home Assistant is ready")

start = datetime.now()
while not (timeout := datetime.now() >= start + STARTUP_API_CHECK_TIMEOUT):
deadline = datetime.now() + STARTUP_API_RESPONSE_TIMEOUT
check_running = False
while not (timeout := datetime.now() >= deadline):
await asyncio.sleep(SECONDS_BETWEEN_API_CHECKS)

# 1: Check if Container is is_running
Expand All @@ -450,15 +454,25 @@ async def _block_till_run(self, version: AwesomeVersion) -> None:
break

# 2: Check if API response
if await self.sys_homeassistant.api.check_api_state():
if await self.sys_homeassistant.api.check_api_state(
check_running=check_running
):
if not check_running:
# API initially available, move deadline up and check API
# state to be running now
_LOGGER.info("Detect a Home Assistant instance")
deadline = datetime.now() + STARTUP_API_CHECK_RUNNING_TIMEOUT
check_running = True
continue

_LOGGER.info("Detect a running Home Assistant instance")
self._error_state = False
return

self._error_state = True
if timeout:
raise HomeAssistantStartupTimeout(
"No API response in 5 minutes, assuming core has had a fatal startup error",
"No Home Assistant Core response, assuming a fatal startup error",
agners marked this conversation as resolved.
Show resolved Hide resolved
_LOGGER.error,
)
raise HomeAssistantCrashError()
Expand Down
Loading