Skip to content

Stop a localized Windows console from losing a child's whole output stream - #10192

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
Lyxot:fix/subprocess-decode-utf8-mode
Sep 2, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
Lyxot:fix/subprocess-decode-utf8-mode

Conversation

@Lyxot

@Lyxot Lyxot commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Fixes #10173.

What happens today

On a non-English Windows the llama.cpp prebuilt update fails with installer exited 2 before 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=1 in setup.ps1 and -X utf8 on the CLI's respawn. In that mode text = True decodes a child's pipes as UTF-8 no matter what the system ANSI code page is. A localized nvidia-smi writes 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 a UnicodeDecodeError raised there is swallowed, leaving stdout as None instead of propagating. POSIX raises, so this reproduces only on Windows.

The driver probe then reads None, driver_cuda_version becomes None, and the installer refuses the wheel it was about to fetch and exits 2.

The same shape reaches further than the installer. _pid_alive is the sharpest case: a localized tasklist prints its no-match notice in the code page, stdout comes back None, and "PID" in None raises TypeError past an except that only expects OSError, ValueError and SubprocessError, so unsloth studio stop fails rather than reporting the process gone.

What changed

Every text-mode subprocess capture under studio/ and unsloth_cli/ now passes encoding = "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_profile reads a path out of cmd.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, wslpath would translate it into a directory that does not exist, and Codex's state would silently go missing. That call keeps errors = "strict" and a UnicodeDecodeError now fails with a message naming the USERPROFILE override 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 65001 or setting PowerShell's OutputEncoding. 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 _communicate path grafted in so the stdout = None behaviour appears on a POSIX host. Regression tests drive the real subprocess against fake children that write GBK, rather than asserting on keyword arguments, and each one fails when its production change is reverted:

tests/studio/install/test_install_llama_prebuilt_logic.py   run_capture keeps the ASCII CUDA line; detect_host reads the driver version
tests/studio/install/test_cuda_repair.py                    the python-stack CUDA-tag probe reads cu130 from a localized banner
tests/studio/test_cli_studio_stop_windows.py                _pid_alive reads a localized tasklist notice; a profile that does not decode fails loudly

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.

495 passed, 1 skipped

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

Lyxot commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-02T12:03:44.849874Z b8fc260 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: b8fc26083e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen merged commit 87ab4ed into unslothai:main Sep 2, 2026
8 of 86 checks passed
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.

[Bug] llama.cpp prebuilt update fails on non-English Windows: nvidia-smi output decoded as UTF-8 → driver_cuda_version=None → "installer exited 2"

2 participants