{"record":{"id":"2e0df7c8a31aee82","repo":"lima-vm/lima","slug":"failed-to-stop-the-instance-q-via-systemctl-w","errorCode":null,"errorMessage":"failed to stop the instance %#q via systemctl: %w","messagePattern":"failed to stop the instance %#q via systemctl: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/autostart/systemd/systemd.go","lineNumber":71,"sourceCode":"}\n\n// AutoStartedUnitName returns the systemd service name if the instance is started by systemd.\nfunc AutoStartedUnitName() string {\n\treturn CurrentUnitName()\n}\n\nfunc RequestStart(ctx context.Context, inst *limatype.Instance) error {\n\tif err := systemctl(ctx, \"--user\", \"start\", UnitNameFrom(inst.Name)); err != nil {\n\t\treturn fmt.Errorf(\"failed to start the instance %#q via systemctl: %w\", inst.Name, err)\n\t}\n\treturn nil\n}\n\nfunc RequestStop(ctx context.Context, inst *limatype.Instance) (bool, error) {\n\tif inst.AutoStartedIdentifier == UnitNameFrom(inst.Name) {\n\t\tlogrus.Infof(\"Stopping the instance %#q started by systemd\", inst.Name)\n\t\tif err := systemctl(ctx, \"--user\", \"stop\", inst.AutoStartedIdentifier); err != nil {\n\t\t\treturn false, fmt.Errorf(\"failed to stop the instance %#q via systemctl: %w\", inst.Name, err)\n\t\t}\n\t\treturn true, nil\n\t}\n\treturn false, nil\n}\n","sourceCodeStart":53,"sourceCodeEnd":77,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/autostart/systemd/systemd.go#L53-L77","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check `systemctl --user status <identifier>`; if the unit is failed, `systemctl --user reset-failed <identifier>` then retry stop","If the unit no longer exists, stop the instance through lima instead: `limactl stop <instance>`","Ensure a systemd user session is available (XDG_RUNTIME_DIR set, `loginctl enable-linger <user>` for headless)","Clear the stale identifier by re-registering or editing the instance's state so RequestStop takes the no-op path"],"exampleFix":"systemctl --user reset-failed lima-default.service || true\nlimactl stop default","handlingStrategy":"fallback","validationCode":"id := \"lima-\" + inst.Name + \".service\"\nout, err := exec.Command(\"systemctl\", \"--user\", \"status\", id).Output()\nif err != nil {\n    // unit unknown or in failed state; reset-failed or use limactl stop instead\n}\n","typeGuard":null,"tryCatchPattern":"stopped, err := mgr.RequestStop(ctx, inst)\nif err != nil {\n    exec.Command(\"systemctl\", \"--user\", \"reset-failed\", \"lima-\"+inst.Name+\".service\").Run()\n    if err2 := limainstance.Stop(ctx, inst); err2 != nil {\n        return fmt.Errorf(\"systemd stop and lima stop both failed: %v / %v\", err, err2)\n    }\n}\n","preventionTips":["Don't delete unit files while their instance is running","Keep a user systemd session alive (enable-linger) so stop jobs can run","After copying instances between machines, clear stale AutoStartedIdentifier state","Prefer `limactl stop` for interactive use; RequestStop is for the autostart lifecycle"],"tags":["systemd","autostart","linux","vm-stop"],"backgroundTag":"systemctl-stop-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}