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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue