Prevent pending writes from restoring removed storage (#186489)

This commit is contained in:
Paulus Schoutsen
2026-10-16 18:10:25 +02:00
committed by GitHub
parent d87bc279e9
commit c877b970b7
2 changed files with 42 additions and 2 deletions
+4 -2
View File
@@ -666,8 +666,10 @@ class Store[_T: Mapping[str, Any] | Sequence[Any]]:
async def async_remove(self) -> None:
"""Remove all data."""
self._io.async_invalidate_cache()
self._data = None
self._async_cleanup_delay_listener()
self._async_cleanup_final_write_listener()
await self._io.async_remove()
async with self._write_lock:
self._io.async_invalidate_cache()
await self._io.async_remove()
+38
View File
@@ -93,6 +93,44 @@ async def test_loading(hass: HomeAssistant, store: storage.Store) -> None:
assert data == MOCK_DATA
async def test_remove_pending_save(store: storage.Store) -> None:
"""Removal discards delayed data instead of returning it on the next load."""
await store.async_save(MOCK_DATA)
store.async_delay_save(lambda: MOCK_DATA2, 60)
await store.async_remove()
assert await store.async_load() is None
async def test_remove_during_write(
hass: HomeAssistant, store: storage.Store, hass_storage: dict[str, Any]
) -> None:
"""An active write cannot recreate removed storage."""
write_started = asyncio.Event()
finish_write = asyncio.Event()
async_write = store._io.async_write
async def blocked_write(mode: str, data: str | bytes) -> None:
write_started.set()
await finish_write.wait()
await async_write(mode, data)
with patch.object(store._io, "async_write", side_effect=blocked_write):
write_task = hass.async_create_task(store.async_save(MOCK_DATA))
await write_started.wait()
remove_task = hass.async_create_task(store.async_remove())
await asyncio.sleep(0)
finish_write.set()
await asyncio.gather(write_task, remove_task)
assert MOCK_KEY not in hass_storage
assert await store.async_load() is None
await store.async_save(MOCK_DATA2)
assert await store.async_load() == MOCK_DATA2
async def test_custom_encoder(hass: HomeAssistant) -> None:
"""Test we can save and load data."""