Repository navigation
Use VarHandle JCTools queues without Unsafe - #17185
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates Netty’s internal queue selection logic to take advantage of JCTools’ VarHandle-based queues when Unsafe is unavailable, by adding/shading the jctools-core-jdk11 artifact and extending PlatformDependent queue factories accordingly.
Changes:
- Add
org.jctools:jctools-core-jdk11:4.0.6to dependency management and tonetty-commonfor shading. - Update
PlatformDependentqueue factories to prefer Unsafe-based queues, then VarHandle-based queues, then atomic queues. - Add a unit test intended to validate VarHandle queue selection when
Unsafeis disabled.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pom.xml | Adds jctools-core-jdk11 version-managed dependency. |
| common/pom.xml | Adds jctools-core-jdk11 as a shaded compile-scope dependency. |
| common/src/main/java/io/netty/util/internal/PlatformDependent.java | Adds VarHandle queue imports and updates queue factory selection logic. |
| common/src/test/java/io/netty/util/internal/PlatformDependentTest.java | Adds test for VarHandle-queue selection when Unsafe is unavailable. |
Suppressed comments (5)
common/src/main/java/io/netty/util/internal/PlatformDependent.java:1300
- Same issue as above: hasVarHandle() is false whenever isUnaligned() is true (initializeVarHandle() returns false early), so this will fall back to the atomic queue implementation on those runtimes even when Unsafe is disabled. Consider checking VarHandle support for queues independently of the VAR_HANDLE flag used for multi-byte access.
return hasVarHandle() ? new MpscUnboundedVarHandleArrayQueue<T>(MPSC_CHUNK_SIZE)
: new MpscUnboundedAtomicArrayQueue<T>(MPSC_CHUNK_SIZE);
common/src/main/java/io/netty/util/internal/PlatformDependent.java:1349
- This uses PlatformDependent.hasVarHandle(), but hasVarHandle() is false whenever isUnaligned() is true (initializeVarHandle() returns false early). That means VarHandle-based queues won’t be selected on those runtimes when Unsafe is disabled. Consider checking queue VarHandle support independently of the VAR_HANDLE flag.
return hasVarHandle() ? new MpscVarHandleArrayQueue<T>(capacity) : new MpscAtomicArrayQueue<T>(capacity);
common/src/main/java/io/netty/util/internal/PlatformDependent.java:1338
- This uses PlatformDependent.hasVarHandle(), but that flag is disabled when isUnaligned() is true (initializeVarHandle() returns false), which can prevent selecting the VarHandle-based queue implementation even when Unsafe is unavailable. Consider using a VarHandle-queue-specific support check instead of hasVarHandle().
return hasVarHandle() ? new SpscLinkedVarHandleQueue<T>() : new SpscLinkedAtomicQueue<T>();
common/src/main/java/io/netty/util/internal/PlatformDependent.java:1362
- This uses PlatformDependent.hasVarHandle(), but hasVarHandle() is disabled when isUnaligned() is true (initializeVarHandle() returns false), which can prevent selecting the VarHandle queue implementation even when Unsafe is unavailable. Consider using a VarHandle-queue-specific support check instead of hasVarHandle().
return hasVarHandle() ? new MpscVarHandleUnpaddedArrayQueue<T>(capacity)
: new MpscAtomicUnpaddedArrayQueue<T>(capacity);
common/src/main/java/io/netty/util/internal/PlatformDependent.java:1373
- This uses PlatformDependent.hasVarHandle(), but hasVarHandle() is false whenever isUnaligned() is true (initializeVarHandle() returns false early), which can prevent selecting the VarHandle queue implementation when Unsafe is unavailable. Consider using a VarHandle-queue-specific availability check instead of hasVarHandle().
return hasVarHandle() ? new MpmcVarHandleArrayQueue<T>(capacity) : new MpmcAtomicArrayQueue<T>(capacity);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
/cc @nitsanw |
|
There is an error when trying to use jpms. This might be a jctools bug tho: [ERROR] Error: java.lang.IllegalArgumentException: org.jctools.core-jdk11: Invalid module name: 'core-jdk11' is not a Java identifier /cc @nitsanw @franz1981 |
I'll look into it and resolve |
c9abf38 to
d7b5989
Compare
The Build PR failure and both CodeQL failures had the same underlying cause. The JPMS test encountered the invalid automatic module name derived from jctools-core-jdk11. Preventing the shaded artifact from leaking transitively should address all three checks |
ac84fe0 to
7d157b4
Compare
7d157b4 to
1217ed2
Compare
|
@arnabnandy7 same problem |
Thanks for checking, I'll review the issue and fix. |
1217ed2 to
e240325
Compare
Please run the CIs again. |
|
@normanmaurer can you please rerun only the failed check aka The detailed log to be referred to check above claim. |
|
#17185 (comment) You're right, and I think this has been fixed since: JCTools/JCTools@aeda6c2 This commit will make it to the 5.0.0 release, which is also the release where we up the bytecode version to 8. |
|
As we shade it we should be good even without JCTools/JCTools@aeda6c2 correct @nitsanw ? |
|
@arnabnandy7 did you sign our icla yet? https://netty.io/s/icla |
Not yet will do tomorrow first thing. |
|
@arnabnandy7 ping us once done |
chrisvest
left a comment
There was a problem hiding this comment.
Just fix the copyright years and it's good.
Done |
franz1981
left a comment
There was a problem hiding this comment.
LGTM thanks for picking this one!
|
@arnabnandy7 fix check style |
Signed-off-by: Arnab Nandy <[email protected]>
Co-authored-by: Chris Vest <[email protected]> Signed-off-by: Arnab Nandy <[email protected]>
69d3702 to
7a5b2fb
Compare
done |
🚨 TestLens detected 1 failed test 🚨Here is what you can do:
Test SummaryBuild PR / windows-x86_64-java11-boringssl > Netty/Buffer
🏷️ Commit: 7a5b2fb Test FailuresPooledByteBufAllocatorTest > testThreadCacheDestroyedByThreadCleaner() (Netty/Buffer in Build PR / windows-x86_64-java11-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. |
|
windows-x86_64-java11-boringssl is OOM, unrelated. Please rerun the failed CI. |
|
Auto-port PR for 5.0: #17239 |
Auto-port of #17185 to 5.0 Cherry-picked commit: 95012ca --- Motivation: Netty currently falls back to JCTools atomic queues when `Unsafe` is unavailable. JCTools 4.0.6 provides VarHandle-based queues that avoid `Unsafe` while offering a more efficient alternative to the atomic implementations. Modification: Added and shaded the `jctools-core-jdk11` dependency. Updated the queue factories in `PlatformDependent` to prefer implementations in this order: 1. Unsafe-based queues when `Unsafe` is available. 2. VarHandle-based queues when `Unsafe` is unavailable but VarHandle is supported. 3. Atomic queues as the fallback. Added test coverage for the VarHandle queue selection when `Unsafe` is disabled. Result: Fixes #17157. Signed-off-by: Arnab Nandy <[email protected]> Co-authored-by: Arnab Nandy <[email protected]> Co-authored-by: Chris Vest <[email protected]>
Motivation: In #17185, jctools-core-jdk11 was added and shaded into netty-common to provide VarHandle-based queues when Unsafe is disabled. However, when bytecode instrumentation tools or agents (e.g. APM agents using ASM COMPUTE_FRAMES) process PlatformDependent, the branch merge of `hasJctoolsVarHandle() ? new SpscLinkedVarHandleQueue<T>() : new SpscLinkedAtomicQueue<T>()` causes the stack map frame to be recomputed to `java/util/AbstractQueue`. This forces the JVM verifier to load `SpscLinkedVarHandleQueue` upon `PlatformDependent` verification. On Java 8, `SpscLinkedVarHandleQueue` is compiled with major version 55.0 (Java 11), resulting in an `UnsupportedClassVersionError` (#17685). Modifications: - Move all `*VarHandle*Queue` instantiations in PlatformDependent into a private static holder class `VarHandleQueueHolder`. - `PlatformDependent.class` no longer references any `*VarHandle*Queue` classes directly, preventing the JVM bytecode verifier or ASM frame recomputations from loading them during PlatformDependent class verification. - On Java 8 runtimes, `hasJctoolsVarHandle()` is false, so `VarHandleQueueHolder` is never accessed or loaded. - On Java 11+ runtimes when Unsafe is disabled, `VarHandleQueueHolder` is lazily loaded on demand, retaining the fast VarHandle queue implementations. Result: Fixes #17685 while retaining VarHandle queues on JDK 11+. Signed-off-by: Arnab Nandy <[email protected]>
… class (#17830) Auto-port of #17750 to 5.0 Cherry-picked commit: 103787e --- Motivation: In #17185, jctools-core-jdk11 was added and shaded into netty-common to provide VarHandle-based queues when Unsafe is disabled. However, when bytecode instrumentation tools or agents (e.g. APM agents using ASM COMPUTE_FRAMES) process PlatformDependent, the branch merge of `hasJctoolsVarHandle() ? new SpscLinkedVarHandleQueue<T>() : new SpscLinkedAtomicQueue<T>()` causes the stack map frame to be recomputed to `java/util/AbstractQueue`. This forces the JVM verifier to load `SpscLinkedVarHandleQueue` upon `PlatformDependent` verification. On Java 8, `SpscLinkedVarHandleQueue` is compiled with major version 55.0 (Java 11), resulting in an `UnsupportedClassVersionError` (#17685). Modifications: - Move all `*VarHandle*Queue` instantiations in PlatformDependent into a private static holder class `VarHandleQueueHolder`. - `PlatformDependent.class` no longer references any `*VarHandle*Queue` classes directly, preventing the JVM bytecode verifier or ASM frame recomputations from loading them during PlatformDependent class verification. - On Java 8 runtimes, `hasJctoolsVarHandle()` is false, so `VarHandleQueueHolder` is never accessed or loaded. - On Java 11+ runtimes when Unsafe is disabled, `VarHandleQueueHolder` is lazily loaded on demand, retaining the fast VarHandle queue implementations. Result: Fixes #17685 while retaining VarHandle queues on JDK 11+. Signed-off-by: Arnab Nandy <[email protected]> Co-authored-by: Arnab Nandy <[email protected]>
…ss (netty/netty#17750) Motivation: In netty/netty#17185, jctools-core-jdk11 was added and shaded into netty-common to provide VarHandle-based queues when Unsafe is disabled. However, when bytecode instrumentation tools or agents (e.g. APM agents using ASM COMPUTE_FRAMES) process PlatformDependent, the branch merge of `hasJctoolsVarHandle() ? new SpscLinkedVarHandleQueue<T>() : new SpscLinkedAtomicQueue<T>()` causes the stack map frame to be recomputed to `java/util/AbstractQueue`. This forces the JVM verifier to load `SpscLinkedVarHandleQueue` upon `PlatformDependent` verification. On Java 8, `SpscLinkedVarHandleQueue` is compiled with major version 55.0 (Java 11), resulting in an `UnsupportedClassVersionError` (netty/netty#17685). Modifications: - Move all `*VarHandle*Queue` instantiations in PlatformDependent into a private static holder class `VarHandleQueueHolder`. - `PlatformDependent.class` no longer references any `*VarHandle*Queue` classes directly, preventing the JVM bytecode verifier or ASM frame recomputations from loading them during PlatformDependent class verification. - On Java 8 runtimes, `hasJctoolsVarHandle()` is false, so `VarHandleQueueHolder` is never accessed or loaded. - On Java 11+ runtimes when Unsafe is disabled, `VarHandleQueueHolder` is lazily loaded on demand, retaining the fast VarHandle queue implementations. Result: Fixes netty/netty#17685 while retaining VarHandle queues on JDK 11+. Signed-off-by: Arnab Nandy <[email protected]>
Motivation:
Netty currently falls back to JCTools atomic queues when
Unsafeis unavailable. JCTools 4.0.6 provides VarHandle-based queues that avoidUnsafewhile offering a more efficient alternative to the atomic implementations.Modification:
Added and shaded the
jctools-core-jdk11dependency. Updated the queue factories inPlatformDependentto prefer implementations in this order:Unsafeis available.Unsafeis unavailable but VarHandle is supported.Added test coverage for the VarHandle queue selection when
Unsafeis disabled.Result:
Fixes #17157.