Skip to content

Commit bcf7baa

Browse files
agnersclaude
andauthored
Flatten log stream error handling (#7340)
The client write errors in advanced_logs_handler() were handled in a try nested inside the log reading loop, with each handler ending the loop. Handle them in the outer try instead to remove a level of nesting. The reader side only raises exceptions that none of the moved handlers catch. ClientConnectionResetError subclasses ConnectionResetError, so its handler has to stay ahead of the one for systemd-journal-gatewayd errors. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8f21f7a commit bcf7baa

1 file changed

Lines changed: 21 additions & 23 deletions

File tree

‎supervisor/api/host.py‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -390,29 +390,21 @@ async def advanced_logs_handler(
390390
async for cursor, line in journal_logs_reader(
391391
resp, log_formatter, no_colors
392392
):
393-
try:
394-
if not headers_returned:
395-
if cursor:
396-
response.headers["X-First-Cursor"] = cursor
397-
response.headers["X-Accel-Buffering"] = "no"
398-
await response.prepare(request)
399-
headers_returned = True
400-
await response.write(line.encode("utf-8") + b"\n")
401-
except ClientConnectionResetError as err:
402-
# When client closes the connection while reading busy logs, we
403-
# sometimes get this exception. It should be safe to ignore it.
404-
_LOGGER.debug(
405-
"ClientConnectionResetError raised when returning journal logs: %s",
406-
err,
407-
)
408-
break
409-
except ConnectionError as err:
410-
_LOGGER.warning(
411-
"%s raised when returning journal logs: %s",
412-
type(err).__name__,
413-
err,
414-
)
415-
break
393+
if not headers_returned:
394+
if cursor:
395+
response.headers["X-First-Cursor"] = cursor
396+
response.headers["X-Accel-Buffering"] = "no"
397+
await response.prepare(request)
398+
headers_returned = True
399+
await response.write(line.encode("utf-8") + b"\n")
400+
except ClientConnectionResetError as err:
401+
# When client closes the connection while reading busy logs, we
402+
# sometimes get this exception. It should be safe to ignore it.
403+
# Must precede the ConnectionResetError handler, it's a subclass.
404+
_LOGGER.debug(
405+
"ClientConnectionResetError raised when returning journal logs: %s",
406+
err,
407+
)
416408
except (ConnectionResetError, ClientPayloadError) as ex:
417409
# If the stream to the client already started, an error response
418410
# can no longer be sent, so just end the stream. This happens
@@ -427,6 +419,12 @@ async def advanced_logs_handler(
427419
type(ex).__name__,
428420
ex,
429421
)
422+
except ConnectionError as err:
423+
_LOGGER.warning(
424+
"%s raised when returning journal logs: %s",
425+
type(err).__name__,
426+
err,
427+
)
430428
return response
431429

432430
@api_process_raw(CONTENT_TYPE_TEXT, error_type=CONTENT_TYPE_TEXT)

0 commit comments

Comments
 (0)