{"record":{"id":"6f2e5209d7abc72e","repo":"lima-vm/lima","slug":"failed-to-unlock-for-reuse-in-the-same-instance","errorCode":null,"errorMessage":"failed to unlock for reuse in the same instance: %w","messagePattern":"failed to unlock for reuse in the same instance: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/store/disk.go","lineNumber":107,"sourceCode":"}\n\nfunc (d *Disk) Lock(instanceDir string) error {\n\tinUseBy := filepath.Join(d.Dir, filenames.InUseBy)\n\treturn os.Symlink(instanceDir, inUseBy)\n}\n\nfunc (d *Disk) Unlock() error {\n\tinUseBy := filepath.Join(d.Dir, filenames.InUseBy)\n\treturn os.Remove(inUseBy)\n}\n\nfunc (d *Disk) LockForInstance(instanceDir string) error {\n\tif d.Instance != \"\" {\n\t\tif d.InstanceDir != instanceDir {\n\t\t\treturn fmt.Errorf(\"in use by instance %#q\", d.Instance)\n\t\t}\n\t\tif err := d.Unlock(); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to unlock for reuse in the same instance: %w\", err)\n\t\t}\n\t}\n\treturn d.Lock(instanceDir)\n}\n","sourceCodeStart":89,"sourceCodeEnd":112,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/store/disk.go#L89-L112","documentation":"When re-attaching a disk to the same instance (restart scenario), LockForInstance first calls Unlock to clear the previous per-instance lock; if that unlock fails the error is wrapped so the caller knows reuse-in-place failed. Underneath this is an flock/remove failure on the lock file.","triggerScenarios":"LockForInstance called with d.Instance == existing instance and d.InstanceDir == instanceDir, but d.Unlock() errors — e.g. the lock file was removed externally while locked, or unlocking fails due to permissions or an fd state problem.","commonSituations":"Stale or manually deleted lock files in the disk directory; disk directory permissions changed; concurrent processes fighting over the same disk lock; NFS mounts where flock is unreliable.","solutions":["Check permissions on the disk directory and its lock files under ~/.lima/disks/<disk>/ and restore ownership to the current user.","Stop all Lima instances using the disk, then remove stale lock artifacts (in-use-by) and retry the operation.","Avoid running Lima home on NFS/network filesystems where flock semantics are unreliable; use a local path for LIMA_HOME.","If transient, simply retry the start/attach after a short delay."],"exampleFix":"// before\nlimactl start myinst   # failed to unlock for reuse in the same instance\nls -la ~/.lima/disks/mydisk/   # wrong owner / stale locks\n// after\nlimactl stop myinst\nsudo chown -R \"$USER\" ~/.lima/disks/mydisk\nrm -f ~/.lima/disks/mydisk/in-use-by\nlimactl start myinst","handlingStrategy":"retry","validationCode":"if info, err := os.Stat(filepath.Join(diskDir, \"in-use-by\")); err == nil {\n    b, _ := os.ReadFile(filepath.Join(diskDir, \"in-use-by\"))\n    // verify recorded instance is stopped before reuse\n    _ = b\n}","typeGuard":null,"tryCatchPattern":"err := disk.LockForInstance(instanceDir)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to unlock\") {\n        // check permissions / stale locks, stop instance, retry\n    }\n    return err\n}","preventionTips":["Keep disk directories owned and writable by the running user","Avoid LIMA_HOME on NFS (unreliable flock)","Clean stale lock files after crashes before restarting the instance"],"tags":["disk","locking","flock"],"backgroundTag":"disk-lock-unlock-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}