{"record":{"id":"89738fefe8b12221","repo":"tracel-ai/burn","slug":"failed-to-create-cache-file","errorCode":null,"errorMessage":"Failed to create cache file","messagePattern":"Failed to create cache file","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-train/src/metric/vision/afine/weights.rs","lineNumber":62,"sourceCode":"\n/// Cached filename inside `~/.cache/burn-dataset/afine/`.\nconst CACHE_FILENAME: &str = \"afine.pth\";\n\nfn get_cache_dir() -> PathBuf {\n    let cache_dir = dirs::cache_dir()\n        .expect(\"Could not get cache directory\")\n        .join(\"burn-dataset\")\n        .join(\"afine\");\n    if !cache_dir.exists() {\n        create_dir_all(&cache_dir).expect(\"Failed to create cache directory\");\n    }\n    cache_dir\n}\n\nfn download_if_needed(url: &str, cache_path: &PathBuf, message: &str) {\n    if !cache_path.exists() {\n        let bytes = download_file_as_bytes(url, message);\n        let mut file = File::create(cache_path).expect(\"Failed to create cache file\");\n        file.write_all(&bytes).expect(\"Failed to write weights\");\n    }\n}\n\n/// Download `afine.pth` (if not cached) and load all six shards into\n/// the matching submodules of an `Afine` previously produced by\n/// `AfineConfig::init`.\n///\n/// Errors during loading are logged via `log::warn!` and the function\n/// returns the module unconditionally — `allow_partial(true)` plus\n/// per-shard regex remapping mean unmapped or unknown checkpoint keys\n/// are silently dropped, matching the behaviour of LPIPS, DISTS, and\n/// FID's pretrained loaders.\npub(crate) fn load_pretrained_weights(mut afine: Afine) -> Afine {\n    let cache_dir = get_cache_dir();\n    let cache_path = cache_dir.join(CACHE_FILENAME);\n    download_if_needed(AFINE_URL, &cache_path, \"Downloading A-FINE weights...\");\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-train/src/metric/vision/afine/weights.rs#L44-L80","documentation":"`download_if_needed` in the afine weights module panics with `File::create(cache_path).expect(\"Failed to create cache file\")` when the cached `afine.pth` cannot be created after a download. The directory exists (it was just ensured by `get_cache_dir`), so failure is typically a permissions issue on the directory or a name collision (e.g. `afine.pth` exists as a directory). It is triggered only when the file is not already cached.","triggerScenarios":"`load_pretrained_weights` (afine) finds no cached `afine.pth`, downloads bytes successfully, but `File::create` fails: read-only or non-writable cache directory, quota exceeded, or `afine.pth` path is an existing directory.","commonSituations":"Cache directory created by root but process now runs as another user; interrupted prior run left `afine.pth` as a directory or stale artifact; disk quota/full filesystem on the cache volume; read-only bind mount of the cache.","solutions":["Verify the cache directory is writable by the current user: `ls -ld ~/.cache/burn-dataset/afine` and `chown`/`chmod` as needed","Delete a corrupt/stale `afine.pth` entry (`rm -rf ~/.cache/burn-dataset/afine/afine.pth`) so it is recreated","Check free space/quota on the cache filesystem (`df -h ~/.cache`)","Pre-place a valid `afine.pth` in the cache so download is skipped"],"exampleFix":"// before (shell)\ncargo run -- eval   # panics: Failed to create cache file\n// after (shell)\nrm -f ~/.cache/burn-dataset/afine/afine.pth\nchmod u+w ~/.cache/burn-dataset/afine\ncargo run -- eval","handlingStrategy":"validation","validationCode":"let cache = dirs::cache_dir().unwrap().join(\"burn-dataset\").join(\"afine\");\nlet path = cache.join(\"afine.pth\");\n// ensure neither a directory nor an unwritable entry blocks creation\nif path.is_dir() { eprintln!(\"{} is a directory; remove it\", path.display()); }\nassert!(dirs::cache_dir().unwrap().metadata().map(|m| !m.permissions().readonly()).unwrap_or(false));","typeGuard":"fn cache_file_creatable(dir: &std::path::Path, name: &str) -> bool {\n    let p = dir.join(name);\n    !p.is_dir() && std::fs::write(dir.join(\".probe\"), b\"\").is_ok()\n}","tryCatchPattern":"let weights = std::panic::catch_unwind(load_pretrained_weights)\n    .map_err(|_| anyhow!(\"failed to create afine.pth cache file; check permissions/quota\"))?;","preventionTips":["Delete stale or corrupt entries under the afine cache dir after interrupted runs","Check free space/quota before bulk downloads","Avoid sharing one cache dir between concurrently downloading processes without locking","Pre-populate the cache (download once) before parallel/CI runs"],"tags":["panic","filesystem","permissions","download"],"backgroundTag":"cache-write-failed","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}