Environment
- Netty 4.2.18.Final (code path verified unchanged in 4.2.20.Final and current 4.2 branch HEAD)
netty-codec-classes-quic / netty-codec-native-quic (quiche pinned at 55886df3), Linux x86_64
- Reproduced with a pure netty client + netty server over loopback (no third-party code)
Steps to reproduce
The existing QuicStreamLimitTest.testStreamLimitEnforcedWhenCreatingViaClient walks right up to this and stops: on the 4.2.18.Final tag it deliberately fails the second createStream (QuicStreamLimitTest.java:101-104), closes the stream, asserts peerAllowedStreams == 1 (:108) and then closes the connection (:109). Adding one more createStream at that point reproduces the defect:
- Server configured with
initialMaxStreamsBidirectional(1).
- Client opens 1 stream → succeeds,
peerAllowedStreams(BIDIRECTIONAL) == 0.
- Second
createStream → fails with QuicException wrapping QuicTransportError(STREAM_LIMIT_ERROR).
- Close the first stream, wait for
QuicStreamLimitChangedEvent / MAX_STREAMS.
peerAllowedStreams(BIDIRECTIONAL) == 1 — as documented, one more stream can be created.
- Third
createStream → still fails with STREAM_LIMIT_ERROR, and every subsequent attempt on this connection fails forever, while peerAllowedStreams keeps returning 1.
We keep this as a standalone regression-style test (runs green in under a second against the current behavior; it documents the defect and will flip red once fixed). Happy to attach it — it only uses netty classes.
Expected behavior
QuicChannel.peerAllowedStreams javadoc:
"Returns the number of streams that can be created before stream creation will fail with {@link QuicTransportError#STREAM_LIMIT_ERROR} error."
After the peer restores the limit and the method returns 1, the next createStream should succeed. Likewise QuicStreamLimitChangedEvent signals "stream limit restored", implying restored quota is usable.
Actual behavior / root cause
QuicheQuicChannel.connectStream allocates the stream ID before attempting the implicit open (idGenerator.nextStreamId(...) at QuicheQuicChannel.java:1543 on 4.2.18.Final), then drives the open via quiche_conn_stream_send. On failure the promise fails but the generator is never rolled back, so every failed attempt permanently advances the sequence of all future stream IDs on the connection.
quiche's implicit-open check is positional: get_or_create_stream rejects when max(local_opened_streams_bidi, id_sequence + 1) > peer_max_streams_bidi and advances nothing on rejection (quiche src/stream/mod.rs, pinned commit). Since peer_streams_left_bidi = peer_max_streams_bidi − local_opened_streams_bidi never counts failed attempts, the two views diverge: after N burned IDs, peerAllowedStreams() > 0 no longer implies the next createStream can succeed, and the only recovery is closing the connection.
A pool-style caller that follows the javadoc ("check peerAllowedStreams > 0, then createStream") still hits this at the limit boundary (concurrent attempts on the last slot), after which the connection keeps reporting capacity while every open fails — which is exactly how we found it in production.
For contrast, skipping IDs is of course RFC 9000-legal (§2.1 out-of-order use), and quiche's own h3 layer avoids the burn by only advancing next_uni_stream_id after a successful send ("To avoid skipping stream IDs, we only calculate the next available stream ID when data has been successfully buffered", quiche src/h3/mod.rs).
Suggested fix
Pre-check the cached bidiStreamsLeft / uniStreamsLeft > 0 in connectStream before allocating the ID, and fail fast with STREAM_LIMIT_ERROR otherwise. The "cache ≤ actual" invariant plus the fact that connectStream and the cache refresh both run on the connection eventloop make this airtight: if the pre-check passes, quiche's positional check passes too. Alternative: roll back QuicStreamIdGenerator on failure (safe for the same serialization reason — the failure branch completes before the next allocation).
Secondary observation while auditing this area: the cache refresh condition uniLeftOld == 0 || bidiLeftOld == 0 (QuicheQuicChannel.java:1754) uses == 0. A cached value that went negative (reachable in a narrow race: cache > 0 while MAX_STREAMS already arrived, then several opens succeed past zero within one batch) never matches == 0 again and peerAllowedStreams freezes below zero permanently. <= 0 (or clamping) would close that corner.