Skip to content

Studio: clear only the compiled cache unsloth_zoo writes to - #15628

Merged
danielhanchen merged 7 commits into
mainfrom
studio-compiled-cache-location
Oct 28, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
studio-compiled-cache-location

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

clear_unsloth_compiled_cache and the Settings cache list decide which compiled-cache folders exist through _configured_cache_dirs() (studio/backend/utils/cache_cleanup.py). It always added <cwd>/unsloth_compiled_cache next to UNSLOTH_COMPILE_LOCATION. Studio always sets that variable, and unsloth_zoo then never writes to the CWD, so a folder of that name in the launch directory is somebody else's or a stale leftover. Every clear and every Settings walk warned about it, and any unsloth_compiled_module_*.py files in it were deleted.

What changes

_configured_cache_dirs() now returns only the folder unsloth_zoo.compiler writes to (_get_compile_folder):

  • UNSLOTH_COMPILE_LOCATION when set (a relative value taken against the CWD, as os.makedirs takes it), otherwise <cwd>/unsloth_compiled_cache;
  • <tempdir>/<same leaf> only when that location cannot be created, decided without creating anything.

Ownership checks and preserve_patterns are unchanged, and no caller changed.

With the variable set, a leftover <cwd>/unsloth_compiled_cache from an older install is no longer cleared or put on sys.path; it simply stays on disk, since the cleanup no longer deletes outside the folder the compiler uses. Studio still expands ~ in the variable while unsloth_zoo does not; that difference predates this change and is left alone.

Tests

  • New studio/backend/tests/test_compiled_cache_location.py (7, temporary directories only): with the variable set, a CWD folder of the same name is neither listed nor cleared and draws no warning; without it the CWD folder is the cache; a relative value is taken against the CWD; an uncreatable location falls back to the temp dir like the compiler, and a creatable one never does. 4 of them fail on main.
  • test_sandbox_files_and_storage_roots.py: test_cache_cleanup_finds_the_configured_and_cwd_caches asserted the old behaviour; it is now test_cache_cleanup_finds_the_configured_cache_and_not_the_cwd_one.
  • Both files: 328 passed on this branch; 5 failed on main (the 4 new tests and the renamed one).

Update after review

  • Cleanup no longer looks at <tempdir>/<leaf>, the folder the compiler falls back to when the configured location cannot be used. That name is shared by every user and every install on the machine, so a folder or symlink found there is not provably ours: clearing it could follow a link into another marked cache, and registering it would put a foreign folder on PYTHONPATH. main never touched that folder either, so this is no change from main. The bullet about the temp fallback and the two fallback tests are replaced by tests that a marked temp folder, a symlink there, and a . leaf are all left alone and never registered.
  • A relative UNSLOTH_COMPILE_LOCATION is now anchored at the working directory, so the Settings purge gate sees the absolute folder the compiler writes to.
  • Correction to the compatibility paragraph: ~ is no longer expanded. The value is taken verbatim, as os.makedirs in the compiler takes it, so ~/x means <cwd>/~/x in both places.

@danielhanchen
danielhanchen marked this pull request as ready for review October 28, 2026 05:52
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-10-28T07:55:12.054915Z c2b19ff 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: ccb03f741e

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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: ccb03f741e

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

<tempdir>/<leaf> is a name every user and install on the machine shares, so a
directory or symlink found there is not ours to clear or put on PYTHONPATH.
Anchor a relative UNSLOTH_COMPILE_LOCATION at the CWD so the Settings purge
gate sees an absolute path.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: c2b19ffcf5

ℹ️ 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 f0cb2a8 into main Oct 28, 2026
2 of 37 checks passed
@danielhanchen
danielhanchen deleted the studio-compiled-cache-location branch October 28, 2026 08:21
danielhanchen added a commit that referenced this pull request Oct 28, 2026
`(Python 3.13, rest)` on #15810 failed `test_legacy_sandbox_is_migrated`
with a log line that gives the cause away:

```
Moved 1 chat sandbox folder(s) from .../test_legacy_sandbox_is_migrate0/userprofile/studio_sandbox
  to .../test_sandbox_lives_under_the_s0/custom_studio_home/sandbox
```

The legacy folder of one test was moved into the sandbox root of the
test that ran before it on the same xdist worker.

## Cause

Since #15628, `get_sandbox_workdir` starts a background
`sandbox-migrate` thread. That thread captures `sandbox_root()` when it
starts, but reads the legacy root (`HOME/studio_sandbox`) only once it
gets scheduled. `test_sandbox_lives_under_the_studio_home` never waits
for its mover. On a busy worker the mover was still pending when
`test_legacy_sandbox_is_migrated` switched `HOME` and reset
`_legacy_sandbox_migrated`. It then moved the new test's folder into the
old root. The new test's own `get_sandbox_workdir` found that mover
still alive and reused it, so nothing ever moved the folder to where the
test looks.

The product is fine: in a real process `HOME` never changes under the
mover. This is test isolation.

There is a second hazard with the same root. The conftest does not
isolate `HOME`, so a mover that outlives a test's `monkeypatch` reads
the real `~/studio_sandbox`.

## Fix

A conftest autouse fixture joins any live `sandbox-migrate` thread
before and after every backend test. It raises if a mover does not
finish in 30s instead of letting it leak. The fixture requests
`monkeypatch`, so the drain runs before the test's `HOME` is restored
and a late mover can never read the real home directory.

## Verification

- Repro: a tiny plugin delays the mover by 1s, the way a loaded worker
deschedules it. Running `test_sandbox_lives_under_the_studio_home` and
then `test_legacy_sandbox_is_migrated`:
- main `3db711acb5`: `1 failed, 1 passed`, with the same misplaced move
as CI
  - this branch: `2 passed`
- `test_sandbox_files_and_storage_roots.py` on its own: 321 passed.
- The 10 backend test files that start a mover, under `-n 8`: 887
passed, 3 skipped.
- Only `conftest.py` and `test_sandbox_files_and_storage_roots.py` drive
the mover directly, and nothing holds one open across tests, so the
drain cannot stall an unrelated test.
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.

1 participant