lima-vm/lima · error

failed to stop the instance %#q via systemctl: %w

Error message

failed to stop the instance %#q via systemctl: %w

What it means

systemd.RequestStop runs `systemctl --user stop <AutoStartedIdentifier>` when the instance was started by systemd (inst.AutoStartedIdentifier matches its unit name). This error wraps a non-zero systemctl exit, meaning the stop job failed and the unit may still be running.

Source

Thrown at pkg/autostart/systemd/systemd.go:71

}

// AutoStartedUnitName returns the systemd service name if the instance is started by systemd.
func AutoStartedUnitName() string {
	return CurrentUnitName()
}

func RequestStart(ctx context.Context, inst *limatype.Instance) error {
	if err := systemctl(ctx, "--user", "start", UnitNameFrom(inst.Name)); err != nil {
		return fmt.Errorf("failed to start the instance %#q via systemctl: %w", inst.Name, err)
	}
	return nil
}

func RequestStop(ctx context.Context, inst *limatype.Instance) (bool, error) {
	if inst.AutoStartedIdentifier == UnitNameFrom(inst.Name) {
		logrus.Infof("Stopping the instance %#q started by systemd", inst.Name)
		if err := systemctl(ctx, "--user", "stop", inst.AutoStartedIdentifier); err != nil {
			return false, fmt.Errorf("failed to stop the instance %#q via systemctl: %w", inst.Name, err)
		}
		return true, nil
	}
	return false, nil
}

View on GitHub (pinned to dd909d0973)

Solutions

  1. Check `systemctl --user status <identifier>`; if the unit is failed, `systemctl --user reset-failed <identifier>` then retry stop
  2. If the unit no longer exists, stop the instance through lima instead: `limactl stop <instance>`
  3. Ensure a systemd user session is available (XDG_RUNTIME_DIR set, `loginctl enable-linger <user>` for headless)
  4. Clear the stale identifier by re-registering or editing the instance's state so RequestStop takes the no-op path

Example fix

systemctl --user reset-failed lima-default.service || true
limactl stop default
Defensive patterns

Strategy: fallback

Validate before calling

id := "lima-" + inst.Name + ".service"
out, err := exec.Command("systemctl", "--user", "status", id).Output()
if err != nil {
    // unit unknown or in failed state; reset-failed or use limactl stop instead
}

Try / catch

stopped, err := mgr.RequestStop(ctx, inst)
if err != nil {
    exec.Command("systemctl", "--user", "reset-failed", "lima-"+inst.Name+".service").Run()
    if err2 := limainstance.Stop(ctx, inst); err2 != nil {
        return fmt.Errorf("systemd stop and lima stop both failed: %v / %v", err, err2)
    }
}

Prevention

When it happens

Trigger: RequestStop called for an instance whose AutoStartedIdentifier equals UnitNameFrom(inst.Name), and `systemctl --user stop` fails — unit already in a failed state, systemd user session gone, or stale AutoStartedIdentifier pointing at a removed unit.

Common situations: Unit file deleted while the instance still runs; user session bus unavailable (headless SSH); instance crashed leaving the unit in failed state so stop returns error; instance data copied between machines/users so the identifier no longer matches any unit.

Related errors


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