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
This commit is contained in:
Stefan Agner 2025-02-14 11:20:08 +00:00
parent 2cb2a48184
commit 3d74d2bb7c
No known key found for this signature in database
GPG Key ID: AE01353D1E44747D

View File

@ -266,6 +266,7 @@ class HomeAssistantWebSocket(CoreSysAttributes):
try:
await self._client.async_send_command(message)
except HomeAssistantWSConnectionError:
if self._client:
await self._client.close()
self._client = None
@ -277,6 +278,7 @@ class HomeAssistantWebSocket(CoreSysAttributes):
try:
return await self._client.async_send_command(message)
except HomeAssistantWSConnectionError:
if self._client:
await self._client.close()
self._client = None
raise