From 23e899f32d9f7b3bbe0b902a95c23be5a1254409 Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Thu, 20 Jun 2024 08:51:35 -0700 Subject: [PATCH 1/3] skip os.removeAll() if PID does not exist --- gpu/assets.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gpu/assets.go b/gpu/assets.go index f2adcf3e..fdb3dd81 100644 --- a/gpu/assets.go +++ b/gpu/assets.go @@ -87,6 +87,8 @@ func cleanupTmpDirs() { } } else { slog.Debug("failed to open ollama.pid", "path", d, "error", err) + // No pid, ignore this tmpdir + continue } err = os.RemoveAll(d) if err != nil { From 4ebb66c6623d85f4fb69db0406ddd05bdc2d893d Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Thu, 20 Jun 2024 09:23:43 -0700 Subject: [PATCH 2/3] reformat error check --- gpu/assets.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/gpu/assets.go b/gpu/assets.go index fdb3dd81..e2abfd58 100644 --- a/gpu/assets.go +++ b/gpu/assets.go @@ -77,19 +77,20 @@ func cleanupTmpDirs() { continue } raw, err := os.ReadFile(filepath.Join(d, "ollama.pid")) - if err == nil { - pid, err := strconv.Atoi(string(raw)) - if err == nil { - if proc, err := os.FindProcess(pid); err == nil && !errors.Is(proc.Signal(syscall.Signal(0)), os.ErrProcessDone) { - // Another running ollama, ignore this tmpdir - continue - } - } - } else { - slog.Debug("failed to open ollama.pid", "path", d, "error", err) + 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)) + if err == nil { + if proc, err := os.FindProcess(pid); err == nil && !errors.Is(proc.Signal(syscall.Signal(0)), os.ErrProcessDone) { + // Another running ollama, ignore this tmpdir + continue + } + } + err = os.RemoveAll(d) if err != nil { slog.Debug("unable to cleanup stale tmpdir", "path", d, "error", err) From 662568d453debcf77d2e077ef98cfb2cfab8575e Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Thu, 20 Jun 2024 09:30:59 -0700 Subject: [PATCH 3/3] err!=nil check --- gpu/assets.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/gpu/assets.go b/gpu/assets.go index e2abfd58..073d2e81 100644 --- a/gpu/assets.go +++ b/gpu/assets.go @@ -84,16 +84,20 @@ func cleanupTmpDirs() { } pid, err := strconv.Atoi(string(raw)) - if err == nil { - if proc, err := os.FindProcess(pid); err == nil && !errors.Is(proc.Signal(syscall.Signal(0)), os.ErrProcessDone) { - // Another running ollama, ignore this tmpdir - continue - } + if err != nil { + slog.Warn("failed to parse pid", "path", d, "error", err) + continue } - err = os.RemoveAll(d) - if err != nil { - slog.Debug("unable to cleanup stale tmpdir", "path", d, "error", err) + 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 + continue + } + + if err := os.Remove(d); err != nil { + slog.Warn("unable to cleanup stale tmpdir", "path", d, "error", err) } } }