GeezerVison is showing github.com — NullPointerException in `PlatformDependent0.safeConstructPutInt` with Netty 4.2.2 and GraalVM Native Image on JDK25+ · Issue #15493 · netty/netty
NullPointerException in PlatformDependent0.safeConstructPutInt with Netty 4.2.2 and GraalVM Native Image on JDK25+ #15493DescriptionCompiling an app running a netty server using Native Image results in a NullPointerException in PlatformDependent0 when the server receives any data.
Sample stack trace:
java.lang.NullPointerException
at io.netty.util.internal.PlatformDependent0.safeConstructPutInt(PlatformDependent0.java:681)
at io.netty.util.internal.PlatformDependent.safeConstructPutInt(PlatformDependent.java:569)
at io.netty.util.internal.ReferenceCountUpdater.setInitialValue(ReferenceCountUpdater.java:67)
at io.netty.buffer.AdaptivePoolingAllocator$Chunk.<init>(AdaptivePoolingAllocator.java:884)
at io.netty.buffer.AdaptivePoolingAllocator$Magazine.newChunkAllocation(AdaptivePoolingAllocator.java:805)
at io.netty.buffer.AdaptivePoolingAllocator$Magazine.allocate(AdaptivePoolingAllocator.java:703)
at io.netty.buffer.AdaptivePoolingAllocator$Magazine.tryAllocate(AdaptivePoolingAllocator.java:574)
at io.netty.buffer.AdaptivePoolingAllocator.allocate(AdaptivePoolingAllocator.java:230)
at io.netty.buffer.AdaptivePoolingAllocator.allocate(AdaptivePoolingAllocator.java:217)
at io.netty.buffer.AdaptiveByteBufAllocator.newDirectBuffer(AdaptiveByteBufAllocator.java:70)
at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:188)
at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:179)
at io.netty.buffer.AbstractByteBufAllocator.ioBuffer(AbstractByteBufAllocator.java:140)
at io.netty.channel.DefaultMaxMessagesRecvByteBufAllocator$MaxMessageHandle.allocate(DefaultMaxMessagesRecvByteBufAllocator.java:120)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.handle(AbstractNioChannel.java:445)
at io.netty.channel.nio.NioIoHandler$DefaultNioRegistration.handle(NioIoHandler.java:381)
at io.netty.channel.nio.NioIoHandler.processSelectedKey(NioIoHandler.java:575)
at io.netty.channel.nio.NioIoHandler.processSelectedKeysPlain(NioIoHandler.java:520)
at io.netty.channel.nio.NioIoHandler.processSelectedKeys(NioIoHandler.java:493)
at io.netty.channel.nio.NioIoHandler.run(NioIoHandler.java:468)
at io.netty.channel.SingleThreadIoEventLoop.runIo(SingleThreadIoEventLoop.java:206)
at io.netty.channel.SingleThreadIoEventLoop.run(SingleThreadIoEventLoop.java:177)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:1073)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base@26/java.lang.Thread.runWith(Thread.java:1487)
at java.base@26/java.lang.Thread.run(Thread.java:1474)
at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:832)
at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:808)
Since version 4.2 (#15231, #15338), Netty doesn't use Unsafe anymore for JDK25+. However, the NPE is thrown in the unsafe code because the Unsafe instance is null, which should be impossible.
I didn't manage to build netty from source to confirm this but my best guess is that this happens due to AdaptivePoolingAllocatorSubstitution:
ReferenceCountUpdater.setInitialValue checks unsafeOffset() to determine whether unsafe should be used (in this case it shouldn't be used, but the offset is still no -1)
unsafeOffset() in AdaptivePoolingAllocator$Chunk.updater returns AdaptivePoolingAllocator.Chunk.REFCNT_FIELD_OFFSET
- This is usually computed using
ReferenceCountUpdater.getUnsafeOffset, which would return -1 because unsafe is not available
- However, on native image the
AdaptivePoolingAllocatorSubstitution class recomputes the field value of REFCNT_FIELD_OFFSET to use the offset as provided by native image (which is never -1), effectively making the code think unsafe is available here
Activityfniephaus commented on Jul 15, 2025 (Disclaimer: Patrick and I work on the GraalVM team)
#14928 introduced a dependency on org.graalvm.nativeimage:svm to implement the AdaptivePoolingAllocatorSubstitution. This dependency is considered internal API and we highly discourage the use of substitutions as they can cause issues like this one.
It would be good to find a way to make this work without the substitution. yawkat commented on Jul 15, 2025 patrick96 commented on Jul 15, 2025 Oh wow, netty 4.2.3 came out 18 hours ago and I just didn't see it because it wasn't yet up on mvnrepository.
I can confirm, this no longer happens with 4.2.3 Reacted by Jonas Konrad, Fabio Niephaus and Chris Vest fniephaus commented on Jul 15, 2025 Nice, thank you @yawkat. Do you think there is any way the substitution can be avoided altogether? yawkat commented on Jul 15, 2025 It's needed for unsafe, which is faster than the modern alternatives, so I don't see how 🤷
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|