mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Prevent spawning script runs when shutting down (#68170)
This commit is contained in:
parent
f7cb10e2f5
commit
1a27025793
@ -110,6 +110,7 @@ ATTR_MAX = "max"
|
|||||||
|
|
||||||
DATA_SCRIPTS = "helpers.script"
|
DATA_SCRIPTS = "helpers.script"
|
||||||
DATA_SCRIPT_BREAKPOINTS = "helpers.script_breakpoints"
|
DATA_SCRIPT_BREAKPOINTS = "helpers.script_breakpoints"
|
||||||
|
DATA_NEW_SCRIPT_RUNS_NOT_ALLOWED = "helpers.script_not_allowed"
|
||||||
RUN_ID_ANY = "*"
|
RUN_ID_ANY = "*"
|
||||||
NODE_ANY = "*"
|
NODE_ANY = "*"
|
||||||
|
|
||||||
@ -883,6 +884,7 @@ class _QueuedScriptRun(_ScriptRun):
|
|||||||
|
|
||||||
async def _async_stop_scripts_after_shutdown(hass, point_in_time):
|
async def _async_stop_scripts_after_shutdown(hass, point_in_time):
|
||||||
"""Stop running Script objects started after shutdown."""
|
"""Stop running Script objects started after shutdown."""
|
||||||
|
hass.data[DATA_NEW_SCRIPT_RUNS_NOT_ALLOWED] = None
|
||||||
running_scripts = [
|
running_scripts = [
|
||||||
script for script in hass.data[DATA_SCRIPTS] if script["instance"].is_running
|
script for script in hass.data[DATA_SCRIPTS] if script["instance"].is_running
|
||||||
]
|
]
|
||||||
@ -1192,6 +1194,12 @@ class Script:
|
|||||||
)
|
)
|
||||||
context = Context()
|
context = Context()
|
||||||
|
|
||||||
|
# Prevent spawning new script runs when Home Assistant is shutting down
|
||||||
|
if DATA_NEW_SCRIPT_RUNS_NOT_ALLOWED in self._hass.data:
|
||||||
|
self._log("Home Assistant is shutting down, starting script blocked")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Prevent spawning new script runs if not allowed by script mode
|
||||||
if self.is_running:
|
if self.is_running:
|
||||||
if self.script_mode == SCRIPT_MODE_SINGLE:
|
if self.script_mode == SCRIPT_MODE_SINGLE:
|
||||||
if self._max_exceeded != "SILENT":
|
if self._max_exceeded != "SILENT":
|
||||||
|
@ -3276,6 +3276,26 @@ async def test_shutdown_after(hass, caplog):
|
|||||||
assert_action_trace(expected_trace)
|
assert_action_trace(expected_trace)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_start_script_after_shutdown(hass, caplog):
|
||||||
|
"""Test starting scripts after shutdown is blocked."""
|
||||||
|
delay_alias = "delay step"
|
||||||
|
sequence = cv.SCRIPT_SCHEMA({"delay": {"seconds": 120}, "alias": delay_alias})
|
||||||
|
script_obj = script.Script(hass, sequence, "test script", "test_domain")
|
||||||
|
|
||||||
|
# Trigger 1st stage script shutdown
|
||||||
|
hass.state = CoreState.stopping
|
||||||
|
hass.bus.async_fire("homeassistant_stop")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
# Trigger 2nd stage script shutdown
|
||||||
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=60))
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Attempt to spawn additional script run
|
||||||
|
await script_obj.async_run(context=Context())
|
||||||
|
assert not script_obj.is_running
|
||||||
|
assert "Home Assistant is shutting down, starting script blocked" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
async def test_update_logger(hass, caplog):
|
async def test_update_logger(hass, caplog):
|
||||||
"""Test updating logger."""
|
"""Test updating logger."""
|
||||||
sequence = cv.SCRIPT_SCHEMA({"event": "test_event"})
|
sequence = cv.SCRIPT_SCHEMA({"event": "test_event"})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user