Skip to content

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

Merged
normanmaurer merged 1 commit into
5.0from
nio_npe5
Aug 10, 2026
Merged

normanmaurer merged 1 commit into
5.0from
nio_npe5

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

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

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]

…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-app

testlens-app Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

🚨 TestLens detected 2 failed tests 🚨

Here is what you can do:

  1. Inspect the test failures carefully.
  2. If you are convinced that some of the tests are flaky, you can mute them below.
  3. Finally, trigger a rerun by checking the rerun checkbox.

Test Summary

Build PR / windows-x86_64-java25-boringssl > Netty/Build/Jextract Maven Plugin

Test Runs Flakiness
JextractLocatorTest > throwsWhenExplicitPathIsNotExecutable() ❌ 0% 🟢
JextractLocatorTest > throwsWhenJextractEnvIsNotExecutable() ❌ 0% 🟢

🏷️ Commit: 696d9dd
▶️ Tests: 227601 executed
⚪️ Checks: 12/12 completed

Test Failures

JextractLocatorTest > throwsWhenExplicitPathIsNotExecutable() (Netty/Build/Jextract Maven Plugin in Build PR / windows-x86_64-java25-boringssl)
org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:31)
	at org.junit.jupiter.api.Assertions.assertTrue(Assertions.java:183)
	at io.netty.build.jextract.JextractLocatorTest.throwsWhenExplicitPathIsNotExecutable(JextractLocatorTest.java:113)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
expected actual
true false
JextractLocatorTest > throwsWhenJextractEnvIsNotExecutable() (Netty/Build/Jextract Maven Plugin in Build PR / windows-x86_64-java25-boringssl)
org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:31)
	at org.junit.jupiter.api.Assertions.assertTrue(Assertions.java:183)
	at io.netty.build.jextract.JextractLocatorTest.throwsWhenJextractEnvIsNotExecutable(JextractLocatorTest.java:127)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
expected actual
true false

Muted Tests

Select tests to mute in this pull request:

  • JextractLocatorTest > throwsWhenExplicitPathIsNotExecutable()
  • JextractLocatorTest > throwsWhenJextractEnvIsNotExecutable()

Reuse successful test results:

  • ♻️ Only rerun the tests that failed or were muted before

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app.

@normanmaurer normanmaurer added this to the 5.0.0.Final milestone Aug 10, 2026
@normanmaurer
normanmaurer merged commit 4da8e29 into 5.0 Aug 10, 2026
12 of 13 checks passed
@normanmaurer
normanmaurer deleted the nio_npe5 branch August 10, 2026 07:39
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.

2 participants