Suppress ClientConnectionResetError when returning logs (#5358)

When client requests (or more often follows) some busy logs and closes
the connection while the StreamWriter tries to write into it, an
exception is raised. This should be harmless, and unless there's another
way to handle this gracefully (I'm not aware of), then it should be safe
to ignore the exception in this context.
This commit is contained in:
Jan Čermák 2024-10-21 10:11:22 +02:00 committed by GitHub
parent fcac17f335
commit 6f196c9dea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,7 +4,7 @@ import asyncio
from contextlib import suppress from contextlib import suppress
import logging import logging
from aiohttp import web from aiohttp import ClientConnectionResetError, web
from aiohttp.hdrs import ACCEPT, RANGE from aiohttp.hdrs import ACCEPT, RANGE
import voluptuous as vol import voluptuous as vol
from voluptuous.error import CoerceInvalid from voluptuous.error import CoerceInvalid
@ -260,7 +260,10 @@ class APIHost(CoreSysAttributes):
response.headers["X-First-Cursor"] = cursor response.headers["X-First-Cursor"] = cursor
await response.prepare(request) await response.prepare(request)
headers_returned = True headers_returned = True
await response.write(line.encode("utf-8") + b"\n") # When client closes the connection while reading busy logs, we
# sometimes get this exception. It should be safe to ignore it.
with suppress(ClientConnectionResetError):
await response.write(line.encode("utf-8") + b"\n")
except ConnectionResetError as ex: except ConnectionResetError as ex:
raise APIError( raise APIError(
"Connection reset when trying to fetch data from systemd-journald." "Connection reset when trying to fetch data from systemd-journald."