From 380798ef4aae7f6ae385b5308be3842f80d2391d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Cerm=C3=A1k?= Date: Mon, 3 Mar 2025 11:48:19 +0100 Subject: [PATCH 1/2] Suppress all ClientConnectionReset when returning logs In #5358 we started suppressing ClientConnectionReset when logs are returned from the Journal Gateway and the client ends connection unexpectedly. The connection can be closed also when the headers are returned, so ignore also that error. Refs #5606 --- supervisor/api/host.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/supervisor/api/host.py b/supervisor/api/host.py index 6e3399420ae..b3ff517a750 100644 --- a/supervisor/api/host.py +++ b/supervisor/api/host.py @@ -255,15 +255,15 @@ async def advanced_logs_handler( response.content_type = CONTENT_TYPE_TEXT headers_returned = False async for cursor, line in journal_logs_reader(resp, log_formatter): - if not headers_returned: - if cursor: - response.headers["X-First-Cursor"] = cursor - response.headers["X-Accel-Buffering"] = "no" - await response.prepare(request) - headers_returned = True # When client closes the connection while reading busy logs, we # sometimes get this exception. It should be safe to ignore it. with suppress(ClientConnectionResetError): + if not headers_returned: + if cursor: + response.headers["X-First-Cursor"] = cursor + response.headers["X-Accel-Buffering"] = "no" + await response.prepare(request) + headers_returned = True await response.write(line.encode("utf-8") + b"\n") except ConnectionResetError as ex: raise APIError( From b25a92e9666af3ef98fba06823b4aa433079c880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Cerm=C3=A1k?= Date: Mon, 3 Mar 2025 13:16:15 +0100 Subject: [PATCH 2/2] Log ClientConnectionResetError as DEBUG instead of suppressing it --- supervisor/api/host.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/supervisor/api/host.py b/supervisor/api/host.py index b3ff517a750..7a6488f74ac 100644 --- a/supervisor/api/host.py +++ b/supervisor/api/host.py @@ -255,9 +255,7 @@ async def advanced_logs_handler( response.content_type = CONTENT_TYPE_TEXT headers_returned = False async for cursor, line in journal_logs_reader(resp, log_formatter): - # When client closes the connection while reading busy logs, we - # sometimes get this exception. It should be safe to ignore it. - with suppress(ClientConnectionResetError): + try: if not headers_returned: if cursor: response.headers["X-First-Cursor"] = cursor @@ -265,6 +263,13 @@ async def advanced_logs_handler( await response.prepare(request) headers_returned = True await response.write(line.encode("utf-8") + b"\n") + except ClientConnectionResetError as err: + # When client closes the connection while reading busy logs, we + # sometimes get this exception. It should be safe to ignore it. + _LOGGER.debug( + "ClientConnectionResetError raised when returning journal logs: %s", + err, + ) except ConnectionResetError as ex: raise APIError( "Connection reset when trying to fetch data from systemd-journald."