mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(autoroute): read the proxy log as UTF-8
_tail() and stream_log() opened the proxy log without an explicit encoding, so the content was decoded with the locale default. The proxy writes its log as UTF-8, and model request/response text routinely contains non-ASCII (CJK prompts, emoji): under a non-UTF-8 locale (e.g. cp936 on Windows, LC_ALL=C on Linux) 'lite autoroute logs' would mojibake or crash with UnicodeDecodeError, and the error tail shown by 'start' on a failed launch would be garbled. Pin encoding='utf-8' with errors='replace' in both places so stray bytes can never take the command down. Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
parent
071cb49d32
commit
04607dd498
2 changed files with 39 additions and 5 deletions
|
|
@ -89,7 +89,9 @@ def launch_proxy(config_path: Path, port: int, log_path: Path) -> "subprocess.Po
|
|||
def _tail(log_path: Path, lines: int = 40) -> str:
|
||||
if not log_path.exists():
|
||||
return "(no log output captured)"
|
||||
return "\n".join(log_path.read_text(errors="replace").splitlines()[-lines:])
|
||||
# The proxy writes its log as UTF-8; pin the encoding (and tolerate stray bytes) so reading
|
||||
# it does not depend on the locale default (e.g. cp936 on Windows, LC_ALL=C on Linux).
|
||||
return "\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-lines:])
|
||||
|
||||
|
||||
def poll_liveliness(base_url: str, log_path: Path, process: "subprocess.Popen[bytes]", timeout: float = 30.0) -> None:
|
||||
|
|
@ -168,7 +170,9 @@ def stream_log(log_path: Path, stop_event: threading.Event) -> None:
|
|||
time.sleep(0.1)
|
||||
if stop_event.is_set() or not log_path.exists():
|
||||
return
|
||||
with open(log_path, "r") as f:
|
||||
# The proxy writes its log as UTF-8; pin the encoding (and tolerate stray bytes) so the
|
||||
# stream does not crash or mojibake under a non-UTF-8 locale (e.g. cp936 on Windows).
|
||||
with open(log_path, "r", encoding="utf-8", errors="replace") as f:
|
||||
while not stop_event.is_set():
|
||||
line = f.readline()
|
||||
if line:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import os
|
||||
import socket
|
||||
from typing import Optional
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
|
@ -22,10 +21,10 @@ from litellm.proxy.client.cli.commands.autoroute.process import (
|
|||
|
||||
|
||||
class FakeProcess:
|
||||
def __init__(self, returncode: Optional[int] = None):
|
||||
def __init__(self, returncode: int | None = None):
|
||||
self.returncode = returncode
|
||||
|
||||
def poll(self) -> Optional[int]:
|
||||
def poll(self) -> int | None:
|
||||
return self.returncode
|
||||
|
||||
|
||||
|
|
@ -149,6 +148,37 @@ class TestPollLiveliness:
|
|||
assert "crash log line" in str(exc_info.value)
|
||||
|
||||
|
||||
class TestLogReading:
|
||||
def test_tail_decodes_utf8_content(self, tmp_path):
|
||||
"""The proxy writes its log as UTF-8; reading it must not depend on the locale
|
||||
default (e.g. cp936 on Windows mojibakes or crashes on these bytes)."""
|
||||
log_path = tmp_path / "proxy.log"
|
||||
log_path.write_bytes("启动 模型 🤗 ready\n".encode())
|
||||
|
||||
assert process_module._tail(log_path) == "启动 模型 🤗 ready"
|
||||
|
||||
def test_stream_log_echoes_utf8_and_survives_stray_bytes(self, tmp_path, monkeypatch):
|
||||
"""`lite autoroute logs` must not crash on UTF-8 model output nor on undecodable
|
||||
bytes in the log."""
|
||||
log_path = tmp_path / "proxy.log"
|
||||
log_path.write_bytes("response 汉 模型 🤗\n".encode() + b"stray \xff\xfe bytes\n")
|
||||
echoed = []
|
||||
stop_event = process_module.threading.Event()
|
||||
|
||||
def fake_echo(text, nl=True):
|
||||
echoed.append(text)
|
||||
if len(echoed) >= 2:
|
||||
stop_event.set()
|
||||
|
||||
monkeypatch.setattr(process_module.click, "echo", fake_echo)
|
||||
monkeypatch.setattr(process_module.time, "sleep", lambda seconds: None)
|
||||
|
||||
process_module.stream_log(log_path, stop_event)
|
||||
|
||||
assert echoed[0] == "response 汉 模型 🤗\n"
|
||||
assert "stray" in echoed[1] # invalid bytes replaced, not raised
|
||||
|
||||
|
||||
class TestMissingProxyRuntimeModules:
|
||||
def test_flags_absent_modules_only(self, monkeypatch):
|
||||
"""A thin litellm[cli] install lacks the proxy runtime; the missing ones must be reported
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue