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 — Keep reading a closed QUIC stream until the end of its input by allthingssecurity · Pull Request #17755 · netty/netty

Conversation

@allthingssecurity

@allthingssecurity allthingssecurity commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Motivation:

When a QuicStreamChannel is closed before the FIN of the remote peer was read, QuicheQuicStreamChannel never reads from that stream again:

  • recv() only loops while (active && readPending && readable),
  • readable() only calls recv() when a read is pending,
  • close() sends our FIN but leaves the receive side alone.

quiche keeps the unread data and the FIN, which has three effects:

  1. The data keeps its connection-level flow control credit, as quiche only returns credit for data the application consumed. A client that closes streams while responses are still arriving (cancelled requests, a common case) uses up the connection window. After that, no stream of the connection receives data anymore.
  2. The stream is never completed and collected, so its stream credit is never returned to the remote peer (no MAX_STREAMS update). A server that closes streams before it read the request's FIN (for example after writing an early error response) stops the client from opening new streams once initialMaxStreamsBidirectional such streams were closed.
  3. The closed stream channel is never removed from the streams of its QuicChannel, as removeStreamFromParent() requires the FIN to be read.

Reproduced on the real transport with the released 4.2.18 native library (the JNI code is unchanged since):

  • Server with initialMaxStreamsBidirectional(2) that closes each stream on its first channelRead: the client completes 2 streams; creating the 3rd fails with QUICHE_ERR_STREAM_LIMIT, and peerAllowedStreams(BIDIRECTIONAL) stays 0. With a server that closes after the FIN, all 6 streams complete.
  • Client with initialMaxData(256 KiB) that closes each stream after the first chunk of a 64 KiB response: after 4 such requests, the next request gets no data at all and the connection idles out.

Modification:

  • At the end of recv(), if the channel is closed, read and drop what is left (discardInput(...)) until quiche reports no more data, the FIN, or an error (stream reset or closed connection). removeStreamFromParent() then removes the stream once the end of the input was seen.
  • readable() calls recv() for a closed channel even if no read is pending, so data and the FIN that arrive after the close are dropped too.
  • close() drops what is left if the stream was readable already (when close() is called from channelRead(...), recv() does it after its loop).
  • Add QuicStreamLimitTest.testStreamLimitRestoredWhenStreamClosedBeforeEndOfInput: the server closes each stream on its first read, before the client's FIN arrives; the client creates three streams in a row with a stream limit of 1 and checks that the server returns the stream credit (QuicStreamLimitChangedEvent) each time.
  • Add QuicStreamChannelCloseTest.testCloseBeforeEndOfInputReturnsFlowControlCredit: the client closes a stream after the first bytes of a response that is twice its connection window, then checks that the next response is received completely.

An alternative is to send STOP_SENDING from close() (stream_shutdown of the read side), which also tells the peer to stop sending. It needs an application error code, which the transport does not know, and the receive side still has to be drained or tracked until the peer's FIN or RESET_STREAM arrives to remove the stream channel. This change keeps the current wire behaviour and only consumes what arrives.

Result:

Closing a QUIC stream before the end of its input no longer leaks connection flow control credit, stream credit and the stream channel.

  • Without the change to QuicheQuicStreamChannel, both new tests fail (server did not complete stream 0; received only 22336 bytes). With it, they pass.
  • All test classes of codec-native-quic pass with the change (30 classes, 284 tests, including QuicChannelConnectTest 84/84), run against the 4.2.18 osx-aarch_64 native library with -Dio.netty.leakDetection.level=paranoid, without any LEAK: report. Building codec-native-quic needs a local BoringSSL/quiche build, so these tests ran outside Maven (JUnit launcher on the module's test classes) against that released library; CI will run them in the normal build. Checkstyle passes for codec-native-quic; checkstyle and the API check pass for codec-classes-quic.

Prepared with Claude Code on behalf of allthingssecurity.

🤖 Generated with Claude Code

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
allthingssecurity and others added 6 commits October 4, 2026 09:08
@allthingssecurity @claude
Keep reading a closed QUIC stream until the end of its input
db8ce54
Motivation:

When a `QuicStreamChannel` is closed before the FIN of the remote peer was read, `QuicheQuicStreamChannel` never reads from the stream again: `recv()` only loops while the channel is active, and `readable()` only calls `recv()` when a read is pending. quiche keeps the unread data and the FIN, so:

- the data keeps its connection flow control credit. A client that closes streams while responses are still arriving (for example cancelled requests) uses up the connection window, and after that no stream of the connection receives data anymore.
- the stream is never completed and collected, so its stream credit is never returned to the remote peer. A server that closes streams before reading the request's FIN (for example after an early error response) stops the client from opening new streams once `initialMaxStreamsBidirectional` such streams were closed.
- the stream channel is never removed from the streams of its `QuicChannel`, as that requires the FIN to be read.

Modification:

- `recv()` reads and drops what is left once the channel is closed, until the FIN is received or the stream was reset.
- `readable()` calls `recv()` for a closed channel even if no read is pending, and `close()` drops what is left if the stream was readable already.
- Add `QuicStreamLimitTest.testStreamLimitRestoredWhenStreamClosedBeforeEndOfInput` and `QuicStreamChannelCloseTest.testCloseBeforeEndOfInputReturnsFlowControlCredit`.

Result:

Closing a QUIC stream before the end of its input no longer leaks flow control credit, stream credit and the stream channel.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@normanmaurer
Share StreamRecvResult handling between recv() and discardInput()
ff2fbf4
@normanmaurer
Flush window updates after dropping input in close()
68931f3
@normanmaurer
Add test for closing a QUIC stream outside of a read callback before …
87af430
…the end of its input
@normanmaurer
Fire exceptions that happen while dropping the input of a closed stream
ac66d7d
@normanmaurer
Don't fire ClosedChannelException when dropping the input of a closed…
d1988a9
… stream
@normanmaurer

normanmaurer commented Oct 15, 2026

Copy link
Copy Markdown
Member

@allthingssecurity PTAL... I pushed some changes that I think were missing, including a test.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer normanmaurer added this to the 4.2.20.Final milestone Oct 15, 2026
@normanmaurer normanmaurer added the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Oct 15, 2026
@allthingssecurity

allthingssecurity commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

@normanmaurer thanks a lot, the changes look right to me. I went through them on d1988a9:

  • handleRecvResult(...) shared by recv() and discardInput(...): same behaviour, less duplication.
  • close() only drops the input when not already in recv(). When it is, the loop ends because the channel is no longer active and discardInput(...) runs right after it. The flush is then done by whoever called recv(): the parent after processing the received packets, or beginRead(). Both already flush.
  • The extra connectionSendAndFlush() after the drop in close() is what I missed: a close from a timeout or another task otherwise never sends the window update. It is also safe when close() runs while the parent is processing packets or writable streams, because connectionSendAndFlush() returns early under inFireChannelReadCompleteQueue / IN_HANDLE_WRITABLE_STREAMS.
  • Firing what fails while dropping the input, except ClosedChannelException when the connection is already gone, makes sense to me.

I ran QuicStreamChannelCloseTest (28) and QuicStreamLimitTest (10) on this head on macOS aarch64, with paranoid leak detection and checkstyle: all pass, no leaks.

One observation, not a request: testCloseOutsideOfReadBeforeEndOfInputReturnsFlowControlCredit also passes when I put back my original version of QuicheQuicStreamChannel (db8ce54, without the flush in close()). So it covers the close outside a read callback, but not the missing window update itself. I guess some other send flushes the update in that test. If you want the flush pinned too, the test would need the peer to be idle after the close, with nothing else to send. I'm happy to try that, or to leave it as it is.

Claude Code on behalf of allthingssecurity

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented Oct 15, 2026

Copy link
Copy Markdown
Member

@allthingssecurity yeah I was not able to add a test for the missing flush as quiche might picky-pack these. That said I think it is a valid fix.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@testlens-app

testlens-app Bot commented Oct 15, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ Build PR / windows-x86_64-java11-boringssl

🏷️ Commit: d1988a9
▶️ Tests: 35105 executed
⚪️ Checks: 28/28 completed

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer
normanmaurer merged commit ff5a1b2 into netty:4.2 Oct 15, 2026
28 of 29 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot

netty-project-bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Could not create auto-port PR.
Got conflicts when cherry-picking onto 5.0.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@allthingssecurity
allthingssecurity deleted the fix-quic-stream-drain-input-after-close branch October 15, 2026 13:18
@normanmaurer

normanmaurer commented Oct 15, 2026

Copy link
Copy Markdown
Member

Port for 5.0: #17839

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer normanmaurer removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Oct 15, 2026
@normanmaurer normanmaurer mentioned this pull request Oct 16, 2026
adapt mem5 #17851
Closed
normanmaurer added a commit that referenced this pull request Oct 19, 2026
@normanmaurer
Fix QuicStreamChannelCloseTest port to 5.0
0994351
Motivation:

testCloseBeforeEndOfInputReturnsFlowControlCredit failed on 5.0 with
"received only 131072 bytes". The port translated the server's
QuicStreamChannel.SHUTDOWN_OUTPUT listener to
ctx.shutdown(ChannelShutdownType.newInbound()), so the server never sent
its FIN and the client never saw the end of the input.

Modification:

- Shut down the outbound side of the server stream after writing the response.
- Forward the received ChannelShutdownType in the client stream handler of
  testCloseOutsideOfReadBeforeEndOfInputReturnsFlowControlCredit, as the
  original forwarded the user event.

Result:

The 5.0 tests match the semantics of the original tests from #17755.
normanmaurer added a commit that referenced this pull request Oct 19, 2026
@normanmaurer @allthingssecurity
Port 5.0: Keep reading a closed QUIC stream until the end of its input (
f03e3ee
#17755) (#17839)

Motivation:

When a `QuicStreamChannel` is closed before the FIN of the remote peer
was read, `QuicheQuicStreamChannel` never reads from that stream again:

- `recv()` only loops `while (active && readPending && readable)`,
- `readable()` only calls `recv()` when a read is pending,
- `close()` sends our FIN but leaves the receive side alone.

quiche keeps the unread data and the FIN, which has three effects:

1. The data keeps its connection-level flow control credit, as quiche
only returns credit for data the application consumed. A client that
closes streams while responses are still arriving (cancelled requests, a
common case) uses up the connection window. After that, no stream of the
connection receives data anymore.
2. The stream is never completed and collected, so its stream credit is
never returned to the remote peer (no MAX_STREAMS update). A server that
closes streams before it read the request's FIN (for example after
writing an early error response) stops the client from opening new
streams once `initialMaxStreamsBidirectional` such streams were closed.
3. The closed stream channel is never removed from the streams of its
`QuicChannel`, as `removeStreamFromParent()` requires the FIN to be
read.

Reproduced on the real transport with the released 4.2.18 native library
(the JNI code is unchanged since):

- Server with `initialMaxStreamsBidirectional(2)` that closes each
stream on its first `channelRead`: the client completes 2 streams;
creating the 3rd fails with `QUICHE_ERR_STREAM_LIMIT`, and
`peerAllowedStreams(BIDIRECTIONAL)` stays 0. With a server that closes
after the FIN, all 6 streams complete.
- Client with `initialMaxData(256 KiB)` that closes each stream after
the first chunk of a 64 KiB response: after 4 such requests, the next
request gets no data at all and the connection idles out.

Modification:

- At the end of `recv()`, if the channel is closed, read and drop what
is left (`discardInput(...)`) until quiche reports no more data, the
FIN, or an error (stream reset or closed connection).
`removeStreamFromParent()` then removes the stream once the end of the
input was seen.
- `readable()` calls `recv()` for a closed channel even if no read is
pending, so data and the FIN that arrive after the close are dropped
too.
- `close()` drops what is left if the stream was readable already (when
`close()` is called from `channelRead(...)`, `recv()` does it after its
loop).
- Add

`QuicStreamLimitTest.testStreamLimitRestoredWhenStreamClosedBeforeEndOfInput`:
the server closes each stream on its first read, before the client's FIN
arrives; the client creates three streams in a row with a stream limit
of 1 and checks that the server returns the stream credit
(`QuicStreamLimitChangedEvent`) each time.
- Add

`QuicStreamChannelCloseTest.testCloseBeforeEndOfInputReturnsFlowControlCredit`:
the client closes a stream after the first bytes of a response that is
twice its connection window, then checks that the next response is
received completely.

An alternative is to send STOP_SENDING from `close()` (`stream_shutdown`
of the read side), which also tells the peer to stop sending. It needs
an application error code, which the transport does not know, and the
receive side still has to be drained or tracked until the peer's FIN or
RESET_STREAM arrives to remove the stream channel. This change keeps the
current wire behaviour and only consumes what arrives.

Result:

Closing a QUIC stream before the end of its input no longer leaks
connection flow control credit, stream credit and the stream channel.

---------

Co-authored-by: Norman Maurer <[email protected]>

---------

Co-authored-by: allthingssecurity <[email protected]>
@jbaldassari

jbaldassari commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

@allthingssecurity @normanmaurer thanks for this fix! Just wanted to confirm that it resolves a real production issue for us.

We run a QUIC-based proxy layer on netty's QUIC codec that multiplexes many short-lived streams over a single connection. A fraction of those streams are closed before the peer's FIN is read (canceled requests and early error responses, exactly the cases in your motivation). Over time this permanently exhausted the connection-level flow-control window (your point 1): max_data − data_sent reached zero with a healthy cwnd and ~0 loss, after which no stream on that connection received any more data, and it stayed wedged until torn down and re-established.

We built against a 4.2.20 snapshot with this change, and the leak is gone. Connections that previously wedged now sustain heavy load (150k+ streams/hr per host) with only transient, self-recovering back-pressure under genuine concurrent-stall conditions; no runaway accumulation. Looking forward to the official 4.2.20.Final release with this fix; thanks for the quick turnaround and the thorough write-up.

allthingssecurity reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@allthingssecurity

allthingssecurity commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

@allthingssecurity @normanmaurer thanks for this fix! Just wanted to confirm that it resolves a real production issue for us.

We run a QUIC-based proxy layer on netty's QUIC codec that multiplexes many short-lived streams over a single connection. A fraction of those streams are closed before the peer's FIN is read (canceled requests and early error responses, exactly the cases in your motivation). Over time this permanently exhausted the connection-level flow-control window (your point 1): max_data − data_sent reached zero with a healthy cwnd and ~0 loss, after which no stream on that connection received any more data, and it stayed wedged until torn down and re-established.

We built against a 4.2.20 snapshot with this change, and the leak is gone. Connections that previously wedged now sustain heavy load (150k+ streams/hr per host) with only transient, self-recovering back-pressure under genuine concurrent-stall conditions; no runaway accumulation. Looking forward to the official 4.2.20.Final release with this fix; thanks for the quick turnaround and the thorough write-up.

So good to hear this @jbaldassari .

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yawkat yawkat mentioned this pull request Nov 24, 2026
Do not fire the reset of a closed QUIC stream #18529
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.2.20.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@allthingssecurity @normanmaurer @netty-project-bot @jbaldassari
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.