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 — Don't stop reading domain datagrams after an empty datagram · netty/netty@184efbd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • testsuite/src/main/java/io/netty/testsuite/transport/socket
    • DatagramUnicastTest.java
  • transport-classes-epoll/src/main/java/io/netty/channel/epoll
    • EpollDomainDatagramChannel.java
  • transport-classes-kqueue/src/main/java/io/netty/channel/kqueue
    • KQueueDomainDatagramChannel.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java
+64Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,70 @@ public void run() {
328328
}
329329
}
330330

331+
@Test
332+
public void testEmptyDatagramStartsReadLoop(TestInfo testInfo) throws Throwable {
333+
run(testInfo, new Runner<Bootstrap, Bootstrap>() {
334+
@Override
335+
public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable {
336+
testEmptyDatagramStartsReadLoop(bootstrap, bootstrap2);
337+
}
338+
});
339+
}
340+
341+
public void testEmptyDatagramStartsReadLoop(Bootstrap sb, Bootstrap cb) throws Throwable {
342+
final String[] sent = { "A", "", "B", "", "C" };
343+
final List<String> received = new ArrayList<String>();
344+
final CountDownLatch firstReadComplete = new CountDownLatch(1);
345+
final CountDownLatch latch = new CountDownLatch(sent.length);
346+
Channel sc = null;
347+
Channel cc = null;
348+
try {
349+
sc = sb.handler(new SimpleChannelInboundHandler<ByteBufHolder>() {
350+
@Override
351+
protected void channelRead0(ChannelHandlerContext ctx, ByteBufHolder msg) {
352+
synchronized (received) {
353+
received.add(msg.content().toString(CharsetUtil.US_ASCII));
354+
}
355+
latch.countDown();
356+
}
357+
358+
@Override
359+
public void channelReadComplete(ChannelHandlerContext ctx) {
360+
firstReadComplete.countDown();
361+
}
362+
}).bind(newSocketAddress()).sync().channel();
363+
cc = cb.handler(new ChannelInboundHandlerAdapter()).bind(newSocketAddress()).sync().channel();
364+
365+
SocketAddress localAddr = sc.localAddress();
366+
SocketAddress addr = localAddr instanceof InetSocketAddress ?
367+
sendToAddress((InetSocketAddress) localAddr) : localAddr;
368+
369+
ByteBuf first = Unpooled.copiedBuffer(sent[0], CharsetUtil.US_ASCII);
370+
ChannelFuture firstFuture = write(cc, first, addr, WrapType.NONE);
371+
first.release();
372+
cc.flush();
373+
firstFuture.sync();
374+
// Wait until the first read loop has completed so that the next one starts with the empty datagram.
375+
assertTrue(firstReadComplete.await(10, TimeUnit.SECONDS), "received " + received);
376+
377+
for (int i = 1; i < sent.length; i++) {
378+
ByteBuf buf = Unpooled.copiedBuffer(sent[i], CharsetUtil.US_ASCII);
379+
write(cc, buf, addr, WrapType.NONE);
380+
buf.release();
381+
}
382+
cc.flush();
383+
384+
// A read loop that starts with an empty datagram must not stop before all datagrams are read.
385+
assertTrue(latch.await(10, TimeUnit.SECONDS), "received " + received);
386+
synchronized (received) {
387+
assertEquals(Arrays.asList(sent), received);
388+
}
389+
} finally {
390+
closeChannel(cc);
391+
closeChannel(sc);
392+
}
393+
}
394+
331395
@Test
332396
public void testSendToUnresolvableAddress(TestInfo testInfo) throws Throwable {
333397
run(testInfo, new Runner<Bootstrap, Bootstrap>() {
Collapse file

‎transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollDomainDatagramChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollDomainDatagramChannel.java
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,11 @@ void epollInReady() {
340340
if (localAddress == null) {
341341
localAddress = (DomainSocketAddress) localAddress();
342342
}
343-
allocHandle.lastBytesRead(remoteAddress.receivedAmount());
344-
byteBuf.writerIndex(byteBuf.writerIndex() + allocHandle.lastBytesRead());
343+
final int receivedAmount = remoteAddress.receivedAmount();
344+
// Avoid signalling end-of-data for zero-sized datagrams, otherwise continueReading(...)
345+
// may stop the loop and leave the remaining datagrams unread.
346+
allocHandle.lastBytesRead(Math.max(1, receivedAmount));
347+
byteBuf.writerIndex(byteBuf.writerIndex() + receivedAmount);
345348

346349
packet = new DomainDatagramPacket(byteBuf, localAddress, remoteAddress);
347350
}
Collapse file

‎transport-classes-kqueue/src/main/java/io/netty/channel/kqueue/KQueueDomainDatagramChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-kqueue/src/main/java/io/netty/channel/kqueue/KQueueDomainDatagramChannel.java
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,11 @@ void readReady(KQueueRecvByteAllocatorHandle allocHandle) {
285285
if (localAddress == null) {
286286
localAddress = (DomainSocketAddress) localAddress();
287287
}
288-
allocHandle.lastBytesRead(remoteAddress.receivedAmount());
289-
byteBuf.writerIndex(byteBuf.writerIndex() + allocHandle.lastBytesRead());
288+
final int receivedAmount = remoteAddress.receivedAmount();
289+
// Avoid signalling end-of-data for zero-sized datagrams, otherwise continueReading(...)
290+
// may stop the loop and leave the remaining datagrams unread.
291+
allocHandle.lastBytesRead(Math.max(1, receivedAmount));
292+
byteBuf.writerIndex(byteBuf.writerIndex() + receivedAmount);
290293

291294
packet = new DomainDatagramPacket(byteBuf, localAddress, remoteAddress);
292295
}

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.