From 3d74d2bb7cf4eb1f3b6c6b7a177342dc6d566b0a Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Fri, 14 Feb 2025 11:20:08 +0000 Subject: [PATCH] Avoid exception when handling closed WebSocket connection When delivering multiple messages to Core, and the first fails with a connection error, the second message will also fail with the same error. But at this point the connection is already clsoed, which leads to an exception in the exception handler. Avoid this compunding error by checking if the connection is still exists before trying to close. Fixes: #5629 --- supervisor/homeassistant/websocket.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/supervisor/homeassistant/websocket.py b/supervisor/homeassistant/websocket.py index 2c54eda64..15c16234e 100644 --- a/supervisor/homeassistant/websocket.py +++ b/supervisor/homeassistant/websocket.py @@ -266,7 +266,8 @@ class HomeAssistantWebSocket(CoreSysAttributes): try: await self._client.async_send_command(message) except HomeAssistantWSConnectionError: - await self._client.close() + if self._client: + await self._client.close() self._client = None async def async_send_command(self, message: dict[str, Any]) -> dict[str, Any]: @@ -277,7 +278,8 @@ class HomeAssistantWebSocket(CoreSysAttributes): try: return await self._client.async_send_command(message) except HomeAssistantWSConnectionError: - await self._client.close() + if self._client: + await self._client.close() self._client = None raise