From 51f847c4b8b8b4b5ec43adf908804e0118a60be7 Mon Sep 17 00:00:00 2001 From: SigmaPic Date: Tue, 3 Nov 2020 10:35:45 +0100 Subject: [PATCH] Catch unhandled exception in websocket_api (#42693) Co-authored-by: springstan <46536646+springstan@users.noreply.github.com> Co-authored-by: Paulus Schoutsen --- .../components/websocket_api/http.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/websocket_api/http.py b/homeassistant/components/websocket_api/http.py index b71b19d5181..6f010153563 100644 --- a/homeassistant/components/websocket_api/http.py +++ b/homeassistant/components/websocket_api/http.py @@ -222,20 +222,20 @@ class WebSocketHandler: self._to_write.put_nowait(None) # Make sure all error messages are written before closing await self._writer_task - except asyncio.QueueFull: + await wsock.close() + except asyncio.QueueFull: # can be raised by put_nowait self._writer_task.cancel() - await wsock.close() + finally: + if disconnect_warn is None: + self._logger.debug("Disconnected") + else: + self._logger.warning("Disconnected: %s", disconnect_warn) - if disconnect_warn is None: - self._logger.debug("Disconnected") - else: - self._logger.warning("Disconnected: %s", disconnect_warn) - - if connection is not None: - self.hass.data[DATA_CONNECTIONS] -= 1 - self.hass.helpers.dispatcher.async_dispatcher_send( - SIGNAL_WEBSOCKET_DISCONNECTED - ) + if connection is not None: + self.hass.data[DATA_CONNECTIONS] -= 1 + self.hass.helpers.dispatcher.async_dispatcher_send( + SIGNAL_WEBSOCKET_DISCONNECTED + ) return wsock