From 6d0349637290600b2b64a99ea183667106f3dd27 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 21 May 2020 03:27:40 -0500 Subject: [PATCH] Ensure storage write consume the data under the lock (#35889) If two writes trigger at the same time the data would already be consumed. --- homeassistant/helpers/storage.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/homeassistant/helpers/storage.py b/homeassistant/helpers/storage.py index b5ac942bf2f..d2b4c334937 100644 --- a/homeassistant/helpers/storage.py +++ b/homeassistant/helpers/storage.py @@ -200,14 +200,19 @@ class Store: async def _async_handle_write_data(self, *_args): """Handle writing the config.""" - data = self._data - - if "data_func" in data: - data["data"] = data.pop("data_func")() - - self._data = None async with self._write_lock: + if self._data is None: + # Another write already consumed the data + return + + data = self._data + + if "data_func" in data: + data["data"] = data.pop("data_func")() + + self._data = None + try: await self.hass.async_add_executor_job( self._write_data, self.path, data