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
This commit is contained in:
Stefan Agner 2025-02-14 13:12:56 +01:00 committed by GitHub
parent 2cb2a48184
commit 9b2dbd634d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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