Fix UnicodeEncodeError in web banner when stdout is non-UTF-8 (Windows) - #2560
Open
LHMQ878 wants to merge 1 commit into
Open
Fix UnicodeEncodeError in web banner when stdout is non-UTF-8 (Windows)#2560LHMQ878 wants to merge 1 commit into
LHMQ878 wants to merge 1 commit into
Conversation
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
This was referenced Jul 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2532
Problem
kimi webcrashes on Windows (Chinese locale, codepage 936/GBK) when stdout is redirected, before the HTTP server binds its port:Repro:
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, soprint()raisesUnicodeEncodeErrorand the process dies before the server starts.chcp 65001,PYTHONUTF8=1andPYTHONIOENCODING=utf-8do not help when stdout is redirected.Fix
Route the banner output through a small
_safe_printhelper insrc/kimi_cli/utils/server.py. It prints normally and, only onUnicodeEncodeError, re-encodes the line with the stream's own encoding usingerrors="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
➜renders as?; banner prints in full.➜prints as before.ruff checkandruff format --checkpass on the changed file.