Skip to content

Guard ChunkedWriteHandler.doFlush() against re-entrant invocation from user code - #17438

Merged
normanmaurer merged 1 commit into
netty:4.2from
fudianchn:fix-chunkedwritehandler-reentrancy
Sep 16, 2026
Merged

normanmaurer merged 1 commit into
netty:4.2from
fudianchn:fix-chunkedwritehandler-reentrancy

Conversation

@fudianchn

@fudianchn fudianchn commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

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() (#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 HTTP/2: Prevent reentrant flush on writability change #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 (Call ChunkedWriteHandler.flush() once again if tried when locked. #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.

…m user code

ChunkedInput.readChunk(...) and isEndOfInput() run between queue.peek()
and queue.remove() in doFlush(...). User code executed there can
synchronously re-enter the handler via flush(), resumeTransfer(),
channelInactive(...) or channelWritabilityChanged(...). The nested
doFlush(...) peeked and consumed the same queue entry again, which made
the outer invocation fail with NoSuchElementException from
ArrayDeque.remove() and let both invocations read from the same (already
closed) input.

A nested invocation is remembered instead of dropped: the outer
invocation re-runs the drain loop before returning, so a
resumeTransfer(...) arriving while it is about to suspend is not lost
(same concern as the old lock-based guard and its flushRequired
follow-up in netty#637).

Fixes netty#14839

Signed-off-by: 付典 <[email protected]>
@testlens-app

testlens-app Bot commented Sep 12, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: ea1d860
▶️ Tests: 432786 executed
⚪️ Checks: 29/29 completed


Learn more about TestLens at testlens.app/docs.

@normanmaurer normanmaurer added this to the 4.2.19.Final milestone Sep 16, 2026
@normanmaurer normanmaurer added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Sep 16, 2026
@normanmaurer

Copy link
Copy Markdown
Member

@fedinskiy this looks correct! I am not sure how often someone would call into the channel or into the handler from readChunk but we need to handle it.

@normanmaurer
normanmaurer merged commit 3061561 into netty:4.2 Sep 16, 2026
30 checks passed
@fedinskiy

Copy link
Copy Markdown
Contributor

@normanmaurer I presume you wanted to tag @fudianchn :)

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #17501

@netty-project-bot

Copy link
Copy Markdown
Contributor

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

@normanmaurer

Copy link
Copy Markdown
Member

Pr for 5.0: #17503

normanmaurer pushed a commit that referenced this pull request Sep 16, 2026
… invocation from user code (#17501)

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 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]>
Co-authored-by: fudian <[email protected]>
normanmaurer added a commit that referenced this pull request Sep 17, 2026
…on from user code (#17438) (#17503)

`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`).

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.

Fixes #14839.

Signed-off-by: 付典 <[email protected]>

Signed-off-by: 付典 <[email protected]>
Co-authored-by: fudian <[email protected]>
@normanmaurer normanmaurer mentioned this pull request Oct 1, 2026
@normanmaurer normanmaurer removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Oct 16, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Netty is throwing NoSuchElementException on 4.1.118.Final during AsyncHttpClient calls.

4 participants