Repository navigation
Auto-port 4.1: Guard ChunkedWriteHandler.doFlush() against re-entrant invocation from user code - #17501
Merged
Conversation
…m user code (#17438) ## Motivation `ChunkedWriteHandler.doFlush(...)` intermittently fails with `NoSuchElementException` from `ArrayDeque.remove()` (#14839). Root cause: `queue.peek()` and the `queue.remove()` calls are separated by user code. `ChunkedInput.readChunk(...)` and `isEndOfInput()` run between them, and that user code can synchronously re-enter the handler — an incidental `flush()`, a `resumeTransfer()`, a writability change, or a close notification. The nested `doFlush(...)` peeks and consumes the *same* queue entry: 1. the outer invocation then calls `queue.remove()` on an already-empty queue → `NoSuchElementException`, 2. both invocations read from the same input — including after the nested one closed it, 3. the double consumption leaks a chunk buffer (caught by leak detection in the regression test). A deterministic reproducer on 4.1.119 matches the stack reported on 4.1.118 frame by frame (`doFlush:287`, `flush:146`). ## Modification 1. Guard `doFlush(...)` against re-entrant invocation: an `inFlush` flag, cleared in `finally` — same shape as the re-entrancy guard added to `Http2ConnectionHandler.flush(...)` in #17266. 2. A nested call is remembered (`flushPending`), not dropped: the outer invocation re-runs the drain loop before returning. A plain `return` guard would lose a `resumeTransfer(...)` that arrives while the transfer is about to suspend — the same lost-resume problem the old lock-based guard addressed with `flushRequired` (#637). 3. Two regression tests in `ChunkedWriteHandlerTest`: - re-entrant `flush()` from `readChunk(...)`: fails on master with the reported `NoSuchElementException` and a leak-detection error, passes with the guard; asserts the input is read exactly twice, closed exactly once after the last read, both chunks written once in order, and the promise succeeds, - `resumeTransfer()` from a suspending `readChunk(...)`: passes on master (where the nested call does the work but the outer `NoSuchElementException` is not surfaced to the caller) and on the guard, fails on a plain-`return` variant of the guard (transfer hangs) — it pins the no-lost-resume property. ## Result Fixes #14839. Signed-off-by: 付典 <[email protected]> (cherry picked from commit 3061561)
🚨 TestLens detected 1 failed test 🚨Here is what you can do:
Test SummaryBuild PR / linux-x86_64-java21 build > Netty/Transport/Native/Epoll
🏷️ Commit: d01b095 Test FailuresEpollLTSocketHalfClosedTest > testHalfClosureReceiveDataOnFinalWait2StateWhenSoLingerSet(TestInfo) (Netty/Transport/Native/Epoll in Build PR / linux-x86_64-java21 build)Rerun ControlsSelect tests to mute in this pull request:
Reuse successful test results:
Click the checkbox to trigger a rerun:
Learn more about TestLens at testlens.app/docs. |
lltopk
pushed a commit
to lltopk/netty
that referenced
this pull request
Oct 24, 2026
… invocation from user code (netty#17501) Auto-port of netty#17438 to 4.1 Cherry-picked commit: 3061561 --- AI disclosure: this change was prepared with AI coding agents, reviewed and revised line by line by me. ## Motivation `ChunkedWriteHandler.doFlush(...)` intermittently fails with `NoSuchElementException` from `ArrayDeque.remove()` (netty#14839). Root cause: `queue.peek()` and the `queue.remove()` calls are separated by user code. `ChunkedInput.readChunk(...)` and `isEndOfInput()` run between them, and that user code can synchronously re-enter the handler — an incidental `flush()`, a `resumeTransfer()`, a writability change, or a close notification. The nested `doFlush(...)` peeks and consumes the *same* queue entry: 1. the outer invocation then calls `queue.remove()` on an already-empty queue → `NoSuchElementException`, 2. both invocations read from the same input — including after the nested one closed it, 3. the double consumption leaks a chunk buffer (caught by leak detection in the regression test). A deterministic reproducer on 4.1.119 matches the stack reported on 4.1.118 frame by frame (`doFlush:287`, `flush:146`). ## Modification 1. Guard `doFlush(...)` against re-entrant invocation: an `inFlush` flag, cleared in `finally` — same shape as the re-entrancy guard added to `Http2ConnectionHandler.flush(...)` in netty#17266. 2. A nested call is remembered (`flushPending`), not dropped: the outer invocation re-runs the drain loop before returning. A plain `return` guard would lose a `resumeTransfer(...)` that arrives while the transfer is about to suspend — the same lost-resume problem the old lock-based guard addressed with `flushRequired` (netty#637). 3. Two regression tests in `ChunkedWriteHandlerTest`: - re-entrant `flush()` from `readChunk(...)`: fails on master with the reported `NoSuchElementException` and a leak-detection error, passes with the guard; asserts the input is read exactly twice, closed exactly once after the last read, both chunks written once in order, and the promise succeeds, - `resumeTransfer()` from a suspending `readChunk(...)`: passes on master (where the nested call does the work but the outer `NoSuchElementException` is not surfaced to the caller) and on the guard, fails on a plain-`return` variant of the guard (transfer hangs) — it pins the no-lost-resume property. ## Result Fixes netty#14839. Signed-off-by: 付典 <[email protected]> Co-authored-by: fudian <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Auto-port of #17438 to 4.1
Cherry-picked commit: 3061561
AI disclosure: this change was prepared with AI coding agents, reviewed and revised line by line by me.
Motivation
ChunkedWriteHandler.doFlush(...)intermittently fails withNoSuchElementExceptionfromArrayDeque.remove()(#14839).Root cause:
queue.peek()and thequeue.remove()calls are separated by user code.ChunkedInput.readChunk(...)andisEndOfInput()run between them, and that user code can synchronously re-enter the handler — an incidentalflush(), aresumeTransfer(), a writability change, or a close notification. The nesteddoFlush(...)peeks and consumes the same queue entry:queue.remove()on an already-empty queue →NoSuchElementException,A deterministic reproducer on 4.1.119 matches the stack reported on 4.1.118 frame by frame (
doFlush:287,flush:146).Modification
doFlush(...)against re-entrant invocation: aninFlushflag, cleared infinally— same shape as the re-entrancy guard added toHttp2ConnectionHandler.flush(...)in HTTP/2: Prevent reentrant flush on writability change #17266.flushPending), not dropped: the outer invocation re-runs the drain loop before returning. A plainreturnguard would lose aresumeTransfer(...)that arrives while the transfer is about to suspend — the same lost-resume problem the old lock-based guard addressed withflushRequired(Call ChunkedWriteHandler.flush() once again if tried when locked. #637).ChunkedWriteHandlerTest:flush()fromreadChunk(...): fails on master with the reportedNoSuchElementExceptionand a leak-detection error, passes with the guard; asserts the input is read exactly twice, closed exactly once after the last read, both chunks written once in order, and the promise succeeds,resumeTransfer()from a suspendingreadChunk(...): passes on master (where the nested call does the work but the outerNoSuchElementExceptionis not surfaced to the caller) and on the guard, fails on a plain-returnvariant of the guard (transfer hangs) — it pins the no-lost-resume property.Result
Fixes #14839.