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 — Port 4.2: LocalChannel: Deliver data written by the peer before it cl… · netty/netty@9291d80

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport/src
    • main/java/io/netty/channel/local
      • LocalChannel.java
    • test/java/io/netty/channel/local
      • LocalChannelTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎transport/src/main/java/io/netty/channel/local/LocalChannel.java‎

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/local/LocalChannel.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ public void run() {
294294

295295
private void tryClose(boolean isActive) {
296296
if (isActive) {
297+
// Data the peer wrote before it closed must be delivered before we close, like TCP does for data that
298+
// arrived before the FIN. The finishReadTask may have been re-queued behind this task by
299+
// finishPeerRead0(...) if we were writing while the peer wrote, in which case the data would otherwise
300+
// be dropped by releaseInboundBuffers().
301+
finishPeerRead0(this);
297302
unsafe().close(unsafe().voidPromise());
298303
} else if (connectPromise != null) {
299304
// The accepted channel was closed before the connection was established, for example because the server
Collapse file

‎transport/src/test/java/io/netty/channel/local/LocalChannelTest.java‎

Copy file name to clipboardExpand all lines: transport/src/test/java/io/netty/channel/local/LocalChannelTest.java
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,93 @@ public void testClientCloseAfterServerClosedWithQueuedConnection() throws Except
13301330
}
13311331
}
13321332

1333+
@Test
1334+
@Timeout(30)
1335+
public void testPeerWriteAndCloseWhileWriteInProgressDeliversData() throws Exception {
1336+
final CountDownLatch writeListenerEntered = new CountDownLatch(1);
1337+
final CountDownLatch releaseWriteListener = new CountDownLatch(1);
1338+
final CountDownLatch dataReceived = new CountDownLatch(1);
1339+
final CountDownLatch clientInactive = new CountDownLatch(1);
1340+
final AtomicReference<Boolean> receivedBeforeInactive = new AtomicReference<>();
1341+
final BlockingQueue<LocalChannel> accepted = new LinkedBlockingQueue<>();
1342+
1343+
ServerBootstrap sb = new ServerBootstrap()
1344+
.group(group1, group2)
1345+
.channel(LocalServerChannel.class)
1346+
.childHandler(new ChannelInitializer<LocalChannel>() {
1347+
@Override
1348+
protected void initChannel(LocalChannel ch) {
1349+
accepted.add(ch);
1350+
}
1351+
});
1352+
Bootstrap cb = new Bootstrap()
1353+
.group(group1)
1354+
.channel(LocalChannel.class)
1355+
.handler(new ChannelInitializer<LocalChannel>() {
1356+
@Override
1357+
protected void initChannel(LocalChannel ch) {
1358+
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
1359+
@Override
1360+
public void channelRead(ChannelHandlerContext ctx, Object msg) {
1361+
ReferenceCountUtil.release(msg);
1362+
dataReceived.countDown();
1363+
}
1364+
1365+
@Override
1366+
public void channelInactive(ChannelHandlerContext ctx) {
1367+
receivedBeforeInactive.set(dataReceived.getCount() == 0);
1368+
clientInactive.countDown();
1369+
}
1370+
});
1371+
}
1372+
});
1373+
1374+
Channel sc = null;
1375+
Channel cc = null;
1376+
try {
1377+
sc = sb.bind(TEST_ADDRESS).sync().channel();
1378+
cc = cb.connect(TEST_ADDRESS).sync().channel();
1379+
final LocalChannel child = accepted.poll(5, SECONDS);
1380+
assertNotNull(child);
1381+
while (!child.isActive()) {
1382+
Thread.sleep(10);
1383+
}
1384+
1385+
// Block the client inside doWrite(...) (the promise listener is run while writeInProgress is true) so
1386+
// that the peer sees the client as writing.
1387+
cc.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 'X' })).addListener(new ChannelFutureListener() {
1388+
@Override
1389+
public void operationComplete(ChannelFuture future) throws Exception {
1390+
writeListenerEntered.countDown();
1391+
try {
1392+
releaseWriteListener.await();
1393+
} catch (InterruptedException ignore) {
1394+
Thread.currentThread().interrupt();
1395+
}
1396+
}
1397+
});
1398+
assertTrue(writeListenerEntered.await(5, SECONDS));
1399+
1400+
// Write and close from the peer while the client is still writing, then unblock the client.
1401+
child.eventLoop().submit(new Runnable() {
1402+
@Override
1403+
public void run() {
1404+
child.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 'P' }));
1405+
child.close();
1406+
}
1407+
}).sync();
1408+
releaseWriteListener.countDown();
1409+
1410+
assertTrue(clientInactive.await(5, SECONDS));
1411+
assertEquals(Boolean.TRUE, receivedBeforeInactive.get(),
1412+
"Data written by the peer before it closed must not be dropped");
1413+
} finally {
1414+
releaseWriteListener.countDown();
1415+
closeChannel(cc);
1416+
closeChannel(sc);
1417+
}
1418+
}
1419+
13331420
@Test
13341421
public void testClientCloseWhileConnectionIsQueued() throws Exception {
13351422
BlockingQueue<LocalChannel> accepted = new LinkedBlockingQueue<>();

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.