From eddb17028d2d5058fb514f51db066f316aa1109f Mon Sep 17 00:00:00 2001 From: allenliang2022 Date: Sun, 31 May 2026 14:32:48 +0800 Subject: [PATCH] fix(updates): prevent startup crash on non-UTF-8 (GBK) locales _run_git used subprocess.run(text=True) without an explicit encoding, so on Chinese Windows (and other non-UTF-8 codepages) git stdout was decoded with the locale codepage. _dirty_suffix() runs `git diff --binary HEAD`, whose binary bytes are not valid GBK, raising UnicodeDecodeError in the subprocess reader thread. That left r.stdout = None, so `r.stdout.strip()` raised AttributeError during module import of api.updates, crashing server.py before it could bind its port. Force UTF-8 decoding with errors=replace and guard against None defensively. --- api/updates.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/api/updates.py b/api/updates.py index 4269e1996..55d0c75a2 100644 --- a/api/updates.py +++ b/api/updates.py @@ -166,9 +166,13 @@ def _run_git(args, cwd, timeout=10): r = subprocess.run( ['git'] + args, cwd=str(cwd), capture_output=True, text=True, timeout=timeout, + encoding='utf-8', errors='replace', ) - stdout = r.stdout.strip() - stderr = r.stderr.strip() + # On non-UTF-8 locales (e.g. Chinese Windows GBK), a binary git + # output that fails to decode used to leave r.stdout = None and crash + # the whole import with AttributeError. Guard against None defensively. + stdout = (r.stdout or '').strip() + stderr = (r.stderr or '').strip() if r.returncode == 0: return stdout, True return stderr or stdout or f"git exited with status {r.returncode}", False