Skip to content

AWS: Fix zero-length S3 tail reads - #18329

Merged
danielcweeks merged 7 commits into
apache:mainfrom
AlejandroMorgante:fix/s3-zero-length-range-reads
Oct 29, 2026
Merged

danielcweeks merged 7 commits into
apache:mainfrom
AlejandroMorgante:fix/s3-zero-length-range-reads

Conversation

@AlejandroMorgante

@AlejandroMorgante AlejandroMorgante commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

S3InputStream.readTail(..., 0) constructs Range: bytes=-0, which fails with InvalidRange. Return 0 immediately after buffer validation when the requested length is zero, without changing the buffer or issuing an S3 request. readFully and nonzero tail reads retain their existing behavior.

Test plan: verify the zero return value, unchanged buffer, and absence of S3 calls; cover empty, nonempty, and missing objects in integration tests, and retain stream-closure coverage for nonzero reads. The S3InputStream unit and integration test classes, spotlessApply, AWS checkstyleMain, and checkstyleTest passed with Java 17.

Validated commit 653c5ce7b against real Amazon S3: zero-length tail reads returned 0 without changing the buffer for 0-byte, 1-byte, and 5-MiB objects and a missing object. Nonzero readTail and readFully reads returned the expected data; a nonzero tail read of the missing object propagated NoSuchKeyException. The temporary bucket and objects were deleted.


AI Disclosure

  • Model: GPT-6
  • Platform/Tool: Codex
  • Human Oversight: fully reviewed
  • Prompt Summary: Simplify zero-length tail reads to return immediately while leaving readFully unchanged; the implementation, adjusted tests, and PR description are AI-assisted.

Skip empty positional and tail reads after validating the buffer so S3 never receives invalid or unsatisfiable ranges.

Generated-by: Codex (GPT-6)
@AlejandroMorgante

Copy link
Copy Markdown
Contributor Author

@pingedbrain, could you please review it when you have some time?

@github-actions github-actions Bot added the AWS label Sep 30, 2026
The early return guarantees a positive read length before read metrics are updated.

Generated-by: Codex (GPT-6)
@pingedbrain

Copy link
Copy Markdown

Would it be worth adding a small RustFS integration test for this case?

I ran this locally against RustFS 1.0.0 with a missing object:

readFully(0, buffer, 1, 0)
readTail(buffer, 1, 0)

On main, the first call sends bytes=0--1 and fails with S3Exception.
On this branch, both calls return successfully without an S3 request.

The unit test covers the mocked client; an integration test would keep this provider-level regression covered.

@AlejandroMorgante
AlejandroMorgante marked this pull request as ready for review September 30, 2026 17:26
@danielcweeks

Copy link
Copy Markdown
Contributor

@AlejandroMorgante can you explain what motivated this PR? This is a little surprising because we're changing the contract for readers under the impression that there's an optimization to be had here. Was this something that was just identified or is there a practical case that this impacts?

@AlejandroMorgante

AlejandroMorgante commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor Author

@danielcweeks

Our processes use Iceberg’s RangeReadable interface with dynamically computed ranges. During testing, a computed tail length was zero. readTail(..., 0) then sent Range: bytes=-0 to S3 and failed with S3Exception. That practical failure motivated this PR

I checked readFully(..., 0) as the same boundary case and found that it also made a GetObject request despite having no bytes to copy. The change makes zero-length reads return after buffer validation. Nonzero reads are unchanged.

Avoiding an unnecessary S3 GetObject request for an empty read is an additional benefit


I think zero-length reads should be no-ops: readFully and readTail read bytes, while checking whether an object exists is a separate operation provided by InputFile.exists(). InputFile

Generated-by: Codex (GPT-6)
@danielcweeks

Copy link
Copy Markdown
Contributor

My higher-level concern is that we're making an optimization that subtly changes the contract behavior. We bypass permissions checks and the underlying SDK doesn't do that, which is why we see the zero byte request pass through.

I don't feel like this would cause problems, but at the same time I feel like it's a narrow optimization case that makes things a little more complicated.

It sounds like the original problem was that we produced an invalid object range. Alternatively, we could maintain the full IO contract at just fix that issue.

@AlejandroMorgante

AlejandroMorgante commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor Author

@danielcweeks

I agree that returning early bypasses the existence and permission checks performed by S3. However, from a caller’s perspective, it seems surprising for a zero-length read to fail with an S3Exception because of the range constructed internally. I couldn’t find a valid Range for requesting zero bytes, and an un-ranged GetObject could consume the whole response body just to discard it. I think a zero-length read should return without an S3 request. What do you think the expected contract should be?

@AlejandroMorgante
AlejandroMorgante marked this pull request as draft September 30, 2026 22:19
@AlejandroMorgante
AlejandroMorgante marked this pull request as ready for review September 30, 2026 22:19
Limit the fix to a valid tail range while retaining GetObject validation and restoring readFully behavior.

Generated-by: Codex
@AlejandroMorgante AlejandroMorgante changed the title AWS: Avoid S3 requests for zero-length range reads AWS: Fix zero-length S3 tail ranges while preserving I/O checks Oct 1, 2026
@AlejandroMorgante

AlejandroMorgante commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor Author

@danielcweeks I updated the PR to preserve the I/O behavior you pointed out. For readTail(..., 0), we now request bytes=-1 instead of bytes=-0, while copying zero bytes into the caller’s buffer and returning 0. The GetObject request and stream closure are preserved. I also reverted the changes to readFully.

Behavior of readTail(buffer, offset, 0):

Scenario Before this PR After this PR
Empty object S3Exception (InvalidRange, HTTP 416) Returns 0; buffer remains unchanged
Nonempty object S3Exception (InvalidRange, HTTP 416) Returns 0; buffer remains unchanged
Missing object Propagates the S3 error Propagates the S3 error
Permission failure Propagates the S3 error Propagates the S3 error

I validated the original bytes=-0 request against real Amazon S3 with empty and 1-byte objects: both returned InvalidRange (HTTP 416). With this revision, zero-length tail reads returned 0 for objects of 0 bytes, 1 byte, and 5 MiB, without modifying the buffer. A missing object correctly raised NoSuchKeyException. All four tests of the revised implementation passed against real S3, and the temporary objects were deleted afterward.

RustFS integration tests also passed for empty, nonempty, and missing objects. Permission-error propagation also passed against real S3: a temporary STS session denying GetObject produced 403 AccessDenied for empty and 1-byte objects, both with the original request and the revised readTail(..., 0). The buffer remained unchanged, and the temporary objects were deleted. The AWS module build and Checkstyle passed.

What do you think of this approach? Does it address your concern about preserving the I/O contract?

@AlejandroMorgante
AlejandroMorgante marked this pull request as draft October 2, 2026 21:36
@AlejandroMorgante
AlejandroMorgante marked this pull request as ready for review October 2, 2026 21:36
Generated-by: Codex
@danielcweeks

Copy link
Copy Markdown
Contributor

@AlejandroMorgante sorry this fell of my radar. Unfortunately, I don't like the 1-byte workaround since that changes the read contract in a strange way. If we can't issue a zero byte read, then I think I would prefer just returning (it's the lesser of two evils here).

However, I wouldn't change the other paths since we're just delegating to the SDK.

Avoid invalid zero-length suffix ranges without requesting an extra byte. Leave readFully behavior unchanged.

Generated-by: Codex
@AlejandroMorgante

Copy link
Copy Markdown
Contributor Author

@danielcweeks Thanks! Updated readTail(..., 0) to return immediately after buffer validation, leaving readFully unchanged. Adjusted the tests accordingly; unit and integration tests pass.

@AlejandroMorgante AlejandroMorgante changed the title AWS: Fix zero-length S3 tail ranges while preserving I/O checks AWS: Fix zero-length S3 tail reads Oct 23, 2026

@danielcweeks danielcweeks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AlejandroMorgante, this is a much narrower and cleaner fix!

@danielcweeks
danielcweeks merged commit 2ebe838 into apache:main Oct 29, 2026
44 of 45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants