From 095a7e4d61722501ebdbeb48f3550cb9cc9b5850 Mon Sep 17 00:00:00 2001 From: Eugene Morozov Date: Wed, 8 Jul 2026 16:14:25 +0300 Subject: [PATCH] Fix UnicodeDecodeError on non-UTF-8 log lines Read the access log in binary mode so decoding happens per-line inside the existing contextlib.suppress block. Previously readlines() decoded the whole file as UTF-8 up front and threw on any raw non-UTF-8 byte (e.g. a scanner's malformed URL) before truncate(0) could run, so the bad line was never cleared and every subsequent scrape failed. --- app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 250a300..ff53512 100644 --- a/app.py +++ b/app.py @@ -21,8 +21,8 @@ async def read_and_process_logs() -> dict[tuple[str, str], dict[str, float]]: metrics_data: dict[tuple[str, str], dict[str, float]] = {} if os.path.exists(unit_log_file): - async with aiofiles.open(unit_log_file, mode="r+") as f: - lines: list[str] = await f.readlines() + async with aiofiles.open(unit_log_file, mode="rb+") as f: + lines: list[bytes] = await f.readlines() await f.truncate(0) for line in lines: