Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent pinned CarrierThreads on JDK-21 while using Virtual Threads #4079

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rjbaucells
Copy link

This PR replaces some of the synchronized() blocks with ReentrantLock equivalents to prevent pinned CarrierThreads when using JDK-21 Virtual Threads.

When a service instance is requested from a Virtual Thread and the service initialization runs a native method or foreign function, the JVM Carrier Thread is pinned preventing the execution of other Virtual Threads. See the following trace logged by using the -Djdk.tracePinnedThreads=full command line argument to the java command:

Thread[#18,ForkJoinPool-1-worker-2,5,CarrierThreads]
    java.base/java.lang.VirtualThread$VThreadContinuation.onPinned(VirtualThread.java:185)
    java.base/jdk.internal.vm.Continuation.onPinned0(Continuation.java:393)
    java.base/java.lang.VirtualThread.park(VirtualThread.java:592)
    java.base/java.lang.System$2.parkVirtualThread(System.java:2639)
    java.base/jdk.internal.misc.VirtualThreads.park(VirtualThreads.java:54)
    java.base/java.util.concurrent.locks.LockSupport.park(LockSupport.java:219)
    java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:754)
    java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:990)
    java.base/java.util.concurrent.locks.ReentrantLock$Sync.lock(ReentrantLock.java:153)
    java.base/java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:322)

    .. service initialization ...

    [email protected]/dagger.internal.DoubleCheck.get(DoubleCheck.java:47) <== monitors:1

After replacing the synchronized(monitor) {} by the ReentrantLock the CarrierThread is no longer pinned and the JVM no longer logs the above Error message.

See JDK related documentation

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.

1 participant