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: don't permanently consume stream ids when createStream(...) fails · Echo009/netty@6a86656

File tree

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

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

Copy file name to clipboardExpand all lines: codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicStreamIdGenerator.java
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,22 @@ long nextStreamId(boolean bidirectional) {
3838
nextUnidirectionalStreamId += 4;
3939
return stream;
4040
}
41+
42+
/**
43+
* Rolls back the given {@code streamId} if it is the most recently allocated one of its
44+
* direction, so a failed stream creation does not permanently consume it. Returns
45+
* {@code true} if the rollback was done.
46+
*/
47+
boolean rollbackStreamId(long streamId, boolean bidirectional) {
48+
if (bidirectional) {
49+
if (streamId == nextBidirectionalStreamId - 4) {
50+
nextBidirectionalStreamId = streamId;
51+
return true;
52+
}
53+
} else if (streamId == nextUnidirectionalStreamId - 4) {
54+
nextUnidirectionalStreamId = streamId;
55+
return true;
56+
}
57+
return false;
58+
}
4159
}
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
+21-2Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,16 @@ void connectStream(QuicStreamType type, @Nullable ChannelHandler handler,
15741574
if (!promise.setUncancellable()) {
15751575
return;
15761576
}
1577+
// Fail fast without consuming a stream id when all credits are used. Otherwise a
1578+
// failed createStream(...) permanently consumes the id: quiche rejects the implicit
1579+
// open by stream id position and never returns it, so once ids are allocated past
1580+
// the peer's limit every future createStream(...) fails with STREAM_LIMIT_ERROR,
1581+
// even after MAX_STREAMS restored the limit, while peerAllowedStreams(...) keeps
1582+
// reporting capacity.
1583+
if (peerAllowedStreams(type) <= 0) {
1584+
promise.setFailure(Quiche.convertToException(Quiche.QUICHE_ERR_STREAM_LIMIT));
1585+
return;
1586+
}
15771587
long streamId = idGenerator.nextStreamId(type == QuicStreamType.BIDIRECTIONAL);
15781588

15791589
try {
@@ -1582,6 +1592,13 @@ void connectStream(QuicStreamType type, @Nullable ChannelHandler handler,
15821592
throw Quiche.convertToException(res);
15831593
}
15841594
} catch (Exception e) {
1595+
// quiche rejected the implicit open before creating any state for the stream,
1596+
// so the id can safely be given back and must be, to not permanently consume
1597+
// it (only done for STREAM_LIMIT as other errors might have left state behind).
1598+
if (e instanceof QuicException &&
1599+
((QuicException) e).error() == QuicTransportError.STREAM_LIMIT_ERROR) {
1600+
idGenerator.rollbackStreamId(streamId, type == QuicStreamType.BIDIRECTIONAL);
1601+
}
15851602
promise.setFailure(e);
15861603
return;
15871604
}
@@ -1784,8 +1801,10 @@ private void processReceived(QuicheQuicConnection conn) {
17841801
Quiche.quiche_conn_is_in_early_data(connAddr)) {
17851802
long uniLeftOld = uniStreamsLeft;
17861803
long bidiLeftOld = bidiStreamsLeft;
1787-
// Only fetch new stream info when we used all our credits
1788-
if (uniLeftOld == 0 || bidiLeftOld == 0) {
1804+
// Only fetch new stream info when we used all our credits. Also cover negative
1805+
// values: these can happen if multiple streams were created within one batch
1806+
// without a refresh in-between, and would otherwise never be refreshed again.
1807+
if (uniLeftOld <= 0 || bidiLeftOld <= 0) {
17891808
long uniLeft = Quiche.quiche_conn_peer_streams_left_uni(connAddr);
17901809
long bidiLeft = Quiche.quiche_conn_peer_streams_left_bidi(connAddr);
17911810
uniStreamsLeft = uniLeft;
Collapse file

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

Copy file name to clipboardExpand all lines: codec-native-quic/src/test/java/io/netty/handler/codec/quic/QuicStreamLimitTest.java
+91Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,97 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
124124
}
125125
}
126126

127+
@ParameterizedTest
128+
@MethodSource("newSslTaskExecutors")
129+
public void testStreamCreationSucceedsAfterFailedCreationAndLimitRestoredViaClientBidirectional(
130+
Executor executor) throws Throwable {
131+
testStreamCreationSucceedsAfterFailedCreationAndLimitRestoredViaClient(executor, QuicStreamType.BIDIRECTIONAL);
132+
}
133+
134+
@ParameterizedTest
135+
@MethodSource("newSslTaskExecutors")
136+
public void testStreamCreationSucceedsAfterFailedCreationAndLimitRestoredViaClientUnidirectional(
137+
Executor executor) throws Throwable {
138+
testStreamCreationSucceedsAfterFailedCreationAndLimitRestoredViaClient(executor, QuicStreamType.UNIDIRECTIONAL);
139+
}
140+
141+
private static void testStreamCreationSucceedsAfterFailedCreationAndLimitRestoredViaClient(
142+
Executor executor, QuicStreamType type) throws Throwable {
143+
QuicChannelValidationHandler serverHandler = new QuicChannelValidationHandler();
144+
Channel server = QuicTestUtils.newServer(
145+
QuicTestUtils.newQuicServerBuilder(executor).initialMaxStreamsBidirectional(1)
146+
.initialMaxStreamsUnidirectional(1),
147+
InsecureQuicTokenHandler.INSTANCE,
148+
serverHandler, new ChannelInboundHandlerAdapter() {
149+
@Override
150+
public boolean isSharable() {
151+
return true;
152+
}
153+
154+
@Override
155+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
156+
if (evt == ChannelInputShutdownReadComplete.INSTANCE) {
157+
ctx.close();
158+
}
159+
}
160+
});
161+
InetSocketAddress address = (InetSocketAddress) server.localAddress();
162+
Channel channel = QuicTestUtils.newClient(executor);
163+
164+
CountDownLatch latch = new CountDownLatch(1);
165+
CountDownLatch latch2 = new CountDownLatch(1);
166+
QuicChannelValidationHandler clientHandler = new QuicChannelValidationHandler() {
167+
@Override
168+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
169+
if (evt instanceof QuicStreamLimitChangedEvent) {
170+
if (latch.getCount() == 0) {
171+
latch2.countDown();
172+
} else {
173+
latch.countDown();
174+
}
175+
}
176+
super.userEventTriggered(ctx, evt);
177+
}
178+
};
179+
try {
180+
QuicChannel quicChannel = QuicTestUtils.newQuicChannelBootstrap(channel)
181+
.handler(clientHandler)
182+
.streamHandler(new ChannelInboundHandlerAdapter())
183+
.remoteAddress(address)
184+
.connect().get();
185+
latch.await();
186+
QuicStreamChannel stream = quicChannel.createStream(
187+
type, new ChannelInboundHandlerAdapter()).get();
188+
assertEquals(0, quicChannel.peerAllowedStreams(type));
189+
190+
// Second stream creation should fail as the limit is reached.
191+
Throwable cause = quicChannel.createStream(
192+
type, new ChannelInboundHandlerAdapter()).await().cause();
193+
assertInstanceOf(QuicException.class, cause);
194+
stream.close().sync();
195+
latch2.await();
196+
assertEquals(1, quicChannel.peerAllowedStreams(type));
197+
198+
// Once the limit is restored, creating a stream must succeed again: a failed
199+
// createStream(...) must not have permanently consumed a stream id. Otherwise
200+
// this and every later creation on the connection would keep failing with
201+
// STREAM_LIMIT_ERROR while peerAllowedStreams(...) reports capacity.
202+
QuicStreamChannel stream2 = quicChannel.createStream(
203+
type, new ChannelInboundHandlerAdapter()).get();
204+
stream2.close().sync();
205+
quicChannel.close().sync();
206+
207+
serverHandler.assertState();
208+
clientHandler.assertState();
209+
} finally {
210+
server.close().sync();
211+
// Close the parent Datagram channel as well.
212+
channel.close().sync();
213+
214+
shutdown(executor);
215+
}
216+
}
217+
127218
@ParameterizedTest
128219
@MethodSource("newSslTaskExecutors")
129220
public void testStreamLimitEnforcedWhenCreatingViaServerBidirectional(Executor executor) throws Throwable {

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.