Trigger Home Assistant shutdown automations right before the stop event instead of during it (#91165)

Co-authored-by: Erik <erik@montnemery.com>
This commit is contained in:
Tudor Sandu 2023-12-05 13:24:41 -08:00 committed by GitHub
parent 44810f9772
commit 636e38f4b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 164 additions and 49 deletions

View file

@ -6,7 +6,7 @@ FROM ${BUILD_FROM}
# Synchronize with homeassistant/core.py:async_stop # Synchronize with homeassistant/core.py:async_stop
ENV \ ENV \
S6_SERVICES_GRACETIME=220000 S6_SERVICES_GRACETIME=240000
ARG QEMU_CPU ARG QEMU_CPU

View file

@ -1,8 +1,8 @@
"""Offer Home Assistant core automation rules.""" """Offer Home Assistant core automation rules."""
import voluptuous as vol import voluptuous as vol
from homeassistant.const import CONF_EVENT, CONF_PLATFORM, EVENT_HOMEASSISTANT_STOP from homeassistant.const import CONF_EVENT, CONF_PLATFORM
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
@ -30,24 +30,17 @@ async def async_attach_trigger(
job = HassJob(action, f"homeassistant trigger {trigger_info}") job = HassJob(action, f"homeassistant trigger {trigger_info}")
if event == EVENT_SHUTDOWN: if event == EVENT_SHUTDOWN:
return hass.async_add_shutdown_job(
@callback job,
def hass_shutdown(event): {
"""Execute when Home Assistant is shutting down.""" "trigger": {
hass.async_run_hass_job( **trigger_data,
job, "platform": "homeassistant",
{ "event": event,
"trigger": { "description": "Home Assistant stopping",
**trigger_data, }
"platform": "homeassistant", },
"event": event, )
"description": "Home Assistant stopping",
}
},
event.context,
)
return hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, hass_shutdown)
# Automation are enabled while hass is starting up, fire right away # Automation are enabled while hass is starting up, fire right away
# Check state because a config reload shouldn't trigger it. # Check state because a config reload shouldn't trigger it.

View file

@ -18,6 +18,7 @@ from collections.abc import (
) )
import concurrent.futures import concurrent.futures
from contextlib import suppress from contextlib import suppress
from dataclasses import dataclass
import datetime import datetime
import enum import enum
import functools import functools
@ -107,9 +108,10 @@ if TYPE_CHECKING:
from .helpers.entity import StateInfo from .helpers.entity import StateInfo
STAGE_1_SHUTDOWN_TIMEOUT = 100 STOPPING_STAGE_SHUTDOWN_TIMEOUT = 20
STAGE_2_SHUTDOWN_TIMEOUT = 60 STOP_STAGE_SHUTDOWN_TIMEOUT = 100
STAGE_3_SHUTDOWN_TIMEOUT = 30 FINAL_WRITE_STAGE_SHUTDOWN_TIMEOUT = 60
CLOSE_STAGE_SHUTDOWN_TIMEOUT = 30
block_async_io.enable() block_async_io.enable()
@ -299,6 +301,14 @@ class HassJob(Generic[_P, _R_co]):
return f"<Job {self.name} {self.job_type} {self.target}>" return f"<Job {self.name} {self.job_type} {self.target}>"
@dataclass(frozen=True)
class HassJobWithArgs:
"""Container for a HassJob and arguments."""
job: HassJob[..., Coroutine[Any, Any, Any] | Any]
args: Iterable[Any]
def _get_hassjob_callable_job_type(target: Callable[..., Any]) -> HassJobType: def _get_hassjob_callable_job_type(target: Callable[..., Any]) -> HassJobType:
"""Determine the job type from the callable.""" """Determine the job type from the callable."""
# Check for partials to properly determine if coroutine function # Check for partials to properly determine if coroutine function
@ -370,6 +380,7 @@ class HomeAssistant:
# Timeout handler for Core/Helper namespace # Timeout handler for Core/Helper namespace
self.timeout: TimeoutManager = TimeoutManager() self.timeout: TimeoutManager = TimeoutManager()
self._stop_future: concurrent.futures.Future[None] | None = None self._stop_future: concurrent.futures.Future[None] | None = None
self._shutdown_jobs: list[HassJobWithArgs] = []
@property @property
def is_running(self) -> bool: def is_running(self) -> bool:
@ -766,6 +777,42 @@ class HomeAssistant:
for task in pending: for task in pending:
_LOGGER.debug("Waited %s seconds for task: %s", wait_time, task) _LOGGER.debug("Waited %s seconds for task: %s", wait_time, task)
@overload
@callback
def async_add_shutdown_job(
self, hassjob: HassJob[..., Coroutine[Any, Any, Any]], *args: Any
) -> CALLBACK_TYPE:
...
@overload
@callback
def async_add_shutdown_job(
self, hassjob: HassJob[..., Coroutine[Any, Any, Any] | Any], *args: Any
) -> CALLBACK_TYPE:
...
@callback
def async_add_shutdown_job(
self, hassjob: HassJob[..., Coroutine[Any, Any, Any] | Any], *args: Any
) -> CALLBACK_TYPE:
"""Add a HassJob which will be executed on shutdown.
This method must be run in the event loop.
hassjob: HassJob
args: parameters for method to call.
Returns function to remove the job.
"""
job_with_args = HassJobWithArgs(hassjob, args)
self._shutdown_jobs.append(job_with_args)
@callback
def remove_job() -> None:
self._shutdown_jobs.remove(job_with_args)
return remove_job
def stop(self) -> None: def stop(self) -> None:
"""Stop Home Assistant and shuts down all threads.""" """Stop Home Assistant and shuts down all threads."""
if self.state == CoreState.not_running: # just ignore if self.state == CoreState.not_running: # just ignore
@ -799,6 +846,26 @@ class HomeAssistant:
"Stopping Home Assistant before startup has completed may fail" "Stopping Home Assistant before startup has completed may fail"
) )
# Stage 1 - Run shutdown jobs
try:
async with self.timeout.async_timeout(STOPPING_STAGE_SHUTDOWN_TIMEOUT):
tasks: list[asyncio.Future[Any]] = []
for job in self._shutdown_jobs:
task_or_none = self.async_run_hass_job(job.job, *job.args)
if not task_or_none:
continue
tasks.append(task_or_none)
if tasks:
asyncio.gather(*tasks, return_exceptions=True)
except asyncio.TimeoutError:
_LOGGER.warning(
"Timed out waiting for shutdown jobs to complete, the shutdown will"
" continue"
)
self._async_log_running_tasks("run shutdown jobs")
# Stage 2 - Stop integrations
# Keep holding the reference to the tasks but do not allow them # Keep holding the reference to the tasks but do not allow them
# to block shutdown. Only tasks created after this point will # to block shutdown. Only tasks created after this point will
# be waited for. # be waited for.
@ -816,33 +883,32 @@ class HomeAssistant:
self.exit_code = exit_code self.exit_code = exit_code
# stage 1
self.state = CoreState.stopping self.state = CoreState.stopping
self.bus.async_fire(EVENT_HOMEASSISTANT_STOP) self.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
try: try:
async with self.timeout.async_timeout(STAGE_1_SHUTDOWN_TIMEOUT): async with self.timeout.async_timeout(STOP_STAGE_SHUTDOWN_TIMEOUT):
await self.async_block_till_done() await self.async_block_till_done()
except asyncio.TimeoutError: except asyncio.TimeoutError:
_LOGGER.warning( _LOGGER.warning(
"Timed out waiting for shutdown stage 1 to complete, the shutdown will" "Timed out waiting for integrations to stop, the shutdown will"
" continue" " continue"
) )
self._async_log_running_tasks(1) self._async_log_running_tasks("stop integrations")
# stage 2 # Stage 3 - Final write
self.state = CoreState.final_write self.state = CoreState.final_write
self.bus.async_fire(EVENT_HOMEASSISTANT_FINAL_WRITE) self.bus.async_fire(EVENT_HOMEASSISTANT_FINAL_WRITE)
try: try:
async with self.timeout.async_timeout(STAGE_2_SHUTDOWN_TIMEOUT): async with self.timeout.async_timeout(FINAL_WRITE_STAGE_SHUTDOWN_TIMEOUT):
await self.async_block_till_done() await self.async_block_till_done()
except asyncio.TimeoutError: except asyncio.TimeoutError:
_LOGGER.warning( _LOGGER.warning(
"Timed out waiting for shutdown stage 2 to complete, the shutdown will" "Timed out waiting for final writes to complete, the shutdown will"
" continue" " continue"
) )
self._async_log_running_tasks(2) self._async_log_running_tasks("final write")
# stage 3 # Stage 4 - Close
self.state = CoreState.not_running self.state = CoreState.not_running
self.bus.async_fire(EVENT_HOMEASSISTANT_CLOSE) self.bus.async_fire(EVENT_HOMEASSISTANT_CLOSE)
@ -856,12 +922,12 @@ class HomeAssistant:
# were awaiting another task # were awaiting another task
continue continue
_LOGGER.warning( _LOGGER.warning(
"Task %s was still running after stage 2 shutdown; " "Task %s was still running after final writes shutdown stage; "
"Integrations should cancel non-critical tasks when receiving " "Integrations should cancel non-critical tasks when receiving "
"the stop event to prevent delaying shutdown", "the stop event to prevent delaying shutdown",
task, task,
) )
task.cancel("Home Assistant stage 2 shutdown") task.cancel("Home Assistant final writes shutdown stage")
try: try:
async with asyncio.timeout(0.1): async with asyncio.timeout(0.1):
await task await task
@ -870,11 +936,11 @@ class HomeAssistant:
except asyncio.TimeoutError: except asyncio.TimeoutError:
# Task may be shielded from cancellation. # Task may be shielded from cancellation.
_LOGGER.exception( _LOGGER.exception(
"Task %s could not be canceled during stage 3 shutdown", task "Task %s could not be canceled during final shutdown stage", task
) )
except Exception as exc: # pylint: disable=broad-except except Exception as exc: # pylint: disable=broad-except
_LOGGER.exception( _LOGGER.exception(
"Task %s error during stage 3 shutdown: %s", task, exc "Task %s error during final shutdown stage: %s", task, exc
) )
# Prevent run_callback_threadsafe from scheduling any additional # Prevent run_callback_threadsafe from scheduling any additional
@ -885,14 +951,14 @@ class HomeAssistant:
shutdown_run_callback_threadsafe(self.loop) shutdown_run_callback_threadsafe(self.loop)
try: try:
async with self.timeout.async_timeout(STAGE_3_SHUTDOWN_TIMEOUT): async with self.timeout.async_timeout(CLOSE_STAGE_SHUTDOWN_TIMEOUT):
await self.async_block_till_done() await self.async_block_till_done()
except asyncio.TimeoutError: except asyncio.TimeoutError:
_LOGGER.warning( _LOGGER.warning(
"Timed out waiting for shutdown stage 3 to complete, the shutdown will" "Timed out waiting for close event to be processed, the shutdown will"
" continue" " continue"
) )
self._async_log_running_tasks(3) self._async_log_running_tasks("close")
self.state = CoreState.stopped self.state = CoreState.stopped
@ -912,10 +978,10 @@ class HomeAssistant:
): ):
handle.cancel() handle.cancel()
def _async_log_running_tasks(self, stage: int) -> None: def _async_log_running_tasks(self, stage: str) -> None:
"""Log all running tasks.""" """Log all running tasks."""
for task in self._tasks: for task in self._tasks:
_LOGGER.warning("Shutdown stage %s: still running: %s", stage, task) _LOGGER.warning("Shutdown stage '%s': still running: %s", stage, task)
class Context: class Context:

View file

@ -59,9 +59,10 @@ WORKDIR /config
def _generate_dockerfile() -> str: def _generate_dockerfile() -> str:
timeout = ( timeout = (
core.STAGE_1_SHUTDOWN_TIMEOUT core.STOPPING_STAGE_SHUTDOWN_TIMEOUT
+ core.STAGE_2_SHUTDOWN_TIMEOUT + core.STOP_STAGE_SHUTDOWN_TIMEOUT
+ core.STAGE_3_SHUTDOWN_TIMEOUT + core.FINAL_WRITE_STAGE_SHUTDOWN_TIMEOUT
+ core.CLOSE_STAGE_SHUTDOWN_TIMEOUT
+ executor.EXECUTOR_SHUTDOWN_TIMEOUT + executor.EXECUTOR_SHUTDOWN_TIMEOUT
+ thread.THREADING_SHUTDOWN_TIMEOUT + thread.THREADING_SHUTDOWN_TIMEOUT
+ 10 + 10

View file

@ -36,6 +36,7 @@ from homeassistant.const import (
) )
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.core import ( from homeassistant.core import (
CoreState,
HassJob, HassJob,
HomeAssistant, HomeAssistant,
ServiceCall, ServiceCall,
@ -399,6 +400,32 @@ async def test_stage_shutdown(hass: HomeAssistant) -> None:
assert len(test_all) == 2 assert len(test_all) == 2
async def test_stage_shutdown_timeouts(hass: HomeAssistant) -> None:
"""Simulate a shutdown, test timeouts at each step."""
with patch.object(hass.timeout, "async_timeout", side_effect=asyncio.TimeoutError):
await hass.async_stop()
assert hass.state == CoreState.stopped
async def test_stage_shutdown_generic_error(hass: HomeAssistant, caplog) -> None:
"""Simulate a shutdown, test that a generic error at the final stage doesn't prevent it."""
task = asyncio.Future()
hass._tasks.add(task)
def fail_the_task(_):
task.set_exception(Exception("test_exception"))
with patch.object(task, "cancel", side_effect=fail_the_task) as patched_call:
await hass.async_stop()
assert patched_call.called
assert "test_exception" in caplog.text
assert hass.state == ha.CoreState.stopped
async def test_stage_shutdown_with_exit_code(hass: HomeAssistant) -> None: async def test_stage_shutdown_with_exit_code(hass: HomeAssistant) -> None:
"""Simulate a shutdown, test calling stuff with exit code checks.""" """Simulate a shutdown, test calling stuff with exit code checks."""
test_stop = async_capture_events(hass, EVENT_HOMEASSISTANT_STOP) test_stop = async_capture_events(hass, EVENT_HOMEASSISTANT_STOP)
@ -2566,3 +2593,30 @@ def test_hassjob_passing_job_type():
HassJob(not_callback_func, job_type=ha.HassJobType.Callback).job_type HassJob(not_callback_func, job_type=ha.HassJobType.Callback).job_type
== ha.HassJobType.Callback == ha.HassJobType.Callback
) )
async def test_shutdown_job(hass: HomeAssistant) -> None:
"""Test async_add_shutdown_job."""
evt = asyncio.Event()
async def shutdown_func() -> None:
evt.set()
job = HassJob(shutdown_func, "shutdown_job")
hass.async_add_shutdown_job(job)
await hass.async_stop()
assert evt.is_set()
async def test_cancel_shutdown_job(hass: HomeAssistant) -> None:
"""Test cancelling a job added to async_add_shutdown_job."""
evt = asyncio.Event()
async def shutdown_func() -> None:
evt.set()
job = HassJob(shutdown_func, "shutdown_job")
cancel = hass.async_add_shutdown_job(job)
cancel()
await hass.async_stop()
assert not evt.is_set()

View file

@ -13,7 +13,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.util import executor, thread from homeassistant.util import executor, thread
# https://github.com/home-assistant/supervisor/blob/main/supervisor/docker/homeassistant.py # https://github.com/home-assistant/supervisor/blob/main/supervisor/docker/homeassistant.py
SUPERVISOR_HARD_TIMEOUT = 220 SUPERVISOR_HARD_TIMEOUT = 240
TIMEOUT_SAFETY_MARGIN = 10 TIMEOUT_SAFETY_MARGIN = 10
@ -21,9 +21,10 @@ TIMEOUT_SAFETY_MARGIN = 10
async def test_cumulative_shutdown_timeout_less_than_supervisor() -> None: async def test_cumulative_shutdown_timeout_less_than_supervisor() -> None:
"""Verify the cumulative shutdown timeout is at least 10s less than the supervisor.""" """Verify the cumulative shutdown timeout is at least 10s less than the supervisor."""
assert ( assert (
core.STAGE_1_SHUTDOWN_TIMEOUT core.STOPPING_STAGE_SHUTDOWN_TIMEOUT
+ core.STAGE_2_SHUTDOWN_TIMEOUT + core.STOP_STAGE_SHUTDOWN_TIMEOUT
+ core.STAGE_3_SHUTDOWN_TIMEOUT + core.FINAL_WRITE_STAGE_SHUTDOWN_TIMEOUT
+ core.CLOSE_STAGE_SHUTDOWN_TIMEOUT
+ executor.EXECUTOR_SHUTDOWN_TIMEOUT + executor.EXECUTOR_SHUTDOWN_TIMEOUT
+ thread.THREADING_SHUTDOWN_TIMEOUT + thread.THREADING_SHUTDOWN_TIMEOUT
+ TIMEOUT_SAFETY_MARGIN + TIMEOUT_SAFETY_MARGIN