Fix issue with concurrent reads (#2610)

* Fix issue with concurrent reads

* Use lock

* acquire/release

* handle ConnectionError

* No there was not mulitple issues, it's the same code just different syle.....Which is stated on the same page as that image...
This commit is contained in:
Joakim Sørensen 2021-02-24 18:36:21 +01:00 committed by GitHub
parent 8630adc54a
commit cb5932cb8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 16 deletions

View File

@ -612,7 +612,7 @@ class Addon(AddonModel):
try:
await self.instance.run()
except DockerRequestError as err:
self.state = AddonState.STOPPED
self.state = AddonState.ERROR
raise AddonsError() from err
except DockerError as err:
self.state = AddonState.ERROR
@ -625,6 +625,7 @@ class Addon(AddonModel):
try:
await self.instance.stop()
except DockerRequestError as err:
self.state = AddonState.ERROR
raise AddonsError() from err
except DockerError as err:
self.state = AddonState.ERROR

View File

@ -1,4 +1,5 @@
"""Home Assistant Websocket API."""
import asyncio
import logging
from typing import Any, Dict, Optional
@ -24,29 +25,34 @@ class WSClient:
self, ha_version: AwesomeVersion, client: aiohttp.ClientWebSocketResponse
):
"""Initialise the WS client."""
self.ha_version = ha_version
self.client = client
self.message_id = 0
self.ha_version: AwesomeVersion = ha_version
self.client: aiohttp.ClientWebSocketResponse = client
self.message_id: int = 0
self._lock: asyncio.Lock = asyncio.Lock()
async def async_send_command(self, message: Dict[str, Any]):
"""Send a websocket command."""
self.message_id += 1
message["id"] = self.message_id
async with self._lock:
self.message_id += 1
message["id"] = self.message_id
_LOGGER.debug("Sending: %s", message)
try:
await self.client.send_json(message)
except HomeAssistantWSNotSupported:
return
_LOGGER.debug("Sending: %s", message)
try:
await self.client.send_json(message)
except ConnectionError:
return
response = await self.client.receive_json()
try:
response = await self.client.receive_json()
except ConnectionError:
return
_LOGGER.debug("Received: %s", response)
_LOGGER.debug("Received: %s", response)
if response["success"]:
return response["result"]
if response["success"]:
return response["result"]
raise HomeAssistantWSError(response)
raise HomeAssistantWSError(response)
@classmethod
async def connect_with_auth(