[PR #14710] server: fix response body leak in upload retry loop #61499

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

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14710
Author: @gambletan
Created: 3/8/2026
Status: 🔄 Open

Base: mainHead: fix/upload-response-body-leak


📝 Commits (1)

  • e706581 server: fix response body leak in upload retry loop

📊 Changes

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

View changed files

📝 server/upload.go (+1 -1)

📄 Description

What

Fix a response body resource leak in blobUpload.Run().

Why

The defer resp.Body.Close() at line 210 is inside a for loop (the retry loop at line 199). In Go, defer is scoped to the enclosing function, not the loop iteration, so the response body is not closed until Run() returns. If retries occur, multiple response bodies accumulate without being closed, leaking HTTP connections.

Since this is a finalization PUT request with Content-Length: 0, the response body only needs to be drained and closed. Closing it immediately (without defer) is correct and prevents the leak.

Change

Replace defer resp.Body.Close() with resp.Body.Close() since the function breaks out of the loop immediately after.


🔄 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/14710 **Author:** [@gambletan](https://github.com/gambletan) **Created:** 3/8/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/upload-response-body-leak` --- ### 📝 Commits (1) - [`e706581`](https://github.com/ollama/ollama/commit/e706581f6f375abe282760031b4c97da1d50e9f1) server: fix response body leak in upload retry loop ### 📊 Changes **1 file changed** (+1 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `server/upload.go` (+1 -1) </details> ### 📄 Description ## What Fix a response body resource leak in `blobUpload.Run()`. ## Why The `defer resp.Body.Close()` at line 210 is inside a `for` loop (the retry loop at line 199). In Go, `defer` is scoped to the enclosing function, not the loop iteration, so the response body is not closed until `Run()` returns. If retries occur, multiple response bodies accumulate without being closed, leaking HTTP connections. Since this is a finalization PUT request with `Content-Length: 0`, the response body only needs to be drained and closed. Closing it immediately (without `defer`) is correct and prevents the leak. ## Change Replace `defer resp.Body.Close()` with `resp.Body.Close()` since the function breaks out of the loop immediately after. --- <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:35:35 -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#61499