{"record":{"id":"eee3508261480201","repo":"tracel-ai/burn","slug":"failed-to-write-weights-eee350","errorCode":null,"errorMessage":"Failed to write weights","messagePattern":"Failed to write weights","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-train/src/metric/vision/lpips/weights.rs","lineNumber":63,"sourceCode":"fn get_cache_dir() -> PathBuf {\n    let cache_dir = dirs::cache_dir()\n        .expect(\"Could not get cache directory\")\n        .join(\"burn-dataset\")\n        .join(\"lpips\");\n\n    if !cache_dir.exists() {\n        create_dir_all(&cache_dir).expect(\"Failed to create cache directory\");\n    }\n\n    cache_dir\n}\n\n/// Download file if not cached and return the cache path.\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 and load pretrained weights into an LPIPS module.\n///\n/// This loads both:\n/// 1. ImageNet pretrained backbone weights (VGG16/AlexNet/SqueezeNet)\n/// 2. LPIPS trained linear layer weights\n///\n/// Weights are cached in the user's cache directory to avoid re-downloading.\n///\n/// # Arguments\n///\n/// * `lpips` - The LPIPS module to load weights into.\n/// * `net` - The network type (determines which weights to download).\n///\n/// # Returns\n///","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-train/src/metric/vision/lpips/weights.rs#L45-L81","documentation":"This panic comes from an `expect` in `download_if_needed` in crates/burn-train/src/metric/vision/lpips/weights.rs:63. The LPIPS metric downloads pretrained network weights, caches them on disk, and this expect fires when `File::write_all` fails to persist the already-downloaded bytes to the cache file. The download itself succeeded, so the failure is on the local filesystem side (disk, permissions, or I/O error during the write).","triggerScenarios":"Calling `load_pretrained_weights` for LPIPS when the cache file does not exist: `download_file_as_bytes` succeeds, `File::create` on the cache path succeeds, but `write_all(&bytes)` returns Err (e.g. disk filled up mid-write, file handle became invalid, or an I/O error such as EIO/ENOSPC).","commonSituations":"Disk quota or full filesystem after the download consumed the remaining space; container/CI runners with tiny tmpfs volumes; antivirus or backup software locking the newly created file; flaky network mounts where the file descriptor goes stale between create and write.","solutions":["Free disk space in the cache partition (`df -h ~/.cache`) and delete any partial cache file, then retry","Verify write permissions on the cache directory (`ls -ld ~/.cache/burn-pretrained-models`) and fix ownership/ACLs if needed","Pre-download the weights manually to the expected cache path so the `download_if_needed` write path is skipped entirely","Move the cache to a more reliable local filesystem (e.g. set HOME/XDG_CACHE_HOME to a local disk instead of an NFS mount)"],"exampleFix":"// before: unguarded write panics with 'Failed to write weights'\nlet mut file = File::create(cache_path).expect(\"Failed to create cache file\");\nfile.write_all(&bytes).expect(\"Failed to write weights\");\n// after: clean error instead of panic\nlet mut file = File::create(cache_path)\n    .map_err(|e| anyhow!(\"Failed to create cache file: {e}\"))?;\nfile.write_all(&bytes)\n    .with_context(|| format!(\"Failed to write weights to {}\", cache_path.display()))?;","handlingStrategy":"try-catch","validationCode":"// Rust: check writability and space before calling load_pretrained_weights\nuse std::fs::OpenOptions;\nlet cache_path = dirs::home_dir().unwrap().join(\n    \".cache/burn-pretrained-models/metric/lpips/weights\");\nif let Some(parent) = cache_path.parent() {\n    assert!(parent.exists() || std::fs::create_dir_all(parent).is_ok(),\n        \"cache dir not creatable\");\n}\n// probe write access\nOpenOptions::new().write(true).create(true)\n    .open(cache_path.with_extension(\".write-test\"))\n    .expect(\"cache path not writable\");","typeGuard":"fn is_cache_writable(cache_path: &std::path::Path) -> bool {\n    cache_path.parent().map(|p| p.is_dir()).unwrap_or(false)\n        && OpenOptions::new().write(true).create(true)\n            .open(cache_path.with_extension(\".probe\")).is_ok()\n}","tryCatchPattern":"// catch_unwind since the library panics via expect\nlet result = std::panic::catch_unwind(|| {\n    lpips::load_pretrained_weights(device)\n});\nmatch result {\n    Ok(Ok(weights)) => {/* use weights */},\n    Ok(Err(e)) => eprintln!(\"load failed: {e}\"),\n    Err(panic) => eprintln!(\"panicked while writing weights: {panic:?}\"),\n}","preventionTips":["Monitor free space on the cache partition before heavy runs","Use a local (non-NFS) filesystem for the model cache","Pre-seed the cache by downloading weights once with normal user permissions","In CI, cache ~/.cache/burn-pretrained-models between runs to skip the write path"],"tags":["filesystem","io","panics","cache","burn"],"backgroundTag":"cache-file-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"}