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: only trust refreshed stream credits for the fail-fast check · Echo009/netty@5c312c6

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
    • test/java/io/netty/handler/codec/quic
      • QuicStreamIdGeneratorTest.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/QuicheQuicChannel.java‎

Copy file name to clipboardExpand all lines: codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicheQuicChannel.java
+20-4Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ public void operationComplete(ChannelFuture future) {
184184
AtomicLongFieldUpdater.newUpdater(QuicheQuicChannel.class, "bidiStreamsLeft");
185185
private volatile long bidiStreamsLeft;
186186

187+
// Whether the cached stream credits were refreshed from quiche at least once. Before the
188+
// first refresh the caches are 0 even though the peer may have announced credits, so the
189+
// fail-fast check in connectStream(...) must not fire yet; a rejected implicit open in
190+
// that window is instead handled by rolling back the allocated stream id.
191+
private volatile boolean streamCreditsInitialized;
192+
187193
private QuicheQuicChannel(Channel parent, boolean server, @Nullable ByteBuffer key, InetSocketAddress local,
188194
InetSocketAddress remote, boolean supportsDatagram, ChannelHandler streamHandler,
189195
Map.Entry<ChannelOption<?>, Object>[] streamOptionsArray,
@@ -1579,8 +1585,10 @@ void connectStream(QuicStreamType type, @Nullable ChannelHandler handler,
15791585
// open by stream id position and never returns it, so once ids are allocated past
15801586
// the peer's limit every future createStream(...) fails with STREAM_LIMIT_ERROR,
15811587
// even after MAX_STREAMS restored the limit, while peerAllowedStreams(...) keeps
1582-
// reporting capacity.
1583-
if (peerAllowedStreams(type) <= 0) {
1588+
// reporting capacity. Only trusted once the caches were refreshed from quiche;
1589+
// before that they are 0 even though the peer may have announced credits, so we
1590+
// fall through to quiche and let the rollback below handle a rejection.
1591+
if (streamCreditsInitialized && peerAllowedStreams(type) <= 0) {
15841592
promise.setFailure(Quiche.convertToException(Quiche.QUICHE_ERR_STREAM_LIMIT));
15851593
return;
15861594
}
@@ -1802,13 +1810,21 @@ private void processReceived(QuicheQuicConnection conn) {
18021810
long uniLeftOld = uniStreamsLeft;
18031811
long bidiLeftOld = bidiStreamsLeft;
18041812
// 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.
1813+
// values: MAX_STREAMS can raise the peer's limit while the cached credits are
1814+
// still non-zero (this refresh only triggers once a direction hit 0), so the
1815+
// cache may lag behind quiche's real credits. createStream(...) then keeps
1816+
// succeeding on quiche's real credits while the cached count is decremented
1817+
// past zero, after which a == 0 check would never match again and the cached
1818+
// credits would be frozen below zero forever. The fail-fast check in
1819+
// connectStream(...) makes crossing zero impossible, so covering negatives
1820+
// here is a safety net that also recovers an already-negative cached value
1821+
// on the next received packet.
18071822
if (uniLeftOld <= 0 || bidiLeftOld <= 0) {
18081823
long uniLeft = Quiche.quiche_conn_peer_streams_left_uni(connAddr);
18091824
long bidiLeft = Quiche.quiche_conn_peer_streams_left_bidi(connAddr);
18101825
uniStreamsLeft = uniLeft;
18111826
bidiStreamsLeft = bidiLeft;
1827+
streamCreditsInitialized = true;
18121828
if (uniLeftOld != uniLeft || bidiLeftOld != bidiLeft) {
18131829
pipeline().fireUserEventTriggered(QuicStreamLimitChangedEvent.INSTANCE);
18141830
}
Collapse file

‎codec-classes-quic/src/test/java/io/netty/handler/codec/quic/QuicStreamIdGeneratorTest.java‎

Copy file name to clipboard
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.codec.quic;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.ValueSource;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
26+
public class QuicStreamIdGeneratorTest {
27+
28+
@ParameterizedTest
29+
@ValueSource(booleans = {true, false})
30+
public void testRollbackRestoresMostRecentlyAllocatedIdAndReusesIt(boolean bidirectional) {
31+
QuicStreamIdGenerator generator = new QuicStreamIdGenerator(false);
32+
long streamId = generator.nextStreamId(bidirectional);
33+
34+
assertTrue(generator.rollbackStreamId(streamId, bidirectional));
35+
// After the rollback the same id must be handed out again, so a failed creation
36+
// does not permanently consume it.
37+
assertEquals(streamId, generator.nextStreamId(bidirectional));
38+
}
39+
40+
@ParameterizedTest
41+
@ValueSource(booleans = {true, false})
42+
public void testRollbackOfIdThatIsNotTheFrontierFails(boolean bidirectional) {
43+
QuicStreamIdGenerator generator = new QuicStreamIdGenerator(false);
44+
long first = generator.nextStreamId(bidirectional);
45+
long second = generator.nextStreamId(bidirectional);
46+
47+
// Only the most recently allocated id of a direction may be rolled back, as older
48+
// ids may already have an open stream in quiche.
49+
assertFalse(generator.rollbackStreamId(first, bidirectional));
50+
// A failed rollback must not move the frontier.
51+
assertEquals(second + 4, generator.nextStreamId(bidirectional));
52+
}
53+
54+
@Test
55+
public void testRollbackIsIndependentPerDirection() {
56+
QuicStreamIdGenerator generator = new QuicStreamIdGenerator(false);
57+
long uni = generator.nextStreamId(false);
58+
long bidi = generator.nextStreamId(true);
59+
60+
assertTrue(generator.rollbackStreamId(uni, false));
61+
// Rolling back the unidirectional frontier must not affect the bidirectional one,
62+
// which keeps advancing, while the rolled-back unidirectional id is handed out again.
63+
assertEquals(bidi + 4, generator.nextStreamId(true));
64+
assertEquals(uni, generator.nextStreamId(false));
65+
}
66+
}
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
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
191191
Throwable cause = quicChannel.createStream(
192192
type, new ChannelInboundHandlerAdapter()).await().cause();
193193
assertInstanceOf(QuicException.class, cause);
194+
195+
// Repeated failed creations while the limit is exhausted must not consume
196+
// stream ids either: if they did, the creation below after the restore would
197+
// fail with STREAM_LIMIT_ERROR again because the stream id position moved
198+
// past the limit.
199+
for (int i = 0; i < 5; i++) {
200+
Throwable repeatedCause = quicChannel.createStream(
201+
type, new ChannelInboundHandlerAdapter()).await().cause();
202+
assertInstanceOf(QuicException.class, repeatedCause);
203+
}
194204
stream.close().sync();
195205
latch2.await();
196206
assertEquals(1, quicChannel.peerAllowedStreams(type));

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.