[PR #15469] fix: check dst.Write error in transfer downloader copy() to prevent silent data corruption #25705

Open
opened 2026-04-19 18:22:01 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/15469
Author: @kuishou68
Created: 4/10/2026
Status: 🔄 Open

Base: mainHead: fix-transfer-copy-write-error


📝 Commits (1)

  • 5a124a4 fix: check dst.Write error in transfer downloader copy() to prevent silent data corruption

📊 Changes

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

View changed files

📝 x/imagegen/transfer/download.go (+7 -1)

📄 Description

Problem

In x/imagegen/transfer/download.go, the copy() function ignores the error returned by dst.Write():

nr, err := src.Read(buf)
if nr > 0 {
    lastRead.Store(time.Now().UnixNano())
    dst.Write(buf[:nr])   // ← error silently discarded!
    h.Write(buf[:nr])
    d.progress.add(int64(nr))
    n += int64(nr)
}

This causes silent data corruption:

  1. If the disk is full or an I/O error occurs during download, writes fail silently
  2. The SHA256 hash is computed over bytes received from the network (not bytes written to disk)
  3. The final hash check passes — the download reports success
  4. But the file on disk is incomplete or corrupt

Fix

Check the error and byte count from dst.Write() and return immediately on failure:

nw, werr := dst.Write(buf[:nr])
if werr != nil {
    return n, werr
}
if nw != nr {
    return n, io.ErrShortWrite
}

This follows the standard Go pattern for detecting short writes (as used in io.Copy in the standard library).

Testing

The fix ensures:

  • Disk-full scenarios cause download failure (not silent corruption)
  • I/O errors propagate up and trigger the retry logic in download()
  • Existing tests continue to pass (no behavior change under normal conditions)

Closes #15468


🔄 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/15469 **Author:** [@kuishou68](https://github.com/kuishou68) **Created:** 4/10/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix-transfer-copy-write-error` --- ### 📝 Commits (1) - [`5a124a4`](https://github.com/ollama/ollama/commit/5a124a4a45116207c7278192d613b197c8df77f7) fix: check dst.Write error in transfer downloader copy() to prevent silent data corruption ### 📊 Changes **1 file changed** (+7 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `x/imagegen/transfer/download.go` (+7 -1) </details> ### 📄 Description ## Problem In `x/imagegen/transfer/download.go`, the `copy()` function ignores the error returned by `dst.Write()`: ```go nr, err := src.Read(buf) if nr > 0 { lastRead.Store(time.Now().UnixNano()) dst.Write(buf[:nr]) // ← error silently discarded! h.Write(buf[:nr]) d.progress.add(int64(nr)) n += int64(nr) } ``` This causes **silent data corruption**: 1. If the disk is full or an I/O error occurs during download, writes fail silently 2. The SHA256 hash is computed over bytes **received from the network** (not bytes **written to disk**) 3. The final hash check passes — the download reports success 4. But the file on disk is incomplete or corrupt ## Fix Check the error and byte count from `dst.Write()` and return immediately on failure: ```go nw, werr := dst.Write(buf[:nr]) if werr != nil { return n, werr } if nw != nr { return n, io.ErrShortWrite } ``` This follows the standard Go pattern for detecting short writes (as used in `io.Copy` in the standard library). ## Testing The fix ensures: - Disk-full scenarios cause download failure (not silent corruption) - I/O errors propagate up and trigger the retry logic in `download()` - Existing tests continue to pass (no behavior change under normal conditions) Closes #15468 --- <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-19 18:22:01 -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#25705