{"record":{"id":"c76824a2e8c3e03b","repo":"lima-vm/lima","slug":"failed-to-kill-process-with-pid-d-w","errorCode":null,"errorMessage":"failed to kill process with pid %d: %w","messagePattern":"failed to kill process with pid (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/networks/usernet/recoincile.go","lineNumber":148,"sourceCode":"func Stop(ctx context.Context, name string) error {\n\tlogrus.Debugf(\"Make sure usernet network is stopped\")\n\tpidFile, err := PIDFile(name)\n\tif err != nil {\n\t\treturn err\n\t}\n\tpid, _ := store.ReadPIDFile(pidFile)\n\n\tif pid != 0 {\n\t\tlogrus.Debugf(\"Stopping usernet daemon\")\n\n\t\terr = writeLeases(ctx, name)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif err := osutil.SysKill(pid, osutil.SigInt); err != nil {\n\t\t\tlogrus.Error(err)\n\t\t\treturn fmt.Errorf(\"failed to kill process with pid %d: %w\", pid, err)\n\t\t}\n\t}\n\n\t// wait for daemons to terminate (up to 5s) before stopping, otherwise the sockets may not get deleted which\n\t// will cause subsequent start commands to fail.\n\tstartWaiting := time.Now()\n\tfor {\n\t\tif pid, _ := store.ReadPIDFile(pidFile); pid == 0 {\n\t\t\tbreak\n\t\t}\n\t\tif time.Since(startWaiting) > 5*time.Second {\n\t\t\tlogrus.Infof(\"usernet network still running after 5 seconds. Attempting to forcibly kill\")\n\t\t\tif err := osutil.SysKill(pid, osutil.SigKill); err != nil {\n\t\t\t\tlogrus.Error(err)\n\t\t\t}\n\t\t\tbreak\n\t\t}\n\t\ttime.Sleep(500 * time.Millisecond)","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/networks/usernet/recoincile.go#L130-L166","documentation":"Lima's usernet network Stop() reads the daemon PID from the pidfile and sends SIGINT via osutil.SysKill. When the kill syscall fails (process no longer exists, permission denied, etc.), the underlying OS error is wrapped with the PID and returned, stopping network shutdown.","triggerScenarios":"Calling usernet.Stop(ctx, name) (via limactl stop / stopNetwork) when the pidfile contains a PID that has already exited (ESRCH), or when the lima user lacks permission to signal the process (EPERM), or the process is a zombie/owned by another user.","commonSituations":"Stale PID files left after a crash of the usernet/vde daemon; running limactl as a different user than the one that started the VM; the daemon died between reading the PID file and sending the signal.","solutions":["Verify the daemon is actually running: ps -p <pid>; if it is gone, the error is harmless — remove the stale pidfile under <LIMA_HOME>/networks/user-v2 and retry.","Check process ownership/permissions; run limactl as the same user that started the network, or fix permission to signal the PID.","Delete the stale pidfile (<LIMA_HOME>/_config pidfile path for the network) and re-run limactl start to restart the usernet daemon cleanly.","If SIGINT keeps failing, kill -9 the leftover process manually and clean up sockets in the usernet dir."],"exampleFix":"// before\nif err := osutil.SysKill(pid, osutil.SigInt); err != nil {\n\treturn fmt.Errorf(\"failed to kill process with pid %d: %w\", pid, err)\n}\n// after\nif err := osutil.SysKill(pid, osutil.SigInt); err != nil {\n\tif errors.Is(err, os.ErrProcessDone) {\n\t\treturn nil // process already exited; stale pidfile\n\t}\n\treturn fmt.Errorf(\"failed to kill process with pid %d: %w\", pid, err)\n}","handlingStrategy":"try-catch","validationCode":"const pid = fs.readFileSync(path.join(limaHome,'networks/user-v2/<name>/pid'),'utf8').trim();\nif (pid) {\n  try { process.kill(Number(pid), 0); } catch (e) {\n    // stale pidfile: daemon already dead; safe to clean up pidfile and sockets\n  }\n}","typeGuard":"function isKillFailure(err) {\n  return err != null && /failed to kill process with pid \\d+/.test(err.message);\n}","tryCatchPattern":"try {\n  await stopNetwork(name);\n} catch (err) {\n  if (/failed to kill process with pid/.test(err.message)) {\n    // inspect ownership (ESRCH vs EPERM), remove stale pidfile, retry once\n  } else throw err;\n}","preventionTips":["Always stop instances with the same user that started them.","Periodically clean stale pidfiles under <LIMA_HOME>/networks/.","Check `ps -p <pid>` before assuming the error is fatal.","Keep lima upgraded; newer versions handle already-exited daemons more gracefully."],"tags":["network","usernet","process-kill","unix"],"backgroundTag":"process-kill-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}