Skip to content

Studio: training survives a non-writable HF datasets cache - #6148

Merged
danielhanchen merged 4 commits into
mainfrom
studio-datasets-cache-fallback
Jun 10, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-datasets-cache-fallback

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Jun 10, 2026 •

Copy link
Copy Markdown
Member

What this does

Fixes training runs dying with:

[Errno 13] Permission denied: '~/.cache/huggingface/datasets/mlabonne___fine_tome-100k/default/0.0.0/<hash>_builder.lock'

Root cause

datasets takes a file lock next to the cached builder before reusing it. When the shared HF datasets cache contains a subtree owned by another user, most commonly because an earlier job ran as root and populated datasets/{name}/{config}/{version}/ with root ownership, the Studio worker (running as the login user) cannot create the lock file inside that directory and load_dataset raises EACCES. Studio honors a pre-set HF_DATASETS_CACHE/HF_HOME, so any environment pointing at a shared cache is exposed. The run then fails even though the dataset is fully cached and readable.

Fix

load_dataset calls in the training worker and trainer now go through load_dataset_cache_safe (utils/datasets/cache_safe.py): on PermissionError it logs a warning and retries with cache_dir pointed at a Studio-owned cache under cache_root()/hf-datasets, also moving HF_DATASETS_CACHE so nested builders follow. Worst case is one rebuild of the dataset in the fallback location; the foreign-owned cache is left untouched. The wrapper is aliased at the existing import sites, so all eleven call sites across worker.py and trainer.py are covered without touching call-site code.

The non-streaming dataset preview loads (routes/datasets.py, hub/services/datasets/local.py, hub/services/datasets/formatting.py) go through the same wrapper, since previewing a cached or local dataset takes the same builder lock and would fail the same way. Streaming previews never call download_and_prepare, so they are unaffected and keep importing datasets.load_dataset directly.

Testing

Reproduced on a machine exhibiting the exact failure (root-owned mlabonne___fine_tome-100k/default/0.0.0/, worker running as ubuntu):

  • Plain load_dataset("mlabonne/FineTome-100k", split="train") raises the reported PermissionError on the builder lock.
  • load_dataset_cache_safe with identical arguments logs the fallback warning, rebuilds the dataset under cache_root()/hf-datasets, and returns all 100000 rows.
  • py_compile passes on the three touched files; the worker spawns as a fresh subprocess per run, so the fix applies to the next training run without restarting the server.

danielhanchen and others added 2 commits June 10, 2026 13:00
A shared HF datasets cache can contain subtrees owned by another user
(for example populated by an earlier root-run job). datasets then dies
with "[Errno 13] Permission denied: ..._builder.lock" while locking
the cached builder and the training run fails. load_dataset in the
training worker and trainer now goes through a wrapper that catches the
EACCES and rebuilds the dataset in a Studio-owned cache under
cache_root()/hf-datasets, logging the fallback.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a permission-safe wrapper, load_dataset_cache_safe, around datasets.load_dataset to handle PermissionError issues when accessing shared Hugging Face dataset caches. It falls back to a studio-owned cache directory when a permission error is encountered and is integrated into both trainer.py and worker.py. Feedback suggests temporarily setting and restoring the HF_DATASETS_CACHE environment variable using a try...finally block to prevent permanently bypassing the shared cache for subsequent dataset loads in the same process.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

I am having trouble creating individual review comments. Click here to see my feedback.

studio/backend/utils/datasets/cache_safe.py (41-44)

high

Permanently mutating os.environ["HF_DATASETS_CACHE"] will cause all subsequent dataset loads in the same process to bypass the shared cache, even if those other datasets are perfectly readable or writable.

To prevent unnecessary redownloads or rebuilds of other datasets, temporarily set the environment variable and restore it using a try...finally block.

        # Nested builders consult the env var, so move it alongside cache_dir.
        # Temporarily set the env var to avoid permanently bypassing the shared cache for other datasets.
        old_env = os.environ.get("HF_DATASETS_CACHE")
        os.environ["HF_DATASETS_CACHE"] = fallback
        kwargs["cache_dir"] = fallback
        try:
            return load_dataset(*args, **kwargs)
        finally:
            if old_env is not None:
                os.environ["HF_DATASETS_CACHE"] = old_env
            else:
                os.environ.pop("HF_DATASETS_CACHE", None)

@danielhanchen

Copy link
Copy Markdown
Member Author

Fair point on the env var: it now gets set only around the fallback load in a try finally and restored after, so later datasets in the same process still try the shared cache first and fall back per call. Re-ran the repro: the poisoned FineTome cache still falls back and loads all 100000 rows, and the env var is back to the shared path right after the call returns.

@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: 3b4ceb57d4

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

# Nested builders consult the env var while the load runs; restore it
# after so other datasets keep trying the shared cache first.
old_env = os.environ.get("HF_DATASETS_CACHE")
os.environ["HF_DATASETS_CACHE"] = fallback

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Guard the HF_DATASETS_CACHE override with a lock

In the API/server process, sync FastAPI handlers such as check_format can run concurrently in the threadpool and now call this wrapper for previews. This line changes a process-wide environment variable while one fallback load is running, so another concurrent dataset load can silently use the fallback cache instead of the configured cache, and overlapping fallback calls can restore HF_DATASETS_CACHE out of order. Please serialize the env override or avoid mutating global process state for the retry.

Useful? React with 👍 / 👎.

@danielhanchen
danielhanchen merged commit 6e057ff into main Jun 10, 2026
26 of 32 checks passed
@danielhanchen
danielhanchen deleted the studio-datasets-cache-fallback branch June 10, 2026 15:22
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