Repository navigation
Studio: do not fail a cached dataset load on a bookkeeping write - #8057
danielhanchen merged 8 commits into
Conversation
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).
ff30f08 to
ebfd375
Compare
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.
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.
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.
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.
|
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.
|
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
|
load_cached_hf_datasetwrites the advisorycompleteflag for the Studio processed-dataset cache right after the dataset has already loaded, uncaught:That write raises in ordinary situations (cache entry or cache root purged during the load, read-only studio home, full disk).
FileNotFoundError/PermissionErrorthen 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 readscompleteback to gate a load.Change
dataset_cache.py: the completion write is best effort.OSError/RuntimeError/ValueErrorare logged as a warning and the loaded dataset is returned. This matches how the rest ofdataset_processed_cache.pyalready treats this cache.dataset_processed_cache.py: newUnsafeDatasetCachePathError(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 beforeresolve(strict=True), so a dangling swapped-in symlink is reported as unsafe instead of as a benignFileNotFoundError. Existingexcept OSErrorcallers are unaffected (subclass).Verification
Real
datasets.load_dataseton a real parquet snapshot, entry directory made read-only after the load: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 raisesUnsafeDatasetCachePathError.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.