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

View File

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