{"record":{"id":"da2a5359fa0e1227","repo":"lima-vm/lima","slug":"failed-to-detach-asif-image-q-w-s","errorCode":null,"errorMessage":"failed to detach ASIF image %#q: %w: %s","messagePattern":"failed to detach ASIF image %#q: %w: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"pkg/imgutil/nativeimgutil/asifutil/asif_darwin.go","lineNumber":61,"sourceCode":"\t}\n\tattachArgs := []string{\"image\", \"attach\", \"--noMount\", path}\n\tout, err := exec.CommandContext(context.Background(), \"diskutil\", attachArgs...).Output()\n\tif err != nil {\n\t\treturn \"\", nil, fmt.Errorf(\"failed to attach ASIF image %#q: %w\", path, err)\n\t}\n\tdevicePath := strings.TrimSpace(string(out))\n\tf, err := os.OpenFile(devicePath, os.O_RDWR, 0o644)\n\tif err != nil {\n\t\t_ = DetachASIF(devicePath)\n\t\treturn \"\", nil, fmt.Errorf(\"failed to open ASIF device %#q: %w\", devicePath, err)\n\t}\n\treturn devicePath, f, err\n}\n\n// DetachASIF detaches the ASIF image device at the specified path.\nfunc DetachASIF(devicePath string) error {\n\tif output, err := exec.CommandContext(context.Background(), \"hdiutil\", \"detach\", devicePath).CombinedOutput(); err != nil {\n\t\treturn fmt.Errorf(\"failed to detach ASIF image %#q: %w: %s\", devicePath, err, output)\n\t}\n\treturn nil\n}\n\n// ResizeASIF resizes the ASIF image at the specified path to the given size.\nfunc ResizeASIF(path string, size int64) error {\n\tresizeArgs := []string{\"image\", \"resize\", \"--size\", fmt.Sprintf(\"%d\", size), path}\n\tif output, err := exec.CommandContext(context.Background(), \"diskutil\", resizeArgs...).CombinedOutput(); err != nil {\n\t\treturn fmt.Errorf(\"failed to resize ASIF image %#q: %w: %s\", path, err, output)\n\t}\n\treturn nil\n}\n\ntype AttachedDisk struct {\n\tDisk      string // whole disk device, e.g. \"disk4\" (GUID_partition_scheme)\n\tContainer string // APFS container partition, e.g. \"disk4s2\" (Apple_APFS)\n\tData      string // data volume device, e.g. \"disk7s5\"\n}","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/imgutil/nativeimgutil/asifutil/asif_darwin.go#L43-L79","documentation":"DetachASIF detaches an attached ASIF image using `hdiutil detach <devicePath>`. This error wraps a non-zero exit and includes the combined output for diagnosis. It means the image device could not be detached.","triggerScenarios":"Calling DetachASIF (from NewAttachedASIF's cleanup path, convertTo, or other callers) when hdiutil detach fails: the device is busy (open file handles), already detached, or the devicePath is invalid.","commonSituations":"Processes still holding the device open during teardown; double-detach after a partial failure; forced detach needed because a mount or helper is stuck.","solutions":["Close all file handles on the device (including f from NewAttachedASIF) before detaching","Retry the detach after a short delay (device busy is often transient)","Check the included hdiutil output; if it suggests force, run `hdiutil detach -force <devicePath>` manually","Ignore the error if it is 'not attached' during cleanup paths"],"exampleFix":"// before\nf.Close()\n_ = asifutil.DetachASIF(devicePath)\n// after\nf.Close()\ntime.Sleep(500 * time.Millisecond)\nif err := asifutil.DetachASIF(devicePath); err != nil {\n    log.Printf(\"detach failed (may need force): %v\", err)\n}","handlingStrategy":"retry","validationCode":"// preflight: ensure no open handles remain before detaching\nif f != nil { f.Close() }","typeGuard":null,"tryCatchPattern":"if err := asifutil.DetachASIF(devicePath); err != nil {\n    time.Sleep(time.Second)\n    if err2 := asifutil.DetachASIF(devicePath); err2 != nil {\n        log.Printf(\"detach failed, may need `hdiutil detach -force`: %v\", err2) // non-fatal in cleanup\n    }\n}","preventionTips":["Close all file handles on the device before detaching","Add a short delay between last I/O and detach","Treat detach failures during teardown as warnings, log diskutil output"],"tags":["macos","hdiutil","detach","asif"],"backgroundTag":"disk-busy-detach-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}