Skip to content

Fix NPE in AbstractNioChannel.removeReadOp() after concurrent deregistration - #17104

Merged
normanmaurer merged 3 commits into
netty:4.2from
seonwooj0810:fix/17103-removereadop-npe-after-deregister
Aug 9, 2026
Merged

normanmaurer merged 3 commits into
netty:4.2from
seonwooj0810:fix/17103-removereadop-npe-after-deregister

Conversation

@seonwooj0810

Copy link
Copy Markdown
Contributor

Fixes #17103

Motivation

clearReadPending() guards scheduling of clearReadPendingRunnable with an isRegistered() check, which reads the registered boolean of AbstractChannel. That boolean and the IoRegistration registration field of AbstractNioChannel are not updated atomically. When a channel is deregistered from another thread after the isRegistered() check passed but before the runnable runs on the EventLoop, doDeregister() has already set registration to null. removeReadOp() then obtained the field via registration() (which only asserts non-null) and dereferenced it, producing the reported NullPointerException in production (or an AssertionError when assertions are enabled):

java.lang.NullPointerException
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.removeReadOp(AbstractNioChannel.java:268)
    at io.netty.channel.nio.AbstractNioChannel.clearReadPending0(AbstractNioChannel.java:223)
    at io.netty.channel.nio.AbstractNioChannel$1.run(AbstractNioChannel.java:66)
    ...

Modification

Make removeReadOp() read the registration field directly and null-check it before use, mirroring the null-safe idiom already used by doBeginRead() in the same class. Behaviour is unchanged when the channel is still registered.

Result

removeReadOp() no longer throws when the channel has been deregistered concurrently.

Added AbstractNioChannelTest#testRemoveReadOpDoesNotThrowAfterDeregister, which registers a NioSocketChannel, deregisters it (clearing the registration), then invokes removeReadOp(). The test fails on the current code (NPE / AssertionError at AbstractNioChannel.java:264 → registration() at :156) and passes with this change.

Verification done: built transport (+ deps) and ran the new regression test (fails before / passes after the fix); ran the existing NioEventLoopTest (13 tests, all green); ran the project checkstyle execution (checkstyle:check@check-style, no violations).

…tration

Motivation:

clearReadPending() guards the scheduling of clearReadPendingRunnable with an
isRegistered() check, which reads the "registered" boolean of AbstractChannel.
That boolean and the IoRegistration field of AbstractNioChannel are not updated
atomically together. When a channel is deregistered from a different thread after
the isRegistered() check passed but before the runnable executes on the EventLoop,
doDeregister() has already set the registration field to null. removeReadOp() then
called registration() (which returns the field after an assert) and dereferenced it,
producing a NullPointerException (or an AssertionError when assertions are enabled):

    java.lang.NullPointerException
        at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.removeReadOp(AbstractNioChannel.java:268)
        at io.netty.channel.nio.AbstractNioChannel.clearReadPending0(AbstractNioChannel.java:223)
        at io.netty.channel.nio.AbstractNioChannel$1.run(AbstractNioChannel.java:66)
        ...

Modification:

Make removeReadOp() read the registration field directly and null-check it before
use, mirroring the idiom already used by doBeginRead() in the same class.

Result:

removeReadOp() no longer throws when the channel has been deregistered concurrently.
Fixes netty#17103.
@normanmaurer normanmaurer added this to the 4.2.17.Final milestone Jul 18, 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 Jul 18, 2026
@seonwooj0810

Copy link
Copy Markdown
Contributor Author

Thanks for the approval, @normanmaurer. Is anything still blocking the merge here, or is it just waiting on a batch? Happy to rebase if that would help.

@testlens-app

testlens-app Bot commented Aug 3, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 271921f
▶️ Tests: 0 executed
⚪️ Checks: 18/18 completed


Learn more about TestLens at testlens.app.

@normanmaurer

Copy link
Copy Markdown
Member

@seonwooj0810

Copy link
Copy Markdown
Contributor Author

@normanmaurer Yes — the ICLA for this account (seonwooj0810) has been submitted via https://netty.io/s/icla. If there is a processing delay on your end, please let me know and I can confirm the submission details.

Also, thank you both to @normanmaurer and @chrisvest for the approvals on this PR!

@chrisvest

Copy link
Copy Markdown
Member

@seonwooj0810 The processing should be instant, but I don't see your github username or your email. Did the form properly submit?

@normanmaurer

Copy link
Copy Markdown
Member

@seonwooj0810 please do again... it was not stored for whatever reason

@chrisvest chrisvest modified the milestones: 4.2.17.Final, 4.2.18.Final Aug 4, 2026
@seonwooj0810

Copy link
Copy Markdown
Contributor Author

Thank you both for checking — sorry for the confusion.

@chrisvest @normanmaurer The form did not store properly — I apologize for the failed submission. I will re-submit the ICLA at https://netty.io/s/icla immediately.

Once it registers, please let me know if anything else is needed on the PR side.

@chrisvest

Copy link
Copy Markdown
Member

@seonwooj0810 I'm still not seeing it registered, but others have successfully signed in the mean time, so the form works. Maybe try a different browser, or try disabling content blockers or the like.

@seonwooj0810

Copy link
Copy Markdown
Contributor Author

Thank you for checking again, @chrisvest. I apologize for the repeated inconvenience.

I will try the form again using a different browser as you suggested. If the submission is still not recorded after that, I will reach out separately to confirm the account details are correct.

I will post an update here once the ICLA shows up on your end.

@seonwooj0810

Copy link
Copy Markdown
Contributor Author

Just resubmitted the ICLA form again (tried a different browser this time as suggested). Please let me know if it's still not showing up on your end.

@normanmaurer

Copy link
Copy Markdown
Member

@seonwooj0810 this worked

@normanmaurer normanmaurer removed the needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. label Aug 9, 2026
@normanmaurer
normanmaurer merged commit f8e1152 into netty:4.2 Aug 9, 2026
34 of 35 checks passed
@netty-project-bot

Copy link
Copy Markdown
Contributor

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

normanmaurer added a commit that referenced this pull request Aug 10, 2026
…tration (#17104) (#17248)

Fixes #17103

`clearReadPending()` guards scheduling of `clearReadPendingRunnable`
with an `isRegistered()` check, which reads the `registered` boolean of
`AbstractChannel`. That boolean and the `IoRegistration registration`
field of `AbstractNioChannel` are not updated atomically. When a channel
is deregistered from another thread *after* the `isRegistered()` check
passed but *before* the runnable runs on the `EventLoop`,
`doDeregister()` has already set `registration` to `null`.
`removeReadOp()` then obtained the field via `registration()` (which
only `assert`s non-null) and dereferenced it, producing the reported
`NullPointerException` in production (or an `AssertionError` when
assertions are enabled):

```
java.lang.NullPointerException
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.removeReadOp(AbstractNioChannel.java:268)
    at io.netty.channel.nio.AbstractNioChannel.clearReadPending0(AbstractNioChannel.java:223)
    at io.netty.channel.nio.AbstractNioChannel$1.run(AbstractNioChannel.java:66)
    ...
```

Make `removeReadOp()` read the `registration` field directly and
null-check it before use, mirroring the null-safe idiom already used by
`doBeginRead()` in the same class. Behaviour is unchanged when the
channel is still registered.

`removeReadOp()` no longer throws when the channel has been deregistered
concurrently.

Added
`AbstractNioChannelTest#testRemoveReadOpDoesNotThrowAfterDeregister`,
which registers a `NioSocketChannel`, deregisters it (clearing the
registration), then invokes `removeReadOp()`. The test fails on the
current code (NPE / AssertionError at `AbstractNioChannel.java:264` →
`registration()` at `:156`) and passes with this change.

Verification done: built `transport` (+ deps) and ran the new regression
test (fails before / passes after the fix); ran the existing
`NioEventLoopTest` (13 tests, all green); ran the project checkstyle
execution (`checkstyle:check@check-style`, no violations).

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

Co-authored-by: seonwoojung <[email protected]>
@chrisvest chrisvest removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Sep 28, 2026
@normanmaurer normanmaurer mentioned this pull request Oct 1, 2026
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.

NPE when clearing read pending status of a NIO channel

4 participants