mirror of
https://github.com/home-assistant/core.git
synced 2026-11-28 08:38:33 -04:00
Fix shutdown deadlock with run_callback_threadsafe (#45807)
This commit is contained in:
@@ -169,6 +169,30 @@ async def test_stage_shutdown(hass):
|
||||
assert len(test_all) == 2
|
||||
|
||||
|
||||
async def test_shutdown_calls_block_till_done_after_shutdown_run_callback_threadsafe(
|
||||
hass,
|
||||
):
|
||||
"""Ensure shutdown_run_callback_threadsafe is called before the final async_block_till_done."""
|
||||
stop_calls = []
|
||||
|
||||
async def _record_block_till_done():
|
||||
nonlocal stop_calls
|
||||
stop_calls.append("async_block_till_done")
|
||||
|
||||
def _record_shutdown_run_callback_threadsafe(loop):
|
||||
nonlocal stop_calls
|
||||
stop_calls.append(("shutdown_run_callback_threadsafe", loop))
|
||||
|
||||
with patch.object(hass, "async_block_till_done", _record_block_till_done), patch(
|
||||
"homeassistant.core.shutdown_run_callback_threadsafe",
|
||||
_record_shutdown_run_callback_threadsafe,
|
||||
):
|
||||
await hass.async_stop()
|
||||
|
||||
assert stop_calls[-2] == ("shutdown_run_callback_threadsafe", hass.loop)
|
||||
assert stop_calls[-1] == "async_block_till_done"
|
||||
|
||||
|
||||
async def test_pending_sheduler(hass):
|
||||
"""Add a coro to pending tasks."""
|
||||
call_count = []
|
||||
|
||||
@@ -50,7 +50,8 @@ def test_fire_coroutine_threadsafe_from_inside_event_loop(
|
||||
def test_run_callback_threadsafe_from_inside_event_loop(mock_ident, _):
|
||||
"""Testing calling run_callback_threadsafe from inside an event loop."""
|
||||
callback = MagicMock()
|
||||
loop = MagicMock()
|
||||
|
||||
loop = Mock(spec=["call_soon_threadsafe"])
|
||||
|
||||
loop._thread_ident = None
|
||||
mock_ident.return_value = 5
|
||||
@@ -168,3 +169,45 @@ async def test_gather_with_concurrency():
|
||||
)
|
||||
|
||||
assert results == [2, 2, -1, -1]
|
||||
|
||||
|
||||
async def test_shutdown_run_callback_threadsafe(hass):
|
||||
"""Test we can shutdown run_callback_threadsafe."""
|
||||
hasync.shutdown_run_callback_threadsafe(hass.loop)
|
||||
callback = MagicMock()
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
hasync.run_callback_threadsafe(hass.loop, callback)
|
||||
|
||||
|
||||
async def test_run_callback_threadsafe(hass):
|
||||
"""Test run_callback_threadsafe runs code in the event loop."""
|
||||
it_ran = False
|
||||
|
||||
def callback():
|
||||
nonlocal it_ran
|
||||
it_ran = True
|
||||
|
||||
assert hasync.run_callback_threadsafe(hass.loop, callback)
|
||||
assert it_ran is False
|
||||
|
||||
# Verify that async_block_till_done will flush
|
||||
# out the callback
|
||||
await hass.async_block_till_done()
|
||||
assert it_ran is True
|
||||
|
||||
|
||||
async def test_callback_is_always_scheduled(hass):
|
||||
"""Test run_callback_threadsafe always calls call_soon_threadsafe before checking for shutdown."""
|
||||
# We have to check the shutdown state AFTER the callback is scheduled otherwise
|
||||
# the function could continue on and the caller call `future.result()` after
|
||||
# the point in the main thread where callbacks are no longer run.
|
||||
|
||||
callback = MagicMock()
|
||||
hasync.shutdown_run_callback_threadsafe(hass.loop)
|
||||
|
||||
with patch.object(hass.loop, "call_soon_threadsafe") as mock_call_soon_threadsafe:
|
||||
with pytest.raises(RuntimeError):
|
||||
hasync.run_callback_threadsafe(hass.loop, callback)
|
||||
|
||||
mock_call_soon_threadsafe.assert_called_once()
|
||||
|
||||
Reference in New Issue
Block a user