lima-vm/lima · error
failed to unlock for reuse in the same instance: %w
Error message
failed to unlock for reuse in the same instance: %w
What it means
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.
Source
Thrown at pkg/store/disk.go:107
}
func (d *Disk) Lock(instanceDir string) error {
inUseBy := filepath.Join(d.Dir, filenames.InUseBy)
return os.Symlink(instanceDir, inUseBy)
}
func (d *Disk) Unlock() error {
inUseBy := filepath.Join(d.Dir, filenames.InUseBy)
return os.Remove(inUseBy)
}
func (d *Disk) LockForInstance(instanceDir string) error {
if d.Instance != "" {
if d.InstanceDir != instanceDir {
return fmt.Errorf("in use by instance %#q", d.Instance)
}
if err := d.Unlock(); err != nil {
return fmt.Errorf("failed to unlock for reuse in the same instance: %w", err)
}
}
return d.Lock(instanceDir)
}
View on GitHub (pinned to dd909d0973)
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.
Example fix
// before limactl start myinst # failed to unlock for reuse in the same instance ls -la ~/.lima/disks/mydisk/ # wrong owner / stale locks // after limactl stop myinst sudo chown -R "$USER" ~/.lima/disks/mydisk rm -f ~/.lima/disks/mydisk/in-use-by limactl start myinst
Defensive patterns
Strategy: retry
Validate before calling
if info, err := os.Stat(filepath.Join(diskDir, "in-use-by")); err == nil {
b, _ := os.ReadFile(filepath.Join(diskDir, "in-use-by"))
// verify recorded instance is stopped before reuse
_ = b
} Try / catch
err := disk.LockForInstance(instanceDir)
if err != nil {
if strings.Contains(err.Error(), "failed to unlock") {
// check permissions / stale locks, stop instance, retry
}
return err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- failed to attach disk %#q: %w
- in use by instance %#q
- disk format %#q not supported, use `qcow2` or `raw` instead
- disk %#q already exists (%#q)
- failed to remove a directory %#q: %w
AI-assisted analysis of lima-vm/lima@dd909d0973 (2026-09-01).
Data as JSON: /api/errors/6f2e5209d7abc72e.
Report an issue: GitHub.