Skip to content

Commit ca155ca

Browse files
committed
Re-initialize platform dependent netty classes/values at runtime
Platform dependent classes should not be initialized at build time to avoid issues when running the native executable on a different platform. This patch registers two platform dependent classes for runtime re-initialization and disables the use of `Unsafe` in netty. Unfortunately when re-initializing `PlatformDependent0` it fails to properly setup unsafe accesses resulting in `NullPointerExceptions` when invoking `putByte`. Disabling unsafe accesses for netty works around this. The above changes result in Quarkus native applications to respect arguments like `-Dio.netty.maxDirectMemory=1024` and `-XX:MaxDirectMemorySize=1g`. Fixes quarkusio#17839
1 parent ca96ab2 commit ca155ca

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

extensions/netty/deployment/src/main/java/io/quarkus/netty/deployment/NettyProcessor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ NativeImageConfigBuildItem build(
9393
String maxOrder = calculateMaxOrder(config.allocatorMaxOrder, minMaxOrderBuildItems, false);
9494

9595
NativeImageConfigBuildItem.Builder builder = NativeImageConfigBuildItem.builder()
96-
//.addNativeImageSystemProperty("io.netty.noUnsafe", "true")
96+
// disable unsafe usage to allow io.netty.internal.PlarformDependent0 to be reinitialized without issues
97+
.addNativeImageSystemProperty("io.netty.noUnsafe", "true")
9798
// Use small chunks to avoid a lot of wasted space. Default is 16mb * arenas (derived from core count)
9899
// Since buffers are cached to threads, the malloc overhead is temporary anyway
99100
.addNativeImageSystemProperty("io.netty.allocator.maxOrder", maxOrder)
@@ -109,6 +110,9 @@ NativeImageConfigBuildItem build(
109110
.addRuntimeInitializedClass("io.netty.buffer.ByteBufUtil")
110111
// The default channel id uses the process id, it should not be cached in the native image.
111112
.addRuntimeInitializedClass("io.netty.channel.DefaultChannelId")
113+
// Make sure to re-initialize platform dependent classes/values at runtime
114+
.addRuntimeReinitializedClass("io.netty.util.internal.PlatformDependent")
115+
.addRuntimeReinitializedClass("io.netty.util.internal.PlatformDependent0")
112116
.addNativeImageSystemProperty("io.netty.leakDetection.level", "DISABLED");
113117

114118
if (QuarkusClassLoader.isClassPresentAtRuntime("io.netty.handler.codec.http.HttpObjectEncoder")) {

0 commit comments

Comments
 (0)