Repository navigation
Fix NPE in AbstractNioChannel.removeReadOp() after concurrent deregistration (#17104) - #17248
Merged
Merged
Conversation
…tration (#17104) 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]>
🚨 TestLens detected 2 failed tests 🚨Here is what you can do:
Test SummaryBuild PR / windows-x86_64-java25-boringssl > Netty/Build/Jextract Maven Plugin
🏷️ Commit: 696d9dd Test FailuresJextractLocatorTest > throwsWhenExplicitPathIsNotExecutable() (Netty/Build/Jextract Maven Plugin in Build PR / windows-x86_64-java25-boringssl)
JextractLocatorTest > throwsWhenJextractEnvIsNotExecutable() (Netty/Build/Jextract Maven Plugin in Build PR / windows-x86_64-java25-boringssl)
Muted TestsSelect 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. |
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.
Fixes #17103
clearReadPending()guards scheduling ofclearReadPendingRunnablewith an
isRegistered()check, which reads theregisteredboolean ofAbstractChannel. That boolean and theIoRegistration registrationfield of
AbstractNioChannelare not updated atomically. When a channelis deregistered from another thread after the
isRegistered()checkpassed but before the runnable runs on the
EventLoop,doDeregister()has already setregistrationtonull.removeReadOp()then obtained the field viaregistration()(whichonly
asserts non-null) and dereferenced it, producing the reportedNullPointerExceptionin production (or anAssertionErrorwhenassertions are enabled):
Make
removeReadOp()read theregistrationfield directly andnull-check it before use, mirroring the null-safe idiom already used by
doBeginRead()in the same class. Behaviour is unchanged when thechannel is still registered.
removeReadOp()no longer throws when the channel has been deregisteredconcurrently.
Added
AbstractNioChannelTest#testRemoveReadOpDoesNotThrowAfterDeregister,which registers a
NioSocketChannel, deregisters it (clearing theregistration), then invokes
removeReadOp(). The test fails on thecurrent code (NPE / AssertionError at
AbstractNioChannel.java:264→registration()at:156) and passes with this change.Verification done: built
transport(+ deps) and ran the new regressiontest (fails before / passes after the fix); ran the existing
NioEventLoopTest(13 tests, all green); ran the project checkstyleexecution (
checkstyle:check@check-style, no violations).Co-authored-by: Norman Maurer [email protected]