Skip to content

Use VarHandle JCTools queues without Unsafe - #17185

Merged
chrisvest merged 2 commits into
netty:4.2from
arnabnandy7:feature/use-varhandle-jctools-queues
Aug 7, 2026
Merged

chrisvest merged 2 commits into
netty:4.2from
arnabnandy7:feature/use-varhandle-jctools-queues

Conversation

@arnabnandy7

Copy link
Copy Markdown
Contributor

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.

Comment thread common/pom.xml Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.6 to dependency management and to netty-common for shading.
  • Update PlatformDependent queue factories to prefer Unsafe-based queues, then VarHandle-based queues, then atomic queues.
  • Add a unit test intended to validate VarHandle queue selection when Unsafe is 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.

@normanmaurer
normanmaurer requested a review from yawkat August 3, 2026 08:02
@normanmaurer

Copy link
Copy Markdown
Member

/cc @nitsanw

@normanmaurer
normanmaurer requested a review from chrisvest August 3, 2026 08:06
@normanmaurer normanmaurer added this to the 4.2.17.Final milestone Aug 3, 2026
@normanmaurer normanmaurer added the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Aug 3, 2026
@normanmaurer

Copy link
Copy Markdown
Member

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

@arnabnandy7

Copy link
Copy Markdown
Contributor Author

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

@arnabnandy7
arnabnandy7 force-pushed the feature/use-varhandle-jctools-queues branch from c9abf38 to d7b5989 Compare August 3, 2026 17:11
@arnabnandy7

Copy link
Copy Markdown
Contributor Author

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

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

@arnabnandy7
arnabnandy7 force-pushed the feature/use-varhandle-jctools-queues branch 2 times, most recently from ac84fe0 to 7d157b4 Compare August 3, 2026 17:15
Comment thread pom.xml Resolved
@arnabnandy7
arnabnandy7 force-pushed the feature/use-varhandle-jctools-queues branch from 7d157b4 to 1217ed2 Compare August 3, 2026 17:56
@normanmaurer

Copy link
Copy Markdown
Member

@arnabnandy7 same problem

@arnabnandy7

Copy link
Copy Markdown
Contributor Author

@arnabnandy7 same problem

Thanks for checking, I'll review the issue and fix.

@arnabnandy7
arnabnandy7 force-pushed the feature/use-varhandle-jctools-queues branch from 1217ed2 to e240325 Compare August 4, 2026 16:47
@arnabnandy7

Copy link
Copy Markdown
Contributor Author

@arnabnandy7 same problem

Please run the CIs again.

@arnabnandy7

arnabnandy7 commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor Author

@normanmaurer can you please rerun only the failed check aka linux-x86_64-java26 build? It seems to be one flaky earlier got resolved as part of commit 9c4d48eed5 it's throwing OOM (java.lang.OutOfMemoryError: Java heap space) then failing with the following;

SingleThreadEventExecutorTest.testNotSuspendedUntilScheduledTaskIsCancelled
expected: <true> but was: <false>

The detailed log to be referred to check above claim.

@chrisvest chrisvest modified the milestones: 4.2.17.Final, 4.2.18.Final Aug 4, 2026
@nitsanw

nitsanw commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

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

@normanmaurer

Copy link
Copy Markdown
Member

As we shade it we should be good even without JCTools/JCTools@aeda6c2 correct @nitsanw ?

@normanmaurer

Copy link
Copy Markdown
Member

@arnabnandy7

Copy link
Copy Markdown
Contributor Author

@arnabnandy7 did you sign our icla yet? https://netty.io/s/icla

Not yet will do tomorrow first thing.

@normanmaurer

Copy link
Copy Markdown
Member

@arnabnandy7 ping us once done

@chrisvest chrisvest left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just fix the copyright years and it's good.

@arnabnandy7
arnabnandy7 requested a review from chrisvest August 7, 2026 05:26
@arnabnandy7

Copy link
Copy Markdown
Contributor Author

@arnabnandy7 ping us once done

Done

@franz1981 franz1981 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks for picking this one!

@normanmaurer

Copy link
Copy Markdown
Member

@arnabnandy7 fix check style

arnabnandy7 and others added 2 commits August 7, 2026 18:16
Co-authored-by: Chris Vest <[email protected]>
Signed-off-by: Arnab Nandy <[email protected]>
@arnabnandy7
arnabnandy7 force-pushed the feature/use-varhandle-jctools-queues branch from 69d3702 to 7a5b2fb Compare August 7, 2026 12:50
@arnabnandy7

Copy link
Copy Markdown
Contributor Author

@arnabnandy7 fix check style

done

@testlens-app

testlens-app Bot commented Aug 7, 2026 •

Copy link
Copy Markdown

🚨 TestLens detected 1 failed test 🚨

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-java11-boringssl > Netty/Buffer

Test Runs Flakiness
PooledByteBufAllocatorTest > testThreadCacheDestroyedByThreadCleaner() 🚫 ❌ 1% 🟡

🏷️ Commit: 7a5b2fb
▶️ Tests: 443849 executed
⚪️ Checks: 18/18 completed

Test Failures

PooledByteBufAllocatorTest > testThreadCacheDestroyedByThreadCleaner() (Netty/Buffer in Build PR / windows-x86_64-java11-boringssl)
java.util.concurrent.TimeoutException: testThreadCacheDestroyedByThreadCleaner() timed out after 20 seconds
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinTask.doJoin(ForkJoinTask.java:396)
	at java.base/java.util.concurrent.ForkJoinTask.join(ForkJoinTask.java:721)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinTask.doJoin(ForkJoinTask.java:396)
	at java.base/java.util.concurrent.ForkJoinTask.join(ForkJoinTask.java:721)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Caused by: org.junit.jupiter.api.AssertTimeoutPreemptively$ExecutionTimeoutException: Execution timed out in thread junit-timeout-thread-2
	at [email protected]/jdk.internal.misc.Unsafe.park(Native Method)
	at [email protected]/java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:357)
	at app//io.netty.buffer.PooledByteBufAllocatorTest.testThreadCacheDestroyed(PooledByteBufAllocatorTest.java:423)
	at app//io.netty.buffer.PooledByteBufAllocatorTest.testThreadCacheDestroyedByThreadCleaner(PooledByteBufAllocatorTest.java:366)
	at [email protected]/java.lang.reflect.Method.invoke(Method.java:566)
	at [email protected]/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at [email protected]/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at [email protected]/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at [email protected]/java.lang.Thread.run(Thread.java:829)

Muted Tests

Select tests to mute in this pull request:

  • PooledByteBufAllocatorTest > testThreadCacheDestroyedByThreadCleaner()

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.

@arnabnandy7

arnabnandy7 commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor Author

windows-x86_64-java11-boringssl is OOM, unrelated. Please rerun the failed CI.

@chrisvest
chrisvest merged commit 95012ca into netty:4.2 Aug 7, 2026
18 of 19 checks passed
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #17239

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Aug 7, 2026
normanmaurer pushed a commit that referenced this pull request Aug 9, 2026
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]>
@normanmaurer normanmaurer mentioned this pull request Oct 1, 2026
normanmaurer pushed a commit that referenced this pull request Oct 15, 2026
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]>
normanmaurer pushed a commit that referenced this pull request Oct 15, 2026
… 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]>
ikpil added a commit to ikpil/Netty.NET that referenced this pull request Nov 29, 2026
…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]>
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.

Use shiny new VarHandle JCTools queues

7 participants