{"record":{"id":"cde4cf159f0dd776","repo":"lima-vm/lima","slug":"pidfile-q-already-exists","errorCode":null,"errorMessage":"pidfile %#q already exists","messagePattern":"pidfile %#q already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/limactl/usernet.go","lineNumber":44,"sourceCode":"\t}\n\thostagentCommand.Flags().StringP(\"pidfile\", \"p\", \"\", \"Write PID to file\")\n\thostagentCommand.Flags().StringP(\"endpoint\", \"e\", \"\", \"Exposes usernet API(s) on this endpoint\")\n\thostagentCommand.Flags().String(\"listen-qemu\", \"\", \"Listen for QMEU connections\")\n\thostagentCommand.Flags().String(\"listen\", \"\", \"Listen on a Unix socket and receive Bess-compatible FDs as SCM_RIGHTS messages\")\n\thostagentCommand.Flags().String(\"subnet\", \"192.168.5.0/24\", \"Sets subnet value for the usernet network\")\n\thostagentCommand.Flags().Int(\"mtu\", 1500, \"mtu\")\n\thostagentCommand.Flags().StringToString(\"leases\", nil, \"Pass default static leases for startup. Eg: '192.168.104.1=52:55:55:b3:bc:d9,192.168.104.2=5a:94:ef:e4:0c:df' \")\n\treturn hostagentCommand\n}\n\nfunc usernetAction(cmd *cobra.Command, _ []string) error {\n\tpidfile, err := cmd.Flags().GetString(\"pidfile\")\n\tif err != nil {\n\t\treturn err\n\t}\n\tif pidfile != \"\" {\n\t\tif _, err := os.Stat(pidfile); !errors.Is(err, os.ErrNotExist) {\n\t\t\treturn fmt.Errorf(\"pidfile %#q already exists\", pidfile)\n\t\t}\n\t\tif err := os.WriteFile(pidfile, []byte(strconv.Itoa(os.Getpid())+\"\\n\"), 0o644); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tdefer os.RemoveAll(pidfile)\n\t}\n\tendpoint, err := cmd.Flags().GetString(\"endpoint\")\n\tif err != nil {\n\t\treturn err\n\t}\n\tqemuSocket, err := cmd.Flags().GetString(\"listen-qemu\")\n\tif err != nil {\n\t\treturn err\n\t}\n\tfdSocket, err := cmd.Flags().GetString(\"listen\")\n\tif err != nil {\n\t\treturn err\n\t}","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/cmd/limactl/usernet.go#L26-L62","documentation":"Thrown by `limactl usernet --pidfile PATH` when the given pidfile path already exists. The tool refuses to overwrite an existing pidfile to avoid clobbering a running usernet daemon's PID record; os.Stat is used with errors.Is(err, os.ErrNotExist) so any stat result other than 'not exist' (including real existence or stat errors) triggers this error.","triggerScenarios":"Running `limactl usernet --pidfile /path/pid` when /path/pid exists from a previous run that was not cleaned up, or when another usernet instance is currently running and holding that pidfile.","commonSituations":"A previous usernet process crashed without its `defer os.RemoveAll(pidfile)` running (SIGKILL, machine reboot); two terminal sessions starting usernet with the same pidfile path; a stale pidfile left behind after an abnormal shutdown.","solutions":["Verify no usernet process is running (check the PID in the existing pidfile with `ps -p $(cat /path/pid)`), then delete the stale pidfile and retry.","If a process is still running, kill it cleanly or use a different pidfile path.","Choose a unique pidfile path per usernet invocation to avoid collisions."],"exampleFix":"// before\nlimactl usernet --pidfile /tmp/lima-usernet.pid\n// pidfile \"/tmp/lima-usernet.pid\" already exists\n// after\nps -p $(cat /tmp/lima-usernet.pid) || rm /tmp/lima-usernet.pid\nlimactl usernet --pidfile /tmp/lima-usernet.pid","handlingStrategy":"validation","validationCode":"if pidfile != \"\" {\n    if _, err := os.Stat(pidfile); err == nil {\n        // decide: reuse, kill the old process, or pick another path\n        pid, _ := os.ReadFile(pidfile)\n        fmt.Printf(\"pidfile held by pid %s\\n\", pid)\n        os.Exit(1)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := usernetCmd.Execute(); err != nil && strings.Contains(err.Error(), \"already exists\") {\n    // remove stale pidfile after confirming the process is gone, then retry once\n}","preventionTips":["Use unique pidfile paths per invocation (e.g. include an instance-specific suffix).","Always stop usernet via a signal that lets its cleanup deferred RemoveAll run (avoid SIGKILL).","Before starting, check whether the pid in an existing pidfile is still alive."],"tags":["cli","filesystem","pidfile","network"],"backgroundTag":"file-already-exists","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}