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

  1. Check permissions on the disk directory and its lock files under ~/.lima/disks/<disk>/ and restore ownership to the current user.
  2. Stop all Lima instances using the disk, then remove stale lock artifacts (in-use-by) and retry the operation.
  3. Avoid running Lima home on NFS/network filesystems where flock semantics are unreliable; use a local path for LIMA_HOME.
  4. 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

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


AI-assisted analysis of lima-vm/lima@dd909d0973 (2026-09-01). Data as JSON: /api/errors/6f2e5209d7abc72e. Report an issue: GitHub.