{"record":{"id":"7f555ee061eea125","repo":"xai-org/grok-build","slug":"failed-to-delete-btrfs-snapshot-at","errorCode":null,"errorMessage":"failed to delete BTRFS snapshot at {}: {}","messagePattern":"failed to delete BTRFS snapshot at (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-fast-worktree/src/btrfs/snapshot.rs","lineNumber":331,"sourceCode":"}\n\n/// Delete a BTRFS subvolume/snapshot.\npub fn delete_snapshot(path: &Path) -> Result<()> {\n    let mut cmd = Command::new(\"btrfs\");\n    xai_tty_utils::detach_std_command(&mut cmd);\n    cmd.stdin(Stdio::null());\n    // Pass the path as OsStr (no lossy `to_str`) so a non-UTF-8 path can never\n    // silently collapse to \".\" and delete the current directory's subvolume.\n    let output = cmd\n        .arg(\"subvolume\")\n        .arg(\"delete\")\n        .arg(path)\n        .output()\n        .with_context(|| \"failed to execute btrfs subvolume delete command\")?;\n\n    if !output.status.success() {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        bail!(\n            \"failed to delete BTRFS snapshot at {}: {}\",\n            path.display(),\n            stderr.trim()\n        );\n    }\n\n    Ok(())\n}\n\n/// Validate that `snapshot_path` is a safe target for a privileged\n/// `btrfs subvolume delete`.\n///\n/// Guards against destroying an arbitrary subvolume (e.g. the live source repo,\n/// or another session/user's snapshot) via a stale, confused, or planted\n/// symlink or `*.btrfs-meta.json` entry. Returns `true` only when the path:\n/// - contains no `..` component,\n/// - is not itself a symlink (`lstat`, so a planted symlink can't redirect the\n///   delete to a subvolume elsewhere),","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-fast-worktree/src/btrfs/snapshot.rs#L313-L349","documentation":"`delete_snapshot` runs `btrfs subvolume delete <path>`; if the command exits non-zero, it bails with this message including the path and btrfs's stderr. The btrfs tool refused to delete the subvolume snapshot.","triggerScenarios":"Calling `delete_snapshot` (directly or from `create_snapshot_with_symlink`'s recreate path) when the path isn't a btrfs subvolume, is busy (open files, mounted), or the user lacks privileges.","commonSituations":"Snapshot still mounted or referenced by a running worktree/container; target is a plain directory, not a subvolume; running without root/CAP_SYS_ADMIN; filesystem errors or read-only subvolume; `btrfs` binary missing in containers.","solutions":["Read the stderr in the error message for btrfs's exact reason ('not a subvolume', 'device busy', 'permission denied').","Ensure nothing uses the snapshot: unmount it, stop containers/worktrees holding it open, then retry.","Verify the path is actually a btrfs subvolume with `btrfs subvolume show <path>`; delete plain directories with `rm -rf` instead.","Run with sufficient privileges and confirm the `btrfs` tool exists in the environment.","For recreation flows, handle this error by cleaning dependents first rather than retrying the same call blindly."],"exampleFix":"// before\ndelete_snapshot(Path::new(\"/mnt/btrfs/snapshots/wt-42\"))?; // busy: still mounted\n// after\n// ensure the snapshot is not mounted, then retry\nrun(\"umount /mnt/btrfs/snapshots/wt-42\");\ndelete_snapshot(Path::new(\"/mnt/btrfs/snapshots/wt-42\"))?;\n","handlingStrategy":"try-catch","validationCode":"use std::process::Command;\nuse std::path::Path;\nfn snapshot_deletable(p: &Path) -> bool {\n    // must be a btrfs subvolume and not currently mounted\n    let is_subvol = Command::new(\"btrfs\").args([\"subvolume\", \"show\"]).arg(p)\n        .output().map(|o| o.status.success()).unwrap_or(false);\n    let not_mounted = !std::fs::read_to_string(\"/proc/mounts\")\n        .map(|m| m.contains(&p.to_string_lossy().to_string())).unwrap_or(true);\n    is_subvol && not_mounted\n}","typeGuard":null,"tryCatchPattern":"match delete_snapshot(&path) {\n    Err(e) if e.to_string().contains(\"failed to delete BTRFS snapshot\") => {\n        eprintln!(\"btrfs delete refused: {e}\");\n        // stop consumers / unmount, then retry once\n    }\n    r => r?,\n}","preventionTips":["Unmount snapshots and stop worktrees/containers using them before deletion","Confirm the path is a subvolume (btrfs subvolume show) before deleting","Run with root or CAP_SYS_ADMIN","Retry deletion after consumers release the path rather than immediately"],"tags":["btrfs","filesystem","delete","external-command"],"backgroundTag":"btrfs-subvolume-delete-failed","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}