{"record":{"id":"56a6a90c1a2bf3e7","repo":"lima-vm/lima","slug":"another-hostagent-may-already-be-running-with-pid","errorCode":null,"errorMessage":"another hostagent may already be running with pid %d (pidfile %#q)","messagePattern":"another hostagent may already be running with pid (.+?) \\(pidfile %#q\\)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/limactl/hostagent.go","lineNumber":50,"sourceCode":"\t}\n\thostagentCommand.Flags().StringP(\"pidfile\", \"p\", \"\", \"Write PID to file\")\n\thostagentCommand.Flags().String(\"socket\", \"\", \"Path of hostagent socket\")\n\thostagentCommand.Flags().Bool(\"run-gui\", false, \"Run GUI synchronously within hostagent\")\n\thostagentCommand.Flags().String(\"guestagent\", \"\", \"Local file path (not URL) of lima-guestagent.OS-ARCH[.gz]\")\n\thostagentCommand.Flags().String(\"nerdctl-archive\", \"\", \"Local file path (not URL) of nerdctl-full-VERSION-GOOS-GOARCH.tar.gz\")\n\thostagentCommand.Flags().Bool(\"progress\", false, \"Show provision script progress by monitoring cloud-init logs\")\n\treturn hostagentCommand\n}\n\nfunc hostagentAction(cmd *cobra.Command, args []string) error {\n\tctx := cmd.Context()\n\tpidfile, err := cmd.Flags().GetString(\"pidfile\")\n\tif err != nil {\n\t\treturn err\n\t}\n\tif pidfile != \"\" {\n\t\tif existingPID, err := store.ReadPIDFile(pidfile); existingPID != 0 {\n\t\t\treturn fmt.Errorf(\"another hostagent may already be running with pid %d (pidfile %#q)\", existingPID, pidfile)\n\t\t} else if err != nil {\n\t\t\treturn fmt.Errorf(\"failed to determine if another hostagent is running: %w\", err)\n\t\t}\n\t\tif err := store.WritePIDFile(pidfile, os.Getpid()); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tdefer os.RemoveAll(pidfile)\n\t}\n\tsocket, err := cmd.Flags().GetString(\"socket\")\n\tif err != nil {\n\t\treturn err\n\t}\n\tif socket == \"\" {\n\t\treturn errors.New(\"socket must be specified (limactl version mismatch?)\")\n\t}\n\n\tinstName := args[0]\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/cmd/limactl/hostagent.go#L32-L68","documentation":"The hostagent startup detects that the PID file it was asked to write already contains a live PID, meaning another hostagent process may already be running for this instance. Starting a second hostagent would fight over the same instance, so hostagentAction aborts immediately after reading the pidfile.","triggerScenarios":"Running `limactl hostagent` (or an internal start path) with --pidfile pointing to a file where store.ReadPIDFile returns a non-zero PID that is still alive, i.e. a hostagent is already running for the instance.","commonSituations":"Double-starting an instance from two terminals; a stale-but-live PID file left after an aborted start; automation retried a start while the first start was still in progress.","solutions":["Check the PID in the pidfile (`cat <pidfile>; ps -p <pid>`) — if a hostagent is genuinely running, do not start another","Stop the existing instance with `limactl stop <instance>` before restarting","If the PID is dead but the file lingers, remove the stale pidfile and retry"],"exampleFix":"// before\nlimactl hostagent --pidfile ~/.lima/myinstance/hostagent.pid myinstance  # pidfile already held by live process\n// after\nlimactl stop myinstance && limactl hostagent --pidfile ~/.lima/myinstance/hostagent.pid myinstance","handlingStrategy":"validation","validationCode":"if existingPID, err := store.ReadPIDFile(pidfile); existingPID != 0 {\n    return fmt.Errorf(\"hostagent already running with pid %d\", existingPID)\n}","typeGuard":"func hostagentAlreadyRunning(pidfile string) bool {\n    pid, _ := store.ReadPIDFile(pidfile)\n    return pid != 0\n}","tryCatchPattern":"if err := startHostagent(); err != nil {\n    if strings.Contains(err.Error(), \"may already be running\") { /* skip start or stop existing */ }\n    return err\n}","preventionTips":["Always stop instances via `limactl stop` rather than killing processes","Check the pidfile PID with ps before manual starts","Avoid concurrent starts of the same instance in CI"],"tags":["cli","process","pidfile","lima"],"backgroundTag":"process-already-running","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}