Skip to content

File.ReadFrom returns nil when writing the last, partial chunk fails #668

Closed

Description

@christhomas

File.ReadFrom reports success when the server refuses the write of the final chunk, if that chunk is shorter than a full packet. With concurrent writes off (the default), that is the last write of nearly every upload, so an upload whose end the server refused, such as on a full disk, comes back as complete.

Versions: v1.13.11, and master at 16147c5.

Cause: in the sequential loop of File.ReadFrom (client.go):

n, err := io.ReadFull(r, b)
...
if n > 0 {
	read += int64(n)
	m, err2 := f.writeChunkAt(ch, b[:n], f.offset)
	f.offset += int64(m)
	if err == nil {
		err = err2
	}
}
if err != nil {
	if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
		return read, nil // return nil explicitly.
	}
	return read, err
}

When the source ends part-way through the buffer, io.ReadFull returns io.ErrUnexpectedEOF along with the final bytes. That error is already in err, so the write error in err2 is not kept, and the io.ErrUnexpectedEOF is then turned into a nil error.

Reproduction: a request server whose writes all fail. ReadFrom reports success; Write on the same file reports the failure.

package main

import (
	"errors"
	"fmt"
	"io"
	"os"
	"strings"

	"github.com/pkg/sftp"
)

// failingWrites is a FileWriter whose every write fails, as a full disk would.
type failingWrites struct{}

func (failingWrites) Filewrite(*sftp.Request) (io.WriterAt, error) { return failingWriterAt{}, nil }

type failingWriterAt struct{}

func (failingWriterAt) WriteAt([]byte, int64) (int, error) { return 0, errors.New("disk full") }

type pipe struct {
	io.Reader
	io.WriteCloser
}

func main() {
	mem := sftp.InMemHandler()
	handlers := sftp.Handlers{FileGet: mem.FileGet, FilePut: failingWrites{}, FileCmd: mem.FileCmd, FileList: mem.FileList}
	clientReads, serverWrites := io.Pipe()
	serverReads, clientWrites := io.Pipe()
	go sftp.NewRequestServer(pipe{serverReads, serverWrites}, handlers).Serve()

	client, err := sftp.NewClientPipe(clientReads, clientWrites)
	if err != nil {
		panic(err)
	}
	f, err := client.OpenFile("/f", os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
	if err != nil {
		panic(err)
	}

	n, err := f.ReadFrom(strings.NewReader("hello"))
	fmt.Println("ReadFrom:", n, err)

	m, err := f.Write([]byte("hello"))
	fmt.Println("Write:   ", m, err)
}

Output, on both versions:

ReadFrom: 5 <nil>
Write:    0 sftp: "disk full" (SSH_FX_FAILURE)

Possible fix: return the write error before looking at the read error, for example:

m, err2 := f.writeChunkAt(ch, b[:n], f.offset)
f.offset += int64(m)
if err2 != nil {
	return read, err2
}

io.Copy into a *sftp.File goes through ReadFrom as well, so it is affected the same way. Wrapping the file so that only Write is visible avoids the problem in the meantime.

Activity

  1. puellanivis commented on Oct 14, 2026

    @puellanivis
    Collaborator

    🤔 I think the fix is to add if err == nil || err == io.ErrUnexpectedEOF? We want to prioritize err over err2, but like you said, if we need to override an unexpected EOF because that gets turned into a nil, we should then just override that specific error?

  2. added a commit that references this issue on Oct 19, 2026
    8634d51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions