GeezerVison is showing github.com — IoUring: Allocate the full chunk buffer for the generic FileRegion fallback by LuciferYang · Pull Request #16830 · netty/netty
Conversation
LuciferYang
commented
May 19, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes #16829.
Motivation:
The generic FileRegion fallback added in #16571 sizes its chunk buffer to min(region.count() - region.transferred(), FILE_REGION_MAX_CHUNK_SIZE). That sizing treats count() as the upper bound on what transferTo() will emit. DefaultFileRegion respects that, but custom FileRegions don't have to: an on-the-fly encryption or framing layer typically reports the plain source size via count() while transferTo() writes a larger ciphertext or framed payload. When that happens the chunk buffer is too small, the per-call output gets truncated, and once the region advances transferred() to count() the completion handler removes it from the outbound buffer with bytes still buffered inside. The peer sees a partial frame.
Modification:
- Always allocate the full
FILE_REGION_MAX_CHUNK_SIZE in the generic-FileRegion path.
- Widen the surrounding
catch (Exception) to catch (Throwable) so an OutOfDirectMemoryError from directBuffer(...), or any Error from transferTo(), still releases the buffer and fails the write future instead of leaking.
- Pull
testFileRegionEmittingMoreThanCount and its EmitMoreThanCountFileRegion fixture into SocketFileRegionTest so the regression runs against every FileRegion-supporting transport.
- Add
SocketFileRegionTest#maxFileRegionPerCallEmit() for transports that copy through an intermediate buffer to declare their per-call cap. Both io_uring subclasses override it to the configured chunk size, which lets the test skip when an operator has shrunk the chunk below the test payload.
- Note the per-call cap on the
FILE_REGION_MAX_CHUNK_SIZE Javadoc.
Result:
Custom FileRegions whose per-call output exceeds count() are no longer truncated, provided that output still fits in FILE_REGION_MAX_CHUNK_SIZE (default 64 KiB, tunable via io.netty.iouring.fileRegionChunkSize). The new regression test exercises the contract on every FileRegion-supporting transport.
Trade-off: every generic-FileRegion send now reserves the full chunk up front, even for tiny regions. The buffer is per in-flight write and released on completion, so the steady-state cost is bounded. Pooled and adaptive allocators recycle it; UnpooledByteBufAllocator pays a fresh native alloc/free per send.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…llback
Motivation:
The generic FileRegion fallback added in netty#16571 sizes its chunk buffer
to `min(region.count() - region.transferred(), FILE_REGION_MAX_CHUNK_SIZE)`.
That sizing treats count() as an upper bound on the bytes transferTo()
will emit -- which holds for DefaultFileRegion but custom FileRegion
implementations may violate it. An on-the-fly encryption or framing
layer, for example, exposes count() as the source size while emitting
larger output through transferTo() (ciphertext, per-chunk framing,
etc.). With a too-small chunk buffer the region's output is truncated;
if the implementation then advances transferred() to count() once its
source is drained, the completion handler removes the entry from the
outbound buffer while the rest of the bytes are still sitting inside
the region. The peer sees a partial frame and the connection stalls.
Modification:
Allocate the full FILE_REGION_MAX_CHUNK_SIZE unconditionally.
Widen the chunk-allocation try/catch from Exception to Throwable so an
OutOfDirectMemoryError from alloc().directBuffer(...) -- or any Error
from transferTo() -- still releases the chunk buffer (when already
allocated) and surfaces on the write future instead of leaking and
stalling the channel.
Add SocketFileRegionTest#testFileRegionEmittingMoreThanCount plus an
EmitMoreThanCountFileRegion fixture that reports count = 5 while
transferTo() emits 20 bytes in one call. The test lives on the base
class so every FileRegion-supporting transport (NIO, epoll, kqueue,
io_uring) must satisfy the same invariant. With the fix applied the
receiver observes all 20 bytes; against unpatched io_uring code the
transport's 5-byte chunk buffer accepts only 5 bytes of the emitted
chunk and the fixture's fail-fast short-write check throws IOException,
which surfaces on the write future and fails the test.
Introduce SocketFileRegionTest#maxFileRegionPerCallEmit() (default
Long.MAX_VALUE) so transports that copy through an intermediate buffer
can declare their per-call output cap. The two io_uring subclasses
override it to CONFIGURED_CHUNK_SIZE so the test skips cleanly when an
operator has shrunk the chunk size below the region's per-call output.
Document the per-call-output cap on the FILE_REGION_MAX_CHUNK_SIZE
field so operators tuning the system property know not to size it
below the largest emit any FileRegion implementation can produce.
Result:
io_uring's generic FileRegion path no longer truncates the per-call
output of custom FileRegion implementations whose actual emit size
exceeds count(), so long as the per-call output fits in
FILE_REGION_MAX_CHUNK_SIZE (default 64 KiB, tunable via the
io.netty.iouring.fileRegionChunkSize system property). The new
regression test is shared across all FileRegion-supporting transports.
Memory cost: every generic-FileRegion send now reserves the full chunk
size up front even for tiny regions. The buffer is per in-flight write
and is released when the write completes; with the default
pooled/adaptive allocator it is recycled, while
UnpooledByteBufAllocator pays a fresh native allocation/free per send.
|
@LuciferYang honestly I am not sure this is a good idea in practice and might result in "huge" allocations even for tiny stuff.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Fair point @normanmaurer. The motivating case is Spark's SaslEncryption.EncryptedMessage, whose own Javadoc admits it returns "technically wrong data" for count() — it slipped through on NIO/epoll/kqueue only because those transports pass the channel straight to the socket and never see the size mismatch. That's a Spark bug, not a contract Netty should accommodate.
#16826 already covers the drain-loop overshoot defensively. I'll fix SaslEncryption on the Spark side so count() matches the wire size, then close this PR.
|
normanmaurer reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|