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 — Quic: Ensure writes are done before notify close promise of QuicheQui… · netty/netty@d810e99

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-classes-quic/src/main/java/io/netty/handler/codec/quic
    • QuicheQuicChannel.java
  • codec-native-quic/src/test/java/io/netty/handler/codec/quic
    • QuicChannelConnectTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicheQuicChannel.java‎

Copy file name to clipboardExpand all lines: codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicheQuicChannel.java
+20-5Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,20 @@ protected void doClose(Promise<Void> promise) {
646646
outErrorCodeBuffer = null;
647647
}
648648
} finally {
649-
if (sendResult == SendResult.SOME) {
650-
// As this is the close let us flush it asap.
651-
forceFlushParent();
649+
Future<Void> wf = null;
650+
final InetSocketAddress sendToAddress = remote;
651+
if (sendToAddress == null) {
652+
if (sendResult == SendResult.SOME) {
653+
// As this is the close let us flush it asap.
654+
forceFlushParent();
655+
} else {
656+
flushParent();
657+
}
652658
} else {
653-
flushParent();
659+
// We need to ensure all writes were done before we actually notify the close promise as otherwise
660+
// we might not be able to write things to the underlying socket if the socket is closed as soon
661+
// as all QuicChannel.close() ChannelFutures are completed.
662+
wf = parent().writeAndFlush(new DatagramPacket(Unpooled.EMPTY_BUFFER, sendToAddress));
654663
}
655664
conn.free();
656665
if (freeTask != null) {
@@ -660,7 +669,13 @@ protected void doClose(Promise<Void> promise) {
660669

661670
local = null;
662671
remote = null;
663-
promise.setSuccess(null);
672+
if (wf == null) {
673+
promise.setSuccess(null);
674+
} else {
675+
wf.addListener(__ -> {
676+
promise.setSuccess(null);
677+
});
678+
}
664679
}
665680
}
666681
}
Collapse file

‎codec-native-quic/src/test/java/io/netty/handler/codec/quic/QuicChannelConnectTest.java‎

Copy file name to clipboardExpand all lines: codec-native-quic/src/test/java/io/netty/handler/codec/quic/QuicChannelConnectTest.java
+111-5Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import java.security.spec.MGF1ParameterSpec;
6969
import java.security.spec.PSSParameterSpec;
7070
import java.util.ArrayList;
71+
import java.util.Collections;
7172
import java.util.HashMap;
7273
import java.util.HashSet;
7374
import java.util.List;
@@ -80,18 +81,19 @@
8081
import java.util.concurrent.LinkedBlockingQueue;
8182
import java.util.concurrent.TimeUnit;
8283
import java.util.concurrent.atomic.AtomicBoolean;
84+
import java.util.concurrent.atomic.AtomicInteger;
8385
import java.util.concurrent.atomic.AtomicReference;
8486
import java.util.function.Consumer;
8587

86-
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
87-
import static org.junit.jupiter.api.Assertions.assertSame;
88-
import static org.junit.jupiter.api.Assertions.assertTrue;
89-
import static org.junit.jupiter.api.Assertions.assertThrows;
90-
import static org.junit.jupiter.api.Assertions.fail;
9188
import static org.junit.jupiter.api.Assertions.assertEquals;
89+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
9290
import static org.junit.jupiter.api.Assertions.assertNotEquals;
9391
import static org.junit.jupiter.api.Assertions.assertNotNull;
9492
import static org.junit.jupiter.api.Assertions.assertNull;
93+
import static org.junit.jupiter.api.Assertions.assertSame;
94+
import static org.junit.jupiter.api.Assertions.assertThrows;
95+
import static org.junit.jupiter.api.Assertions.assertTrue;
96+
import static org.junit.jupiter.api.Assertions.fail;
9597

9698
public class QuicChannelConnectTest extends AbstractQuicTest {
9799

@@ -1861,6 +1863,110 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
18611863
}
18621864
}
18631865

1866+
@ParameterizedTest
1867+
@MethodSource("newSslTaskExecutors")
1868+
@Timeout(10)
1869+
public void testConnectionCloseFrameDeliveredWhenParentClosedImmediately(Executor executor) throws Throwable {
1870+
int numConnections = 10;
1871+
int streamsPerConnection = 50;
1872+
1873+
CountDownLatch serverStreamsLatch = new CountDownLatch(numConnections * streamsPerConnection);
1874+
List<QuicChannel> serverConnections = Collections.synchronizedList(new ArrayList<>(numConnections));
1875+
1876+
Channel server = QuicTestUtils.newServer(executor, new ChannelInboundHandler() {
1877+
@Override
1878+
public boolean isSharable() {
1879+
return true;
1880+
}
1881+
1882+
@Override
1883+
public void channelActive(ChannelHandlerContext ctx) {
1884+
serverConnections.add((QuicChannel) ctx.channel());
1885+
ctx.fireChannelActive();
1886+
}
1887+
}, new ChannelInboundHandler() {
1888+
@Override
1889+
public boolean isSharable() {
1890+
return true;
1891+
}
1892+
1893+
@Override
1894+
public void channelActive(ChannelHandlerContext ctx) {
1895+
serverStreamsLatch.countDown();
1896+
ctx.fireChannelActive();
1897+
}
1898+
});
1899+
1900+
server.pipeline().addFirst(new ChannelOutboundHandler() {
1901+
// simulate delay for writing and flushing packets so it would race with closing.
1902+
@Override
1903+
public void write(ChannelHandlerContext ctx, Object msg, CompletionHandler<Void> callback) {
1904+
ctx.executor().execute(() -> ctx.write(msg, callback));
1905+
}
1906+
1907+
@Override
1908+
public void flush(ChannelHandlerContext ctx) {
1909+
ctx.executor().execute(ctx::flush);
1910+
}
1911+
});
1912+
InetSocketAddress address = (InetSocketAddress) server.localAddress();
1913+
Channel channel = QuicTestUtils.newClient(executor);
1914+
1915+
CountDownLatch clientConnectionsLatch = new CountDownLatch(numConnections);
1916+
try {
1917+
for (int i = 0; i < numConnections; i++) {
1918+
QuicChannelBootstrap quicChannelBootstrap = QuicTestUtils.newQuicChannelBootstrap(channel);
1919+
quicChannelBootstrap.handler(new ChannelInboundHandler() {
1920+
@Override
1921+
public boolean isSharable() {
1922+
return true;
1923+
}
1924+
1925+
@Override
1926+
public void userEventTriggered(ChannelHandlerContext ctx,
1927+
Object evt) {
1928+
if (evt instanceof QuicConnectionCloseEvent) {
1929+
clientConnectionsLatch.countDown();
1930+
}
1931+
ctx.fireUserEventTriggered(evt);
1932+
}
1933+
});
1934+
QuicChannel quicChannel = quicChannelBootstrap.streamHandler(new ChannelInboundHandler() { })
1935+
.remoteAddress(address)
1936+
.connect()
1937+
.get();
1938+
1939+
for (int j = 0; j < streamsPerConnection; j++) {
1940+
QuicStreamChannel stream =
1941+
quicChannel.createStream(QuicStreamType.BIDIRECTIONAL, new ChannelInboundHandler() { })
1942+
.sync().getNow();
1943+
stream.writeAndFlush(Unpooled.directBuffer().writeZero(1)).sync();
1944+
}
1945+
}
1946+
1947+
assertTrue(serverStreamsLatch.await(5, TimeUnit.SECONDS), "Server did not receive all streams");
1948+
assertEquals(numConnections, serverConnections.size());
1949+
1950+
AtomicInteger closedConnections = new AtomicInteger();
1951+
server.executor().execute(() -> {
1952+
for (int i = 0; i < numConnections; i++) {
1953+
serverConnections.get(i).close().addListener(v -> {
1954+
if (closedConnections.incrementAndGet() == numConnections) {
1955+
server.close();
1956+
}
1957+
});
1958+
}
1959+
});
1960+
1961+
assertTrue(clientConnectionsLatch.await(5, TimeUnit.SECONDS),
1962+
"Not all clients received QuicConnectionCloseEvent");
1963+
} finally {
1964+
server.close().sync();
1965+
channel.close().sync();
1966+
shutdown(executor);
1967+
}
1968+
}
1969+
18641970
private static final class ChannelStateVerifyHandler extends QuicChannelValidationHandler {
18651971
@Override
18661972
public void channelActive(ChannelHandlerContext ctx) {

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.