Terminate scripts with until and while conditions that execute more than 10000 times (#115110)
This commit is contained in:
parent
771fe57e32
commit
569f54d8e3
2 changed files with 115 additions and 0 deletions
|
@ -280,6 +280,9 @@ STATIC_VALIDATION_ACTION_TYPES = (
|
|||
cv.SCRIPT_ACTION_WAIT_TEMPLATE,
|
||||
)
|
||||
|
||||
REPEAT_WARN_ITERATIONS = 5000
|
||||
REPEAT_TERMINATE_ITERATIONS = 10000
|
||||
|
||||
|
||||
async def async_validate_actions_config(
|
||||
hass: HomeAssistant, actions: list[ConfigType]
|
||||
|
@ -839,6 +842,7 @@ class _ScriptRun:
|
|||
|
||||
# pylint: disable-next=protected-access
|
||||
script = self._script._get_repeat_script(self._step)
|
||||
warned_too_many_loops = False
|
||||
|
||||
async def async_run_sequence(iteration, extra_msg=""):
|
||||
self._log("Repeating %s: Iteration %i%s", description, iteration, extra_msg)
|
||||
|
@ -909,6 +913,36 @@ class _ScriptRun:
|
|||
_LOGGER.warning("Error in 'while' evaluation:\n%s", ex)
|
||||
break
|
||||
|
||||
if iteration > 1:
|
||||
if iteration > REPEAT_WARN_ITERATIONS:
|
||||
if not warned_too_many_loops:
|
||||
warned_too_many_loops = True
|
||||
_LOGGER.warning(
|
||||
"While condition %s in script `%s` looped %s times",
|
||||
repeat[CONF_WHILE],
|
||||
self._script.name,
|
||||
REPEAT_WARN_ITERATIONS,
|
||||
)
|
||||
|
||||
if iteration > REPEAT_TERMINATE_ITERATIONS:
|
||||
_LOGGER.critical(
|
||||
"While condition %s in script `%s` "
|
||||
"terminated because it looped %s times",
|
||||
repeat[CONF_WHILE],
|
||||
self._script.name,
|
||||
REPEAT_TERMINATE_ITERATIONS,
|
||||
)
|
||||
raise _AbortScript(
|
||||
f"While condition {repeat[CONF_WHILE]} "
|
||||
"terminated because it looped "
|
||||
f" {REPEAT_TERMINATE_ITERATIONS} times"
|
||||
)
|
||||
|
||||
# If the user creates a script with a tight loop,
|
||||
# yield to the event loop so the system stays
|
||||
# responsive while all the cpu time is consumed.
|
||||
await asyncio.sleep(0)
|
||||
|
||||
await async_run_sequence(iteration)
|
||||
|
||||
elif CONF_UNTIL in repeat:
|
||||
|
@ -927,6 +961,35 @@ class _ScriptRun:
|
|||
_LOGGER.warning("Error in 'until' evaluation:\n%s", ex)
|
||||
break
|
||||
|
||||
if iteration >= REPEAT_WARN_ITERATIONS:
|
||||
if not warned_too_many_loops:
|
||||
warned_too_many_loops = True
|
||||
_LOGGER.warning(
|
||||
"Until condition %s in script `%s` looped %s times",
|
||||
repeat[CONF_UNTIL],
|
||||
self._script.name,
|
||||
REPEAT_WARN_ITERATIONS,
|
||||
)
|
||||
|
||||
if iteration >= REPEAT_TERMINATE_ITERATIONS:
|
||||
_LOGGER.critical(
|
||||
"Until condition %s in script `%s` "
|
||||
"terminated because it looped %s times",
|
||||
repeat[CONF_UNTIL],
|
||||
self._script.name,
|
||||
REPEAT_TERMINATE_ITERATIONS,
|
||||
)
|
||||
raise _AbortScript(
|
||||
f"Until condition {repeat[CONF_UNTIL]} "
|
||||
"terminated because it looped "
|
||||
f"{REPEAT_TERMINATE_ITERATIONS} times"
|
||||
)
|
||||
|
||||
# If the user creates a script with a tight loop,
|
||||
# yield to the event loop so the system stays responsive
|
||||
# while all the cpu time is consumed.
|
||||
await asyncio.sleep(0)
|
||||
|
||||
if saved_repeat_vars:
|
||||
self._variables["repeat"] = saved_repeat_vars
|
||||
else:
|
||||
|
|
|
@ -2837,6 +2837,58 @@ async def test_repeat_nested(
|
|||
assert_action_trace(expected_trace)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("condition", "check"), [("while", "above"), ("until", "below")]
|
||||
)
|
||||
async def test_repeat_limits(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, condition: str, check: str
|
||||
) -> None:
|
||||
"""Test limits on repeats prevent the system from hanging."""
|
||||
event = "test_event"
|
||||
events = async_capture_events(hass, event)
|
||||
hass.states.async_set("sensor.test", "0.5")
|
||||
|
||||
sequence = {
|
||||
"repeat": {
|
||||
"sequence": [
|
||||
{
|
||||
"event": event,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
sequence["repeat"][condition] = {
|
||||
"condition": "numeric_state",
|
||||
"entity_id": "sensor.test",
|
||||
check: "0",
|
||||
}
|
||||
|
||||
with (
|
||||
patch.object(script, "REPEAT_WARN_ITERATIONS", 5),
|
||||
patch.object(script, "REPEAT_TERMINATE_ITERATIONS", 10),
|
||||
):
|
||||
script_obj = script.Script(
|
||||
hass, cv.SCRIPT_SCHEMA(sequence), f"Test {condition}", "test_domain"
|
||||
)
|
||||
|
||||
caplog.clear()
|
||||
caplog.set_level(logging.WARNING)
|
||||
|
||||
hass.async_create_task(script_obj.async_run(context=Context()))
|
||||
await asyncio.wait_for(hass.async_block_till_done(), 1)
|
||||
|
||||
title_condition = condition.title()
|
||||
|
||||
assert f"{title_condition} condition" in caplog.text
|
||||
assert f"in script `Test {condition}` looped 5 times" in caplog.text
|
||||
assert (
|
||||
f"script `Test {condition}` terminated because it looped 10 times"
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
assert len(events) == 10
|
||||
|
||||
|
||||
async def test_choose_warning(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue