From 9b2dbd634dcaff1bd4aced0ab076a52cea110339 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Fri, 14 Feb 2025 13:12:56 +0100 Subject: [PATCH] Avoid exception when handling closed WebSocket connection (#5630) 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