Skip to content

fix(studio): detect truncated sidecar files via RECORD in _venv_dir_is_valid (#7715) - #7721

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
Anai-Guo:fix/venv-record-integrity-7715
Aug 2, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
Anai-Guo:fix/venv-record-integrity-7715

Conversation

@Anai-Guo

@Anai-Guo Anai-Guo commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

What

Closes #7715.

_venv_dir_is_valid (studio/backend/utils/transformers_version.py) validated each .venv_t5_* sidecar by directory existence + .dist-info/METADATA version only. A truncated or deleted file inside an otherwise-intact package (disk-full, interrupted pip) passed validation, so the existing wipe-and-reinstall self-heal (_ensure_venv_dir) never fired. The export worker then died on import with an error pointing at transformers rather than at the damaged install.

Change

  • New _dist_record_intact(venv_dir, dist_info) helper compares a distribution's RECORD manifest against the filesystem: a listed file that is missing or shorter than its recorded size fails validation; a file larger than recorded is treated as a packaging collision, not damage. .pyc and .dist-info entries and rows without a recorded size are skipped.
  • _venv_dir_is_valid calls it after the version check succeeds — so the check runs only for pinned packages (the existing loop scope), keeping cost bounded. On a live install this is ~0.04s per sidecar per spawn, small next to spawning a worker and importing transformers.
  • A missing/unreadable RECORD leaves the existing METADATA gate in charge, so installers that omit RECORD are unaffected.

Failing validation makes _ensure_venv_dir wipe-and-reinstall automatically, covering users who never run unsloth studio update (the CLI-side scan in #7691 can only report).

Tests

TestVenvDirIsValidRecordIntegrity in test_transformers_version.py covers: intact RECORD passes; a missing file is detected (+WARNING); a truncated file is detected; a file larger than recorded is not flagged; and no-RECORD falls back to the old METADATA behaviour.

🤖 Generated with Claude Code

…s_valid (unslothai#7715)

The .venv_t5_* sidecar validation checked only the package directory and
METADATA version, so a truncated or deleted file inside an otherwise-intact
sidecar (disk-full, interrupted pip) passed validation. The worker then died
on import pointing at transformers rather than at the damaged install.

Extend _venv_dir_is_valid to compare each pinned package's RECORD manifest
against the filesystem: a listed file that is missing or shorter than its
recorded size fails validation, which makes the existing wipe-and-reinstall
self-heal fire. A file larger than recorded is treated as a packaging
collision, not damage; .pyc/.dist-info entries and rows without a recorded
size are skipped; a missing RECORD leaves the METADATA gate in charge.
@Anai-Guo
Anai-Guo requested a review from danielhanchen as a code owner August 1, 2026 07:19

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a020f75510

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

except ValueError:
continue
try:
actual = (root / rel).stat().st_size

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Handle --target RECORD script paths

When a pinned package installed with pip/uv --target declares console scripts, its RECORD entries use paths such as ../../bin/hf even though the script is installed under <target>/bin. Joining that entry directly to venv_dir checks outside the sidecar, so an intact install of a package like the pinned huggingface_hub is reported as missing on every subsequent validation; _ensure_venv_dir then deletes and reinstalls the sidecar each spawn, and offline users fail after the first run. Please skip or remap RECORD entries that escape the target root instead of treating them as package damage.

Useful? React with 👍 / 👎.

--target installs record console scripts (e.g. huggingface_hub's hf) with
escaping paths like ../../bin/hf that resolve outside the sidecar. The RECORD
integrity check joined them to venv_dir and read the miss as damage, wiping and
reinstalling an intact sidecar on every spawn (breaking offline users). Skip
entries with .. path components, alongside the existing .pyc/.dist-info skips.

Addresses the Codex P1 review on unslothai#7721.
@Anai-Guo

Anai-Guo commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the Codex P1 in f514525: --target installs record console scripts with escaping paths like ../../bin/hf (huggingface_hub ships hf/huggingface-cli), which the RECORD check was joining to venv_dir and reading as damage — wiping an intact sidecar on every spawn. Entries with .. path components are now skipped alongside the existing .pyc/.dist-info skips, with a regression test (test_out_of_tree_script_entry_is_skipped).

@Anai-Guo

Anai-Guo commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

Note on the red Backend CI: the 6 failures (tests/test_vision_cache.py::TestLocalGgufVisionDetection::* and tests/test_cached_gguf_routes.py::test_legacy_custom_inventory_filters_registered_mtp_root) are pre-existing on main, not from this PR.

The same 6 fail on the Backend CI run for main at #7723 (all of 3.10–3.13). This branch only touches studio/backend/utils/transformers_version.py and studio/backend/tests/test_transformers_version.py; the new TestVenvDirIsValidRecordIntegrity cases pass.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: f514525d6c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

The RECORD-vs-filesystem integrity check this PR introduced is now on main,
so the branch carries no remaining difference.

unslothai#7725 built on the approach here and kept its core decisions: compare each
RECORD row against the filesystem, treat a file larger than recorded as a
packaging collision rather than damage, and leave a missing or unreadable
RECORD to the existing checks rather than guess. It also took the fix from
the follow-up commit on this branch, skipping out-of-tree script rows such
as ../../bin/hf that a --target install records but never resolves.

It differs in where the scan runs. _venv_dir_is_valid is reached from
_latest_sidecar_intact several times per validate request, so the scan sits
only at the sites that can actually repair, and the routing predicate stays
package level.
@danielhanchen

Copy link
Copy Markdown
Member

Thanks for this, and sorry for the overlap. I have merged current main into the branch so it now carries no remaining diff, rather than closing it, because the approach here is the one that shipped and your commits stay in the history.

#7725 kept the core decisions from this PR:

  • compare each RECORD row against the filesystem, on size and existence
  • treat a file larger than recorded as a packaging collision rather than damage
  • leave a missing or unreadable RECORD to the existing checks instead of guessing

It also took the fix from your follow-up commit here, skipping out-of-tree rows such as ../../bin/hf that a --target install records but that never resolve inside the sidecar. That one matters more than it looks: without it the check fails closed on a healthy pip-installed sidecar and triggers a several-hundred-MB reinstall, and it was the first thing review caught on my side too.

The one place it went differently is where the scan runs. _venv_dir_is_valid is reached from _latest_sidecar_intact, which runs several times per POST /validate, so putting the RECORD walk there puts roughly 25 ms on a request path that only routes and never repairs. #7725 keeps that predicate package level and does the scan only at the three sites that can actually act on the answer.

The rest of #7725 was fallout from wiring it into those repair sites, which is where most of the review went: a worker child cannot repair the pinned latest sidecar, so damage it found had to be handed back to the parent, and getting that wrong deadlocked every retry.

Happy to have you as the reporter and first fix on this one.

@danielhanchen
danielhanchen merged commit 64c4f8d into unslothai:main Aug 2, 2026
2 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.

Damaged .venv_t5_* sidecar files are neither detected nor repaired

3 participants