Blog

A reader longer than the size it was given: PutObject in minio-go

On this page
  1. When it happens
  2. Why the gap existed
  3. The fix
  4. The test
  5. What review changed
  6. What it does not change
  7. The lesson

minio-go’s PutObject took a declared size and a reader that held more bytes than that, and gave a different result on each path that sends a stream: an error with no error code, the last part sent 10 times, or a call that succeeded. I sent the fix (PR #2334, merged 2026-11-23). It refers to issue #2331 but does not close it; the issue is still open.

When it happens

The issue’s example uses ConcurrentStreamParts:

r := bytes.NewReader(make([]byte, 12<<20+1))
info, err := client.PutObject(ctx, "bucket", "object", r, 12<<20, minio.PutObjectOptions{
	PartSize:              5 << 20,
	ConcurrentStreamParts: true,
	NumThreads:            3,
})
// err == nil, info.Size == 12582913

The caller said 12 MiB and the call completed with one byte more, and no error. A “plain reader” below is a bytes.Reader wrapped in struct{ io.Reader }, so it has no ReadAt or Seek. The declared size is 4 MiB for a single PUT and 12 MiB for multipart, with 5 MiB parts; the reader is one byte too long.

  • Single PUT, plain reader. The call failed with http: ContentLength=4194304 with Body length 4194305, which has no error code. Over HTTPS, net/http had already sent all 4194304 bytes, so the server held a complete body. With SendContentMd5 the extra byte was ignored and the call succeeded with Size=4194304.
  • Serial multipart stream. The last part failed with http: ContentLength=2097152 with Body length 2097153 and was sent 10 times, 12 PUT requests in all. When the size was a multiple of the part size (10 MiB declared, 10 MiB + 1 in the reader), the extra byte was never read and the call completed 2 parts.
  • Parallel multipart stream, 3 threads. Extra bytes that fit in the last part’s buffer were uploaded: 12 MiB + 1 completed with Size=12582913. A reader that ran past the last part failed with EntityTooLarge from errUploadTooLarge, whose message tells the caller to set PutObjectOptions.PartSize. That does not help: the part layout is exact when the size is known, and the reader is longer than the size it was given.

Why the gap existed

The single PUT hands its reader to net/http, which checks the content length and returns its own error inside a *url.Error. ToErrorResponse does not unwrap that, so the caller got no code. The serial and parallel loops read each part into a buffer sized for a full part, not for what was left of the declared size, and neither checked for a byte after the declared end. PR #2330 passed the size to the parallel path for the short-reader case and left the long case alone.

The fix

A new error, errReaderTooLong, has code EntityTooLarge and the message Input stream holds more than the size ‘N’ given for it. A helper reads one more byte:

func errIfLonger(reader io.Reader, tooLong error) error {
	var b [1]byte
	n, err := readFull(reader, b[:])
	if n > 0 {
		return tooLong
	}
	if err != nil && err != io.EOF {
		return err
	}
	return nil
}

Both multipart paths now read no more than what is left of the declared size, and call errIfLonger after the last part is read, before it is sent. The upload is aborted and nothing is completed. The message reports the declared size whatever the surplus.

A plain single PUT gets a wrapper. A reader that cannot seek is wrapped in a sizeLimitReader, which fails the read that would complete the body when more data follows, so the server never gets a complete body. putObject then returns errReaderTooLong instead of net/http’s error. With SendContentMd5 the body is read first and checked before any request: none is sent.

A read error other than io.EOF in place of that extra byte also fails the call. Before, a reader that failed at its end completed a single PUT with SendContentMd5.

The test

TestPutObjectMultipartStreamParallelShortRead and TestPutObjectMultipartStreamShortRead gained rows for a longer reader and for a read error at the end. Each failing row checks the error, that the abort was sent and that nothing was completed; the longer-reader rows check the new message too. TestPutObjectSingleLongerReader is new, with six rows at 1 MiB declared and a TLS server, because over plain HTTP the streaming signature hides whether the last chunk was sent. Each row checks that no request carried a complete body. TestErrReaderTooLong checks the fields of the new error. Per the PR description, every new row fails on master except the plain “read error at the end” row in each multipart test.

What review changed

My first commit returned errUnexpectedEOF for a longer reader. @klauspost commented: “Wouldn’t errUploadTooLarge be more appropriate? I would expect errUnexpectedEOF to be from short inputs.” I agreed on the code, but not on reusing errUploadTooLarge itself, because its message is about the part limit. The next commit added errReaderTooLong with the same EntityTooLarge code. On the issue, the same maintainer had written that “a consistent error message should be returned” in place of a silent truncate.

The review bot asked for changes twice, both about putObject replacing a reader’s own error. A reader error that wrapped an EntityTooLarge response came back as the inner response, and errors.Is no longer matched it. Then a reader error that wrapped a response equal to errReaderTooLong lost its wrapper the same way. The fix is a *sizeLimitError that putObject creates and matches by pointer, with a new row for each case. @klauspost and the review bot approved, and @harshavardhana merged it.

What it does not change

A reader with ReadAt and Seek (*os.File, bytes.Reader) still sends its first size bytes and succeeds when it is longer, in a single PUT and in the ReadAt multipart path: 12 MiB declared with a 12 MiB + 1 reader completes with Size=12582912. A declared size of 0 and the unknown-size path are unchanged. The PR text offers to extend the error to the seekable paths and notes it would break calls that work now; it has no reply on that. One cost: a single PUT with SendContentMd5, and a serial upload that ends on a part boundary, now wait for the extra byte, so a source that stays open without sending more holds the call.

The lesson

  • A declared size binds both ways. A reader that stops early already got an error; one that runs long should get one too, with its own code.
  • One call gave different outcomes by reader type and options. Test each path with the same input.
  • Match a locally made error by identity, not by value. An equal value from the reader looked the same to errors.Is.

If a reader of yours can hold more bytes than the size you pass to an upload call, reply on X.