mirror of
https://github.com/home-assistant/core.git
synced 2026-02-28 13:56:28 +00:00
Mark executor jobs as background unless created from a tracked task (#114450)
* Mark executor jobs as background unless created from a tracked task If the current task is not tracked the executor job should not be a background task to avoid delaying startup and shutdown. Currently any executor job created in a untracked task or background task would end up being tracked and delaying startup/shutdown * import exec has the same issue * Avoid tracking import executor jobs There is no reason to track these jobs as they are always awaited and we do not want to support fire and forget import executor jobs * fix xiaomi_miio * lots of fire time changed without background await * revert changes moved to other PR * more * more * more * m * m * p * fix fire and forget tests * scrape * sonos * system * more * capture callback before block * coverage * more * more races * more races * more * missed some * more fixes * missed some more * fix * remove unneeded * one more race * two
This commit is contained in:
@@ -78,7 +78,7 @@ hass.states.set('test.entity', data.get('name', 'not set'))
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {"name": "paulus"})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert hass.states.is_state("test.entity", "paulus")
|
||||
|
||||
@@ -96,7 +96,7 @@ print("This triggers warning.")
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert "Don't use print() inside scripts." in caplog.text
|
||||
|
||||
@@ -111,7 +111,7 @@ logger.info('Logging from inside script')
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert "Logging from inside script" in caplog.text
|
||||
|
||||
@@ -126,7 +126,7 @@ this is not valid Python
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert "Error loading script test.py" in caplog.text
|
||||
|
||||
@@ -140,8 +140,8 @@ async def test_execute_runtime_error(
|
||||
raise Exception('boom')
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert "Error executing script" in caplog.text
|
||||
|
||||
@@ -153,7 +153,7 @@ raise Exception('boom')
|
||||
"""
|
||||
|
||||
task = hass.async_add_executor_job(execute, hass, "test.py", source, {}, True)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert type(task.exception()) == HomeAssistantError
|
||||
assert "Error executing script (Exception): boom" in str(task.exception())
|
||||
@@ -168,7 +168,7 @@ async def test_accessing_async_methods(
|
||||
hass.async_stop()
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Not allowed to access async methods" in caplog.text
|
||||
@@ -181,7 +181,7 @@ hass.async_stop()
|
||||
"""
|
||||
|
||||
task = hass.async_add_executor_job(execute, hass, "test.py", source, {}, True)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert type(task.exception()) == ServiceValidationError
|
||||
assert "Not allowed to access async methods" in str(task.exception())
|
||||
@@ -198,7 +198,7 @@ mylist = [1, 2, 3, 4]
|
||||
logger.info('Logging from inside script: %s %s' % (mydict["a"], mylist[2]))
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Logging from inside script: 1 3" in caplog.text
|
||||
@@ -217,7 +217,7 @@ async def test_accessing_forbidden_methods(
|
||||
"time.tzset()": "TimeWrapper.tzset",
|
||||
}.items():
|
||||
caplog.records.clear()
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
assert f"Not allowed to access {name}" in caplog.text
|
||||
|
||||
@@ -231,7 +231,7 @@ async def test_accessing_forbidden_methods_with_response(hass: HomeAssistant) ->
|
||||
"time.tzset()": "TimeWrapper.tzset",
|
||||
}.items():
|
||||
task = hass.async_add_executor_job(execute, hass, "test.py", source, {}, True)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert type(task.exception()) == ServiceValidationError
|
||||
assert f"Not allowed to access {name}" in str(task.exception())
|
||||
@@ -244,7 +244,7 @@ for i in [1, 2]:
|
||||
hass.states.set('hello.{}'.format(i), 'world')
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.is_state("hello.1", "world")
|
||||
@@ -279,7 +279,7 @@ hass.states.set('hello.ab_list', '{}'.format(ab_list))
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert hass.states.is_state("hello.a", "1")
|
||||
assert hass.states.is_state("hello.b", "2")
|
||||
@@ -302,7 +302,7 @@ hass.states.set('hello.b', a[1])
|
||||
hass.states.set('hello.c', a[2])
|
||||
"""
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert hass.states.is_state("hello.a", "1")
|
||||
assert hass.states.is_state("hello.b", "2")
|
||||
@@ -325,7 +325,7 @@ hass.states.set('module.datetime',
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert hass.states.is_state("module.time", "1986")
|
||||
assert hass.states.is_state("module.time_strptime", "12:34")
|
||||
@@ -351,7 +351,7 @@ def b():
|
||||
b()
|
||||
"""
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert hass.states.is_state("hello.a", "one")
|
||||
assert hass.states.is_state("hello.b", "two")
|
||||
@@ -517,7 +517,7 @@ time.sleep(5)
|
||||
|
||||
with patch("homeassistant.components.python_script.time.sleep"):
|
||||
hass.async_add_executor_job(execute, hass, "test.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert caplog.text.count("time.sleep") == 1
|
||||
|
||||
@@ -664,7 +664,7 @@ hass.states.set('hello.c', c)
|
||||
"""
|
||||
|
||||
hass.async_add_executor_job(execute, hass, "aug_assign.py", source, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert hass.states.get("hello.a").state == str(((10 + 20) * 5) - 8)
|
||||
assert hass.states.get("hello.b").state == ("foo" + "bar") * 2
|
||||
@@ -686,5 +686,5 @@ async def test_prohibited_augmented_assignment_operations(
|
||||
) -> None:
|
||||
"""Test that prohibited augmented assignment operations raise an error."""
|
||||
hass.async_add_executor_job(execute, hass, "aug_assign_prohibited.py", case, {})
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
assert error in caplog.text
|
||||
|
||||
Reference in New Issue
Block a user