Repository navigation
Stop a localized Windows console from losing a child's whole output stream - #10192
Merged
danielhanchen merged 2 commits intoSep 2, 2026
Merged
Conversation
Studio launches every Windows command with `-X utf8`, so `subprocess.run(..., text = True)` decodes child pipes as UTF-8 regardless of the system ANSI code page. A localized `nvidia-smi` writes that code page instead. On Windows CPython reads pipes on helper threads, so the `UnicodeDecodeError` kills the thread and `communicate()` returns `stdout = None` without raising; on POSIX the same bytes raise. Either way the parse is lost: `detect_host` records no driver CUDA version, no Windows CUDA asset is selectable, and the llama.cpp prebuilt update exits 2 and falls back to a source build that no-ops. whisper.cpp inherits the same host detection. Every text-mode subprocess capture under `studio/` now passes `encoding = "utf-8", errors = "replace"`, the form the backend already uses at all of its sites. Under the forced-UTF-8 launch path the explicit encoding changes nothing; run directly, it removes the dependence on the ambient code page. Every consumer parses ASCII tokens, so a replaced character never reaches a parser. `detect_host` also logs why the bare `nvidia-smi` probe failed instead of swallowing it, which is what made this take a user reading source to find. Fixes unslothai#10173
Same defect as the installers: the CLI runs under `-X utf8`, and a child that writes the system ANSI code page loses its whole stream to a strict decode. `_pid_alive` on Windows is the sharpest case: `tasklist` prints its no-match notice localized, `stdout` comes back `None`, and `in None` raises `TypeError` past the `except` that only expects `OSError`, `ValueError` and `SubprocessError`. Its already-fixed twin in `studio/backend/run.py` claims to mirror this one. Every text-mode subprocess capture under `unsloth_cli/` now passes `encoding = "utf-8", errors = "replace"`, with one deliberate exception. `_wsl_windows_user_profile` reads a path out of `cmd.exe /c echo %USERPROFILE%`, which writes the Windows code page, and the path is the value: replacement would turn a localized profile into a corrupted home that `wslpath` translates into a nonexistent directory and Codex's state silently goes missing. That call decodes strictly and a `UnicodeDecodeError` fails with a message naming the `USERPROFILE` override the function already honours.
Lyxot
requested review from
Etherll,
Imagineer99,
LeoBorcherding,
NilayYadav,
danielhanchen and
oobabooga
as code owners
September 2, 2026 11:53
Collaborator
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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 #10173.
What happens today
On a non-English Windows the llama.cpp prebuilt update fails with
installer exited 2before it downloads anything. The installer reports no CUDA driver on a machine that has one, and there is nothing in the log that points at the cause.Root cause
Studio starts its children under UTF-8 mode, through
PYTHONUTF8=1insetup.ps1and-X utf8on the CLI's respawn. In that modetext = Truedecodes a child's pipes as UTF-8 no matter what the system ANSI code page is. A localizednvidia-smiwrites that code page, so its banner is not valid UTF-8.What makes this silent is the Windows path in CPython's
subprocess. Output is read on a helper thread, and aUnicodeDecodeErrorraised there is swallowed, leavingstdoutasNoneinstead of propagating. POSIX raises, so this reproduces only on Windows.The driver probe then reads
None,driver_cuda_versionbecomesNone, and the installer refuses the wheel it was about to fetch and exits 2.The same shape reaches further than the installer.
_pid_aliveis the sharpest case: a localizedtasklistprints its no-match notice in the code page,stdoutcomes backNone, and"PID" in NoneraisesTypeErrorpast anexceptthat only expectsOSError,ValueErrorandSubprocessError, sounsloth studio stopfails rather than reporting the process gone.What changed
Every text-mode subprocess capture under
studio/andunsloth_cli/now passesencoding = "utf-8", errors = "replace". Those two trees are exactly what runs under UTF-8 mode. This is the idiom the backend already uses at more than fifty call sites; the installers and the CLI had been missed. Undecodable bytes become U+FFFD and the surrounding ASCII survives, which is all these callers parse: a CUDA version, a PID, a package name.One call decodes strictly on purpose.
_wsl_windows_user_profilereads a path out ofcmd.exe /c echo %USERPROFILE%, and there the decoded text is the value rather than a field inside it. Replacement would turn a localized profile into a corrupted path,wslpathwould translate it into a directory that does not exist, and Codex's state would silently go missing. That call keepserrors = "strict"and aUnicodeDecodeErrornow fails with a message naming theUSERPROFILEoverride the function already honours.Risks and tradeoffs
errors = "replace"cannot fail, so a caller that needs the exact bytes would be given mojibake instead of an error. Every site changed here parses ASCII out of the stream, and the one site whose value is the whole string was deliberately left strict, which is the distinction to check when reviewing.Making a localized profile work, rather than fail clearly, would mean forcing the console to UTF-8 with
chcp 65001or setting PowerShell'sOutputEncoding. That is a larger change and is left out of this one.Validation
Reproduced first, with a real child writing GBK bytes in place of
nvidia-smi, including the Windows_communicatepath grafted in so thestdout = Nonebehaviour appears on a POSIX host. Regression tests drive the realsubprocessagainst fake children that write GBK, rather than asserting on keyword arguments, and each one fails when its production change is reverted:The tests set the child's decoding explicitly rather than inheriting the host's, so they behave the same on a runner in any locale.