From 3c84c7ccf098f89efc165f1efb72466b6ca2f443 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 5 Nov 2020 22:27:41 -1000 Subject: [PATCH] Fix memory leak in websocket_api (#42894) * Fix memory leak in websocket_api Each named logger lives forever * tweak --- homeassistant/components/websocket_api/http.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/websocket_api/http.py b/homeassistant/components/websocket_api/http.py index 6f010153563..daa8529e8bd 100644 --- a/homeassistant/components/websocket_api/http.py +++ b/homeassistant/components/websocket_api/http.py @@ -27,6 +27,7 @@ from .error import Disconnect from .messages import message_to_json # mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs +_WS_LOGGER = logging.getLogger(f"{__name__}.connection") class WebsocketAPIView(HomeAssistantView): @@ -41,6 +42,14 @@ class WebsocketAPIView(HomeAssistantView): return await WebSocketHandler(request.app["hass"], request).async_handle() +class WebSocketAdapter(logging.LoggerAdapter): + """Add connection id to websocket messages.""" + + def process(self, msg, kwargs): + """Add connid to websocket log messages.""" + return f'[{self.extra["connid"]}] {msg}', kwargs + + class WebSocketHandler: """Handle an active websocket client connection.""" @@ -52,7 +61,7 @@ class WebSocketHandler: self._to_write: asyncio.Queue = asyncio.Queue(maxsize=MAX_PENDING_MSG) self._handle_task = None self._writer_task = None - self._logger = logging.getLogger("{}.connection.{}".format(__name__, id(self))) + self._logger = WebSocketAdapter(_WS_LOGGER, {"connid": id(self)}) self._peak_checker_unsub = None async def _writer(self):