[PR #14981] fix: close temp file on Import error paths #61648

Open
opened 2026-04-29 16:42:37 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14981
Author: @buley
Created: 3/20/2026
Status: 🔄 Open

Base: mainHead: fix/close-temp-file-on-import-error


📝 Commits (1)

  • 252364a fix: close temp file on Import error paths

📊 Changes

1 file changed (+1 additions, -0 deletions)

View changed files

📝 server/internal/cache/blob/cache.go (+1 -0)

📄 Description

Summary

In server/internal/cache/blob/cache.go, DiskCache.Import() creates a temp file with os.CreateTemp but if io.Copy fails or the size check fails, the function returns the error without closing the file handle. The defer os.Remove(f.Name()) attempts cleanup of the file on disk, but the fd itself leaks.

Fix

Added defer f.Close() immediately after the os.CreateTemp call. This ensures the fd is closed on all exit paths. The explicit f.Close() on the happy path (line 244) becomes a harmless no-op since *os.File.Close is safe to call multiple times.

How to reproduce

// Pass a reader that fails partway through:
r := io.LimitReader(bytes.NewReader(data), len(data)/2)
cache.Import(iotest.ErrReader(r), int64(len(data)))
// The temp file fd is leaked because f is never closed on the io.Copy error path

AI Disclosure

This bug was found using static analysis tooling with AI assistance (pattern matching for os.CreateTemp without defer Close on error paths). The one-line fix was verified by reading the surrounding code.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/14981 **Author:** [@buley](https://github.com/buley) **Created:** 3/20/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/close-temp-file-on-import-error` --- ### 📝 Commits (1) - [`252364a`](https://github.com/ollama/ollama/commit/252364a15b8049873a0b4f92e7d0eee2b75c4646) fix: close temp file on Import error paths ### 📊 Changes **1 file changed** (+1 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `server/internal/cache/blob/cache.go` (+1 -0) </details> ### 📄 Description ## Summary In `server/internal/cache/blob/cache.go`, `DiskCache.Import()` creates a temp file with `os.CreateTemp` but if `io.Copy` fails or the size check fails, the function returns the error without closing the file handle. The `defer os.Remove(f.Name())` attempts cleanup of the file on disk, but the fd itself leaks. ## Fix Added `defer f.Close()` immediately after the `os.CreateTemp` call. This ensures the fd is closed on all exit paths. The explicit `f.Close()` on the happy path (line 244) becomes a harmless no-op since `*os.File.Close` is safe to call multiple times. ## How to reproduce ```go // Pass a reader that fails partway through: r := io.LimitReader(bytes.NewReader(data), len(data)/2) cache.Import(iotest.ErrReader(r), int64(len(data))) // The temp file fd is leaked because f is never closed on the io.Copy error path ``` ## AI Disclosure This bug was found using static analysis tooling with AI assistance (pattern matching for `os.CreateTemp` without `defer Close` on error paths). The one-line fix was verified by reading the surrounding code. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-29 16:42:37 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#61648