Skip to content

Studio: do not fail a cached dataset load on a bookkeeping write - #8057

Merged
danielhanchen merged 8 commits into
unslothai:mainfrom
BardiaKoopah:fix/studio-dataset-cache-bookkeeping-main
Sep 29, 2026
Merged

danielhanchen merged 8 commits into
unslothai:mainfrom
BardiaKoopah:fix/studio-dataset-cache-bookkeeping-main

Conversation

@BardiaKoopah

@BardiaKoopah BardiaKoopah commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

load_cached_hf_dataset writes the advisory complete flag for the Studio processed-dataset cache right after the dataset has already loaded, uncaught:

dataset = load_dataset(**kwargs)
if app_cache is not None:
    mark_app_processed_dataset_cache_complete(app_cache)
return dataset

That write raises in ordinary situations (cache entry or cache root purged during the load, read-only studio home, full disk). FileNotFoundError / PermissionError then classify as cache-artifact errors, so a normal cached start throws away a good cached dataset and re-downloads it from the Hub, and an offline start or an exact-resource resume fails the run. Nothing reads complete back to gate a load.

Change

  • dataset_cache.py: the completion write is best effort. OSError / RuntimeError / ValueError are logged as a warning and the loaded dataset is returned. This matches how the rest of dataset_processed_cache.py already treats this cache.
  • dataset_processed_cache.py: new UnsafeDatasetCachePathError(OSError) for a symlinked entry / data dir or an entry that resolves outside the cache root. It stays fatal. The leaf symlink check now runs before resolve(strict=True), so a dangling swapped-in symlink is reported as unsafe instead of as a benign FileNotFoundError. Existing except OSError callers are unaffected (subclass).

Verification

Real datasets.load_dataset on a real parquet snapshot, entry directory made read-only after the load:

main: RAISED PermissionError [Errno 13] Permission denied: .../abc123/.metadata.json.tmp-b2eb7dca
PR:   [warning] Could not record processed dataset cache completion for Org/Data
      rows 100

New tests in tests/test_dataset_cache_paths.py: entry purged and root purged during the load (both fail on main, pass here), and a dangling symlinked entry still raises UnsafeDatasetCachePathError. test_dataset_cache_paths, test_training_cached_start, test_training_preflight, test_audio_type_inconclusive, hub/tests/test_dataset_services, test_refactor_guard: 463 passed.

load_cached_hf_dataset calls mark_app_processed_dataset_cache_complete right
after the dataset has already loaded, uncaught. That call raises in ordinary
situations:

  cache purged mid-load (the user deletes the dataset cache) -> FileNotFoundError
  studio cache root gone                                     -> OSError
  read-only volume or full disk                              -> PermissionError

Probed against a real cached snapshot in an isolated studio home: with the entry
directory read-only the load raises PermissionError, and the identical load with
it writable returns 51760 rows. The data was fine; only the metadata write failed.

The flag it records is advisory. Nothing outside dataset_processed_cache.py reads
`complete` back, and no load, inventory or deletion path gates on it. The blast
radius is not advisory though: FileNotFoundError and PermissionError both classify
as cache-artifact errors, so a normal start discards a good cached dataset and
re-downloads the whole thing from the Hub, while an offline start or a resume that
requires exact resources has no fallback and fails the run outright.

Make it best effort and log, matching how the rest of the module already treats
this cache -- _read_cache_entry, iter_app_processed_dataset_caches and
delete_app_processed_dataset_caches all swallow (OSError, RuntimeError, ValueError).

The new test fails with PermissionError without this change. tests/
test_dataset_cache_paths.py is 87 passed with it, alongside test_training_cached_start
(162), test_gguf_load_cache_reuse (40) and hub/tests/test_dataset_services (31).
@BardiaKoopah
BardiaKoopah force-pushed the fix/studio-dataset-cache-bookkeeping-main branch from ff30f08 to ebfd375 Compare August 7, 2026 00:22
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Codex flagged that the new best-effort handler around
mark_app_processed_dataset_cache_complete swallowed the function's path
safety checks alongside its metadata write. Marking is the only
validation that runs after load_dataset, so it is what catches the cache
entry being swapped for a symlink, or resolving outside the trusted cache
root, in the window between prepare_app_processed_dataset_cache and the
completion write. Swallowing that handed back a dataset read from an
untrusted location.

Split the two concerns. mark_app_processed_dataset_cache_complete now
raises UnsafeDatasetCachePathError for the cases it can positively
identify as untrusted -- a root that is present but does not resolve
inside the trusted root, an entry escaping the root, and a symlinked
entry or cache dir -- and a plain FileNotFoundError for a root that is
simply gone. load_cached_hf_dataset re-raises the unsafe error and keeps
swallowing only the failures that mean "the bookkeeping write did not
land": purged entry, read-only studio home, full disk.

UnsafeDatasetCachePathError subclasses OSError, so existing callers that
catch OSError are unaffected. Security posture is unchanged from before
this PR for every unsafe case; only the benign write failures are now
non-fatal, which is the bug this PR set out to fix.

Adds two tests: a real symlink swapped in after prepare is reported as
unsafe, and an unsafe path still fails the cached load.
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Codex P2 on 84e2870. The symlink check sat after entry.path.resolve(strict=True),
so a symlink whose target is removed between the load and the completion marking
raised FileNotFoundError from the resolve, never reaching the check. The caller
reads that as "the entry was purged" and continues, which hides the swap the check
exists to catch.

Reproduced against a real cached snapshot in an isolated studio home: with
entry.path replaced by a dangling symlink, marking raised FileNotFoundError
(swallowed as benign) instead of UnsafeDatasetCachePathError.

Hoist the symlink classification above the strict resolve. Nothing else moves, and
the escape check still runs on the resolved path.

The new test fails with FileNotFoundError without this change. Suites at baseline:
test_dataset_cache_paths 90, test_training_cached_start 162,
hub/tests/test_dataset_services 31.
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Codex P2 on bbc3741. The guard tested is_symlink() on entry.path and
entry.cache_dir only. When a hashed ancestor is swapped instead -- the repo-key or
hub-key directory -- is_symlink() on the leaf reports False, so a dangling ancestor
fell through to the strict resolve, raised FileNotFoundError, and the best-effort
caller read it as a benign purge. A swapped parent redirects the entry just as
effectively as a swapped leaf.

Reproduced against a real cached snapshot in an isolated studio home:

  dangling symlink at leaf        -> UnsafeDatasetCachePathError (already caught)
  dangling symlink at parent      -> FileNotFoundError (swallowed)
  dangling symlink at grandparent -> FileNotFoundError (swallowed)

_symlinked_component walks every component from the trusted root down to the target
and returns the first symlink, so all three now raise UnsafeDatasetCachePathError.
It returns None when the path is not lexically under the root, leaving that to the
existing escape check.

The new test is parametrized over leaf / parent / grandparent; the parent and
grandparent cases fail without this change. Suites at baseline:
test_dataset_cache_paths 93, test_training_cached_start 162,
hub/tests/test_dataset_services 31.
chatgpt-codex-connector[bot]

This comment was marked as outdated.

Codex P2 on c20a711, the same class one level higher. _cache_root_is_present
tested only the snapshot-loads leaf. A dangling symlink at an ancestor inside
cache_root() -- hf-datasets, say -- leaves is_symlink() False on the leaf and
exists() False because it follows the broken parent, so a redirected root read as
a plain missing directory and the caller swallowed it as a benign purge.

Reproduced against a real cached snapshot in an isolated studio home, before:

  hf-datasets is_symlink   : True
  snapshot-loads is_symlink: False   (the leaf itself is not a link)
  snapshot-loads exists    : False   (follows the dangling parent)
  -> FileNotFoundError, swallowed

_cache_root_is_present now walks the components from cache_root() down to
snapshot-loads with the same helper used below the root, so any link in the chain
counts as present-but-unresolvable.

That closes the whole chain. Verified end to end, dangling symlink at:

  entry / repo_dir / snapshot-loads / hf-datasets -> UnsafeDatasetCachePathError
  genuine purge, no symlink anywhere              -> FileNotFoundError (benign)

The benign case matters as much as the unsafe ones: tightening this must not turn
an ordinary cache purge into a hard failure, so it has its own test asserting the
error is a plain FileNotFoundError and not the unsafe subclass.

Suites at baseline: test_dataset_cache_paths 96, test_training_cached_start 162,
hub/tests/test_dataset_services 31.
chatgpt-codex-connector[bot]

This comment was marked as outdated.

@Imagineer99

Copy link
Copy Markdown
Member

Valid bug and good direction, but I wouldn’t merge yet the latest symlink-root P2 is still open, and the fix has become pretty complex for best-effort bookkeeping. I’d tighten the exception handling and simplify the validation/write split.

…king

Codex P2 on 2b2b177. The descendant walk started BELOW configured_root, so a
dangling symlink at the configured cache directory itself left every walked child
reporting is_symlink() False and exists() False, and the redirected root was again
read as a plain purge.

Classify configured_root before walking its descendants. That sets the trust
boundary explicitly: the configured cache root and everything under it is ours to
classify, and anything ABOVE it is the user's own filesystem layout, deliberately
out of scope.

This path only runs once resolution has already failed, so a cache directory
symlinked to another volume -- a legitimate and common setup -- resolves normally
and never reaches the classification. That is now pinned by its own test, because
the risk in tightening this repeatedly is breaking someone who moved their cache
off the system disk:

  dangling symlink at cache_root -> UnsafeDatasetCachePathError
  LIVE symlink at cache_root     -> marks completion normally

Suites at baseline: test_dataset_cache_paths 98, test_training_cached_start 162,
hub/tests/test_dataset_services 31.
…ecks

Drops the ancestor/root symlink classification layer per review. Unsafe
leaf symlinks and root escapes still raise; purges, read-only homes and
full disks now only log.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from BardiaKoopah Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Review record for #8057, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#188.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror a26657dba (this PR's 771e657b5)

chatgpt-codex-connector[bot] on studio/backend/hub/utils/dataset_cache.py (on the mirror)

@@ -559,7 +563,17 @@ def load_cached_hf_dataset(
         )
     dataset = load_dataset(**kwargs)
     if app_cache is not None:
-        mark_app_processed_dataset_cache_complete(app_cache)
+        # Advisory flag: a purged entry, read-only home or full disk must not discard a loaded dataset.
+        try:
+            mark_app_processed_dataset_cache_complete(app_cache)
+        except UnsafeDatasetCachePathError:
+            raise
+        except (OSError, RuntimeError, ValueError):

P2 Badge Keep a symlinked processed-cache root fatal

If snapshot-loads is replaced with a symlink after prepare_app_processed_dataset_cache—for example, during load_dataset—_resolved_app_processed_dataset_cache_root(create=False) rejects it and mark_app_processed_dataset_cache_complete raises a plain OSError. This handler now swallows that error and returns a dataset whose cache path may have been redirected through the symlink; the new UnsafeDatasetCachePathError only covers the entry and data leaves. Classify an invalid/symlinked cache root as unsafe before applying this best-effort fallback.

Useful? React with 👍 / 👎.

Rejected on the mirror

Reply from danielhanchen:

Not changing this. Swapping snapshot-loads for a symlink mid-load needs write access to the user's private Studio home, which is outside the threat model for best-effort bookkeeping; an ordinary purge of that root is exactly the case this handler must tolerate. The entry and data leaves stay fatal, and the security review on this commit found no issues. The wider ancestor/root classification was removed on purpose to keep this change small.

Round 2: reviewed mirror a26657dba7 (this PR's 771e657b5)

chatgpt-codex-connector[bot] (on the mirror)

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: a26657dba7

View security finding report

Only the user who started this review can view the report in Codex.

No triage reaction was recorded on the mirror

chatgpt-codex-connector[bot] (on the mirror)

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

Reviewed commit: a26657dba7

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror a26657dba7 (this PR's 771e657b5).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen
danielhanchen merged commit 7617fc3 into unslothai:main Sep 29, 2026
43 of 46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants