Merge pull request #5188 from ollama/jyan/tmpdir2

fix: skip os.removeAll() if PID does not exist
This commit is contained in:
Josh 2024-06-20 10:40:59 -07:00 committed by GitHub
commit 0195d6a2f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -77,20 +77,27 @@ func cleanupTmpDirs() {
continue continue
} }
raw, err := os.ReadFile(filepath.Join(d, "ollama.pid")) raw, err := os.ReadFile(filepath.Join(d, "ollama.pid"))
if err == nil { if err != nil {
slog.Warn("failed to read ollama.pid", "path", d, "error", err)
// No pid, ignore this tmpdir
continue
}
pid, err := strconv.Atoi(string(raw)) pid, err := strconv.Atoi(string(raw))
if err == nil { if err != nil {
if proc, err := os.FindProcess(pid); err == nil && !errors.Is(proc.Signal(syscall.Signal(0)), os.ErrProcessDone) { slog.Warn("failed to parse pid", "path", d, "error", err)
continue
}
proc, err := os.FindProcess(pid)
if err == nil && !errors.Is(proc.Signal(syscall.Signal(0)), os.ErrProcessDone) {
slog.Warn("found running ollama", "pid", pid, "path", d)
// Another running ollama, ignore this tmpdir // Another running ollama, ignore this tmpdir
continue continue
} }
}
} else { if err := os.Remove(d); err != nil {
slog.Debug("failed to open ollama.pid", "path", d, "error", err) slog.Warn("unable to cleanup stale tmpdir", "path", d, "error", err)
}
err = os.RemoveAll(d)
if err != nil {
slog.Debug("unable to cleanup stale tmpdir", "path", d, "error", err)
} }
} }
} }