Skip to content

Fix UnicodeEncodeError in web banner when stdout is non-UTF-8 (Windows) - #2560

Open
LHMQ878 wants to merge 1 commit into
MoonshotAI:mainfrom
LHMQ878:fix/banner-encoding-crash
Open

Fix UnicodeEncodeError in web banner when stdout is non-UTF-8 (Windows)#2560
LHMQ878 wants to merge 1 commit into
MoonshotAI:mainfrom
LHMQ878:fix/banner-encoding-crash

Conversation

@LHMQ878

@LHMQ878 LHMQ878 commented Jul 27, 2026

Copy link
Copy Markdown

Fixes #2532

Problem

kimi web crashes on Windows (Chinese locale, codepage 936/GBK) when stdout is redirected, before the HTTP server binds its port:

UnicodeEncodeError: 'gbk' codec can't encode character '➜' in position 4: illegal multibyte sequence
  print_banner:118 -> run_web_server:437 -> web:70

Repro:

kimi.exe web --port 58627 --no-open > web.log 2>&1

The startup banner contains (U+279C). When stdout is redirected to a file or pipe on Windows, Python uses the system locale encoding (GBK/cp936) instead of UTF-8, so print() raises UnicodeEncodeError and the process dies before the server starts. chcp 65001, PYTHONUTF8=1 and PYTHONIOENCODING=utf-8 do not help when stdout is redirected.

Fix

Route the banner output through a small _safe_print helper in src/kimi_cli/utils/server.py. It prints normally and, only on UnicodeEncodeError, re-encodes the line with the stream's own encoding using errors="replace" so a non-encodable glyph degrades to a placeholder instead of killing the process.

UTF-8 terminals are completely unaffected — still prints as-is.

Verification

  • Simulated GBK-redirected stdout: no crash; renders as ?; banner prints in full.
  • UTF-8 stdout: unchanged, prints as before.
  • ruff check and ruff format --check pass on the changed file.

Open in Devin Review

The startup banner contains '➜' (U+279C). When stdout is redirected to a
file or pipe on Windows, Python uses the system locale encoding (e.g.
GBK/cp936) instead of UTF-8, so printing the banner raises
UnicodeEncodeError and kills the process before the web server binds its
port.

Wrap banner output in a _safe_print helper that falls back to the stream's
encoding with errors='replace' on UnicodeEncodeError, so startup continues
regardless of stdout encoding. UTF-8 terminals are unaffected.

Fixes MoonshotAI#2532

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kimi web crashes at startup on Windows when stdout is redirected: UnicodeEncodeError (gbk) in print_banner

1 participant