Skip to content

Commit 11a7943

Browse files
authored
Merge pull request #149 from rancher-sandbox/verify-pid
2 parents 1db1182 + b3b0583 commit 11a7943

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pkg/store/instance.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"strconv"
88
"strings"
9+
"syscall"
910

1011
"github.com/lima-vm/lima/pkg/limayaml"
1112
"github.com/lima-vm/lima/pkg/store/filenames"
@@ -93,7 +94,8 @@ func Inspect(instName string) (*Instance, error) {
9394
return inst, nil
9495
}
9596

96-
// readPIDFile returns 0 if the PID file does not exist
97+
// readPIDFile returns 0 if the PID file does not exist or the process has already terminated
98+
// (in which case the PID file will be removed).
9799
func readPIDFile(path string) (int, error) {
98100
b, err := os.ReadFile(path)
99101
if err != nil {
@@ -102,5 +104,21 @@ func readPIDFile(path string) (int, error) {
102104
}
103105
return 0, err
104106
}
105-
return strconv.Atoi(strings.TrimSpace(string(b)))
107+
pid, err := strconv.Atoi(strings.TrimSpace(string(b)))
108+
if err != nil {
109+
return 0, err
110+
}
111+
proc, err := os.FindProcess(pid)
112+
if err != nil {
113+
return 0, err
114+
}
115+
err = proc.Signal(syscall.Signal(0))
116+
if err != nil {
117+
if errors.Is(err, os.ErrProcessDone) {
118+
os.Remove(path)
119+
return 0, nil
120+
}
121+
return 0, err
122+
}
123+
return pid, nil
106124
}

0 commit comments

Comments
 (0)