Repository: ollama/ollama
File: gpu/assets.go (Go)

Code before:
			return "", err
		}

		// We create a distinct subdirectory for payloads within the tmpdir
		// This will typically look like /tmp/ollama3208993108/runners on linux
		payloadsDir = filepath.Join(tmpDir, "runners")
	}
	return payloadsDir, nil
}

// Best effort to clean up prior tmpdirs
func cleanupTmpDirs() {
	dirs, err := filepath.Glob(filepath.Join(os.TempDir(), "ollama*"))
	if err != nil {
		return
	}
	for _, d := range dirs {
		info, err := os.Stat(d)
		if err != nil || !info.IsDir() {
			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)
			// No pid, ignore this tmpdir
			continue
		}
		err = os.RemoveAll(d)
		if err != nil {
			slog.Debug("unable to cleanup stale tmpdir", "path", d, "error", err)
		}
	}
}

func Cleanup() {
	lock.Lock()
	defer lock.Unlock()
	runnersDir := envconfig.RunnersDir
	if payloadsDir != "" && runnersDir == "" && runtime.GOOS != "windows" {
		// We want to fully clean up the tmpdir parent of the payloads dir
		tmpDir := filepath.Clean(filepath.Join(payloadsDir, ".."))
		slog.Debug("cleaning up", "dir", tmpDir)
		err := os.RemoveAll(tmpDir)

Review comment (suggestion):
It'll be cleaner if you invert the if

```go
raw, err := os.ReadFile(...)
if err != nil {
    slog.Warn("...")
    continue
}

pid, err := strconv.Atoi(string(raw))
if err != nil {
    slog.Warn("...")
    continue
}

proc, err := os.FindProcess(pid)
if err != nil {
    slog.Warn("...")
    continue
}

if err := os.Remove(d); err != nil {
    slog.Warn("...")
}
```

Code after:
			return "", err
		}

		// We create a distinct subdirectory for payloads within the tmpdir
		// This will typically look like /tmp/ollama3208993108/runners on linux
		payloadsDir = filepath.Join(tmpDir, "runners")
	}
	return payloadsDir, nil
}

// Best effort to clean up prior tmpdirs
func cleanupTmpDirs() {
	dirs, err := filepath.Glob(filepath.Join(os.TempDir(), "ollama*"))
	if err != nil {
		return
	}
	for _, d := range dirs {
		info, err := os.Stat(d)
		if err != nil || !info.IsDir() {
			continue
		}
		raw, err := os.ReadFile(filepath.Join(d, "ollama.pid"))
		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 {
			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
			continue
		}

		if err := os.Remove(d); err != nil {
			slog.Warn("unable to cleanup stale tmpdir", "path", d, "error", err)
		}
	}
}

func Cleanup() {
	lock.Lock()
	defer lock.Unlock()
	runnersDir := envconfig.RunnersDir