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 — Call ChunkedWriteHandler.flush() once again if tried when locked. by losipiuk · Pull Request #637 · netty/netty

Conversation

@losipiuk

@losipiuk losipiuk commented Oct 2, 2012

Copy link
Copy Markdown

Force recursive call of ChunkedWriteHandler.flush() if another thread
tried to call it when lock was held.

Solves problem of lost resumeTransfer() call.

Problem arises if one thread is calling flush() and gets null from ChunkedInput in line 222 (in original file)
while endOfInput is false.

ChunkedWriteHandler gets into suspend mode in such case but it is possible that resumeTransfer() call
arrives before thread releases lock. In such case transfer never resumes, unless application logic calls resumeTransfer() again, which may never happen.

I added volatile flushRequired field to handle this case. The issue with the fix is that this variable does not take into account the value of fireNow parameter of flush method. Therefore recursive call to flush(), resulting from set flushRequired field may be done with different value of fireNow parameter than originally intended.

I leave it this way, as I am not sure what is the semantics of this parameter, and more complicated solution would probably be broken anyway. Let me know what you think :)

Sorry, something went wrong.

Uh oh!

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

All reactions
Lukasz Osipiuk
Call ChunkedWriteHandler.flush() once again if tried when locked.
20509fb
Force recursive call of ChunkedWriteHandler.flush() if another thread
tried to call it when lock was held.
Solves problem of lost resumeTransfer() call.
normanmaurer added a commit that referenced this pull request Oct 4, 2012
@normanmaurer
Merge pull request #637 from losipiuk/3
4bca917
Call ChunkedWriteHandler.flush() once again if tried when locked.
@normanmaurer
normanmaurer merged commit 4bca917 into netty:3 Oct 4, 2012
@ghost ghost assigned normanmaurer Oct 4, 2012
@normanmaurer normanmaurer mentioned this pull request Apr 1, 2013
Patch 1 #1219
Closed
@normanmaurer normanmaurer mentioned this pull request Sep 19, 2013
3 #1854
Closed
chrisvest pushed a commit to chrisvest/netty that referenced this pull request Apr 2, 2025
@normanmaurer
Update netty to 4.1.103.Final (netty#637)
4af24a6
@fudianchn fudianchn mentioned this pull request Sep 12, 2026
Guard ChunkedWriteHandler.doFlush() against re-entrant invocation from user code #17438
Merged
normanmaurer pushed a commit that referenced this pull request Sep 16, 2026
@fudianchn
Guard ChunkedWriteHandler.doFlush() against re-entrant invocation fro…
3061561
…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]>
@netty-project-bot netty-project-bot mentioned this pull request Sep 16, 2026
Auto-port 4.1: Guard ChunkedWriteHandler.doFlush() against re-entrant invocation from user code #17501
Merged
@normanmaurer normanmaurer mentioned this pull request Sep 16, 2026
Port: Guard ChunkedWriteHandler.doFlush() against re-entrant invocation from user code (#17438) #17503
Merged
normanmaurer pushed a commit that referenced this pull request Sep 16, 2026
@netty-project-bot @fudianchn
Auto-port 4.1: Guard ChunkedWriteHandler.doFlush() against re-entrant…
2550ea3
… 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
@normanmaurer @fudianchn
Port: Guard ChunkedWriteHandler.doFlush() against re-entrant invocati…
3ac1c84
…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]>
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
@netty-project-bot @fudianchn @lltopk
Auto-port 4.1: Guard ChunkedWriteHandler.doFlush() against re-entrant…
e3029a1
… 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

Reviewers

No reviews

Uh oh!

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

Assignees

@normanmaurer normanmaurer

Labels

None yet

Projects

None yet

Milestone

3.5.9.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

2 participants

@losipiuk @normanmaurer
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.