{"record":{"id":"7643abd302a32800","repo":"lima-vm/lima","slug":"failed-to-run-wsl-exe-terminate-s-w-out","errorCode":null,"errorMessage":"failed to run `wsl.exe --terminate %s`: %w (out=%#q)","messagePattern":"failed to run `wsl\\.exe --terminate (.+?)`: %w \\(out=%#q\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/driver/wsl2/vm_windows.go","lineNumber":66,"sourceCode":"\t\tinstanceDir,\n\t\tbaseDisk,\n\t}, executil.WithContext(ctx))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to run `wsl.exe --import %s %s %s`: %w (out=%#q)\",\n\t\t\tdistroName, instanceDir, baseDisk, err, out)\n\t}\n\treturn nil\n}\n\n// stopVM calls WSL to stop a running VM.\nfunc stopVM(ctx context.Context, distroName string) error {\n\tout, err := executil.RunUTF16leCommand([]string{\n\t\t\"wsl.exe\",\n\t\t\"--terminate\",\n\t\tdistroName,\n\t}, executil.WithContext(ctx))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to run `wsl.exe --terminate %s`: %w (out=%#q)\",\n\t\t\tdistroName, err, out)\n\t}\n\treturn nil\n}\n\n//go:embed lima-init.TEMPLATE\nvar limaBoot string\n\n// provisionVM starts Lima's boot process inside an already imported VM.\nfunc provisionVM(ctx context.Context, instanceDir, instanceName, distroName string, errCh chan<- error) error {\n\tciDataPath := filepath.Join(instanceDir, filenames.CIDataISODir)\n\tm := map[string]string{\n\t\t\"CIDataPath\": ciDataPath,\n\t}\n\tlimaBootB, err := textutil.ExecuteTemplate(limaBoot, m)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to construct wsl boot.sh script: %w\", err)\n\t}","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/driver/wsl2/vm_windows.go#L48-L84","documentation":"stopVM runs `wsl.exe --terminate <distroName>` to stop the Lima WSL distro. If wsl.exe exits non-zero, the driver wraps the error and decoded output. This means WSL refused to terminate the distribution (or the command itself failed).","triggerScenarios":"Stop() (or an anonymous caller) invokes stopVM and wsl.exe --terminate returns non-zero: distro not registered, name mismatch, or WSL service failure.","commonSituations":"Distro already gone (double-stop, instance deleted concurrently); WSL service wedged after host sleep; distro name drifted from what WSL has registered; wsl.exe unavailable in PATH for the calling process.","solutions":["Check `wsl.exe --list --verbose` for the distro's actual registered name and state","If the distro is already Stopped, treat the error as harmless and continue (idempotent stop)","Run `wsl.exe --shutdown` to reset the WSL VM if terminate keeps failing, then retry","Read the out= field for the wsl.exe error code (e.g. not-found) and act accordingly","Restart the LxssManager/WSL service or reboot if the WSL service is unresponsive"],"exampleFix":"// before\nerr := driver.Stop(ctx) // terminate failed\n// after\nif err := driver.Stop(ctx); err != nil {\n    if !strings.Contains(err.Error(), \"not found\") {\n        return err // only real failures\n    } // already stopped: ignore\n}","handlingStrategy":"fallback","validationCode":"// confirm distro is running before terminating\nout, _ := exec.Command(\"wsl.exe\", \"--list\", \"--verbose\").Output()\nif !strings.Contains(decodeUTF16le(string(out)), \"Running\") {\n    return nil // already stopped; skip terminate\n}","typeGuard":null,"tryCatchPattern":"if err := driver.Stop(ctx); err != nil {\n    if strings.Contains(err.Error(), \"--terminate\") {\n        // fallback: reset WSL entirely\n        exec.Command(\"wsl.exe\", \"--shutdown\").Run()\n    }\n    return err\n}","preventionTips":["Avoid double-stop of the same instance concurrently","Don't delete an instance while it's stopping","Keep wsl.exe on PATH for the process environment","Reset WSL after host sleep/resume if terminate fails repeatedly"],"tags":["wsl2","windows","wsl-exe","vm-stop"],"backgroundTag":"wsl-command-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}