GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — IoUring: Stop generic FileRegion drain loop when transferred() reache… · netty/netty@9e19320

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • testsuite/src/main/java/io/netty/testsuite/transport/socket
    • SocketFileRegionTest.java
  • transport-classes-io_uring/src/main/java/io/netty/channel/uring
    • AbstractIoUringStreamChannel.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketFileRegionTest.java‎

Copy file name to clipboardExpand all lines: testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketFileRegionTest.java
+183Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,31 @@
2626
import io.netty.channel.DefaultFileRegion;
2727
import io.netty.channel.FileRegion;
2828
import io.netty.channel.SimpleChannelInboundHandler;
29+
import io.netty.channel.socket.oio.OioSocketChannel;
30+
import io.netty.util.AbstractReferenceCounted;
31+
import io.netty.util.internal.ObjectUtil;
2932
import io.netty.util.internal.PlatformDependent;
3033
import org.junit.jupiter.api.Test;
3134
import org.junit.jupiter.api.TestInfo;
35+
import org.junit.jupiter.api.Timeout;
3236

3337
import java.io.File;
3438
import java.io.FileOutputStream;
3539
import java.io.IOException;
3640
import java.io.RandomAccessFile;
41+
import java.nio.ByteBuffer;
3742
import java.nio.channels.WritableByteChannel;
3843
import java.util.Random;
3944
import java.util.concurrent.ThreadLocalRandom;
45+
import java.util.concurrent.TimeUnit;
46+
import java.util.concurrent.atomic.AtomicInteger;
4047
import java.util.concurrent.atomic.AtomicReference;
4148

4249
import static io.netty.testsuite.transport.TestsuitePermutation.randomBufferType;
4350
import static org.junit.jupiter.api.Assertions.assertEquals;
4451
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
4552
import static org.junit.jupiter.api.Assertions.assertNotEquals;
53+
import static org.junit.jupiter.api.Assumptions.assumeFalse;
4654
import static org.junit.jupiter.api.Assumptions.assumeTrue;
4755

4856
public class SocketFileRegionTest extends AbstractSocketTest {
@@ -118,6 +126,18 @@ public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Thr
118126
});
119127
}
120128

129+
@Test
130+
@Timeout(value = 30, unit = TimeUnit.SECONDS)
131+
public void testFileRegionDrainStopsAtCompletion(TestInfo testInfo) throws Throwable {
132+
assumeTrue(supportsCustomFileRegion());
133+
run(testInfo, new Runner<ServerBootstrap, Bootstrap>() {
134+
@Override
135+
public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable {
136+
testFileRegionDrainStopsAtCompletion(serverBootstrap, bootstrap);
137+
}
138+
});
139+
}
140+
121141
public void testFileRegion(ServerBootstrap sb, Bootstrap cb) throws Throwable {
122142
testFileRegion0(sb, cb, false, true, true);
123143
}
@@ -166,6 +186,64 @@ protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
166186
sc.close().sync();
167187
}
168188

189+
/**
190+
* Reproducer for a {@code FileRegion} drain-loop overshoot. Transports must short-circuit
191+
* as soon as the region reports {@code transferred() >= count()}; any extra
192+
* {@code transferTo} call violates the contract and would corrupt
193+
* implementations that lazily emit per-chunk framing.
194+
*
195+
* <p>A custom {@link FileRegion} whose {@code transferTo} advances {@code transferred}
196+
* past the bytes it writes to the target on a single call (legal: an encryption- or
197+
* framing-on-write FileRegion that flushes a complete inner chunk in one call behaves
198+
* this way) records every {@code transferTo} invocation made past
199+
* {@code transferred == count}. The expected behaviour for every transport is that
200+
* {@code transferToCallsPastCompletion} stays zero.
201+
*/
202+
public void testFileRegionDrainStopsAtCompletion(ServerBootstrap sb, Bootstrap cb) throws Throwable {
203+
// Region size > 1 so any chunked transport (e.g. io_uring's generic FileRegion fallback)
204+
// that sizes its chunk buffer from count() retains spare capacity after the first
205+
// (and only) source byte is written -- without that spare capacity an inner drain loop
206+
// would exit on writableBytes() == 0 and the overshoot path would not be exercised.
207+
final int regionSize = 16;
208+
209+
sb.childHandler(new SimpleChannelInboundHandler<ByteBuf>() {
210+
@Override
211+
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
212+
// drain
213+
}
214+
});
215+
cb.handler(new ChannelInboundHandlerAdapter());
216+
217+
Channel sc = sb.bind().sync().channel();
218+
Channel cc = cb.connect(sc.localAddress()).sync().channel();
219+
try {
220+
// OioByteStreamChannel#doWriteFileRegion drains using a locally-tracked
221+
// bytes-written counter as the transferTo position (ignoring transferred()), so an
222+
// ill-behaved FileRegion that advances transferred() past actual bytes written --
223+
// the exact pattern this fixture uses to exercise the overshoot path -- cannot
224+
// satisfy OIO's loop without violating the position invariant. The overshoot
225+
// detection is meaningless on OIO for the same reason; skip the permutation.
226+
assumeFalse(cc instanceof OioSocketChannel,
227+
"OIO transport does not honour transferred() for drain-loop termination");
228+
229+
OvershootDetectingFileRegion region = new OvershootDetectingFileRegion(regionSize);
230+
// sync() blocks until the write future completes, by which point every
231+
// transferTo() call the transport is going to make has already been made --
232+
// the fixture's call counter is observable here without any timing wait. Ref
233+
// ownership is transferred to the pipeline on writeAndFlush(), which releases
234+
// it as the write completes (success or failure).
235+
cc.writeAndFlush(region).sync();
236+
int overshoot = region.transferToCallsPastCompletion.get();
237+
assertEquals(0, overshoot,
238+
"transferTo() invoked " + overshoot
239+
+ " time(s) after region.transferred() reached region.count()="
240+
+ regionSize);
241+
} finally {
242+
cc.close().sync();
243+
sc.close().sync();
244+
}
245+
}
246+
169247
private static void testFileRegion0(
170248
ServerBootstrap sb, Bootstrap cb, boolean voidPromise, final boolean autoRead, boolean defaultFileRegion)
171249
throws Throwable {
@@ -316,6 +394,111 @@ public void exceptionCaught(ChannelHandlerContext ctx,
316394
}
317395
}
318396

397+
/**
398+
* Custom {@link FileRegion} that advances {@code transferred()} past the bytes it writes
399+
* to the target on a single {@code transferTo} call -- the same pattern an
400+
* encryption-on-write or framing FileRegion follows when it reports an inner chunk as
401+
* "delivered" once that chunk fully drains. Records every {@code transferTo} invocation
402+
* made after the region has been fully transferred so a drain-loop overshoot is visible
403+
* synchronously on the writer side.
404+
*
405+
* <ul>
406+
* <li>{@link #count()} returns the constant configured size.</li>
407+
* <li>The first {@code transferTo} call writes one byte to the target; once that
408+
* byte is accepted it advances {@code transferred} to {@code count} and returns
409+
* the bytes written.</li>
410+
* <li>Every subsequent {@code transferTo} call increments
411+
* {@link #transferToCallsPastCompletion} and writes one phantom byte to mimic
412+
* the protocol-corrupting side effect a real overshoot would produce.</li>
413+
* </ul>
414+
* The {@code transferred} field is written and read only on the EventLoop (in
415+
* {@code transferTo} and the surrounding drain loop), so no synchronization is needed.
416+
* The cross-thread observation channel is the {@link AtomicInteger} counter, which the
417+
* test reads after {@code writeAndFlush(...).sync()} establishes the happens-before.
418+
*/
419+
private static final class OvershootDetectingFileRegion extends AbstractReferenceCounted
420+
implements FileRegion {
421+
private final long count;
422+
private long transferred;
423+
final AtomicInteger transferToCallsPastCompletion = new AtomicInteger();
424+
425+
OvershootDetectingFileRegion(long count) {
426+
// count must be > 1 so the chunk buffer retains writable capacity after the first
427+
// source byte is written -- otherwise the drain-loop overshoot path is not exercised.
428+
this.count = ObjectUtil.checkInRange(count, 2L, Long.MAX_VALUE, "count");
429+
}
430+
431+
@Override
432+
public long position() {
433+
return 0;
434+
}
435+
436+
@Override
437+
public long count() {
438+
return count;
439+
}
440+
441+
@Override
442+
public long transferred() {
443+
return transferred;
444+
}
445+
446+
@Override
447+
@Deprecated
448+
public long transfered() {
449+
return transferred;
450+
}
451+
452+
@Override
453+
public long transferTo(WritableByteChannel target, long position) throws IOException {
454+
// Per FileRegion's contract, the caller passes transferred() as position. Surface
455+
// a violation via IOException so the transport's catch (Exception) at the write
456+
// site routes it to the write future's cause -- a JUnit AssertionError would
457+
// bypass that catch and wedge the EventLoop.
458+
if (position != transferred) {
459+
throw new IOException("transferTo position " + position + " != transferred() "
460+
+ transferred);
461+
}
462+
if (transferred < count) {
463+
int n = target.write(ByteBuffer.wrap(new byte[] { 0x42 }));
464+
if (n > 0) {
465+
transferred = count;
466+
}
467+
return n;
468+
}
469+
transferToCallsPastCompletion.incrementAndGet();
470+
return target.write(ByteBuffer.wrap(new byte[] { (byte) 0xFF }));
471+
}
472+
473+
@Override
474+
protected void deallocate() {
475+
// No native resources to release. The overshoot counter is observed on the
476+
// writer thread immediately after sync() returns; nothing else needs cleanup.
477+
}
478+
479+
@Override
480+
public FileRegion retain() {
481+
super.retain();
482+
return this;
483+
}
484+
485+
@Override
486+
public FileRegion retain(int increment) {
487+
super.retain(increment);
488+
return this;
489+
}
490+
491+
@Override
492+
public FileRegion touch() {
493+
return this;
494+
}
495+
496+
@Override
497+
public FileRegion touch(Object hint) {
498+
return this;
499+
}
500+
}
501+
319502
private static final class FileRegionWrapper implements FileRegion {
320503
private final FileRegion region;
321504

Collapse file

‎transport-classes-io_uring/src/main/java/io/netty/channel/uring/AbstractIoUringStreamChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/AbstractIoUringStreamChannel.java
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,11 @@ private int scheduleWriteFileRegion(int fd, IoRegistration registration, FileReg
341341
buf = alloc().directBuffer(chunkSize);
342342
try {
343343
ByteBufWritableByteChannel ch = new ByteBufWritableByteChannel(buf);
344-
while (buf.writableBytes() > 0) {
344+
// Mirror epoll's writeFileRegion(): stop calling transferTo() once
345+
// the region reports it has been fully transferred. The FileRegion
346+
// contract permits implementations to assume no further invocations
347+
// past transferred() == count().
348+
while (buf.writableBytes() > 0 && region.transferred() < region.count()) {
345349
long t = region.transferTo(ch, region.transferred());
346350
if (t <= 0) {
347351
break;

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.