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:
J. Nick Koston 2024-03-29 18:16:53 -10:00 committed by GitHub
parent aec7a67a58
commit 9a79320861
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 246 additions and 180 deletions

View file

@ -97,7 +97,7 @@ async def test_sensor_process_fails(
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
process_sensor = hass.states.get("binary_sensor.system_monitor_process_python3")
assert process_sensor is not None

View file

@ -232,7 +232,7 @@ async def test_sensor_updating(
mock_psutil.virtual_memory.side_effect = Exception("Failed to update")
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
memory_sensor = hass.states.get("sensor.system_monitor_memory_free")
assert memory_sensor is not None
@ -248,7 +248,7 @@ async def test_sensor_updating(
)
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
memory_sensor = hass.states.get("sensor.system_monitor_memory_free")
assert memory_sensor is not None
@ -293,7 +293,7 @@ async def test_sensor_process_fails(
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
process_sensor = hass.states.get("sensor.system_monitor_process_python3")
assert process_sensor is not None
@ -330,7 +330,7 @@ async def test_sensor_network_sensors(
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
network_out_sensor = hass.states.get("sensor.system_monitor_network_out_eth1")
packets_out_sensor = hass.states.get("sensor.system_monitor_packets_out_eth1")
@ -362,7 +362,7 @@ async def test_sensor_network_sensors(
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
network_out_sensor = hass.states.get("sensor.system_monitor_network_out_eth1")
packets_out_sensor = hass.states.get("sensor.system_monitor_packets_out_eth1")
@ -470,7 +470,7 @@ async def test_exception_handling_disk_sensor(
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
assert "OS error for /" in caplog.text
@ -483,7 +483,7 @@ async def test_exception_handling_disk_sensor(
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
assert "OS error for /" in caplog.text
@ -498,7 +498,7 @@ async def test_exception_handling_disk_sensor(
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
disk_sensor = hass.states.get("sensor.system_monitor_disk_free")
assert disk_sensor is not None
@ -528,7 +528,7 @@ async def test_cpu_percentage_is_zero_returns_unknown(
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
cpu_sensor = hass.states.get("sensor.system_monitor_processor_use")
assert cpu_sensor is not None
@ -538,7 +538,7 @@ async def test_cpu_percentage_is_zero_returns_unknown(
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
cpu_sensor = hass.states.get("sensor.system_monitor_processor_use")
assert cpu_sensor is not None
@ -573,7 +573,7 @@ async def test_remove_obsolete_entities(
)
freezer.tick(timedelta(minutes=5))
async_fire_time_changed(hass)
await hass.async_block_till_done()
await hass.async_block_till_done(wait_background_tasks=True)
# Fake an entity which should be removed as not supported and disabled
entity_registry.async_get_or_create(