Techniques for debugging threading issues with Virtual Threads

Alan Bateman alan.bateman at oracle.com
Wed Nov 20 07:22:57 UTC 2024



On 20/11/2024 07:10, Dr Heinz M. Kabutz wrote:
> With platform threads, we have had good techniques for finding 
> threading issues. For example, a thread dump would reveal deadlocks, 
> livelocks and contention.
>
> However, with virtual threads, most of these techniques won't work 
> anymore. For example, here is a SimpleDeadlock:
>
> import java.util.concurrent.locks.*;
>
> public class SimpleDeadlock {
>     public static void main(String... args) throws InterruptedException {
>         var monitor1 = new Object();
>         var monitor2 = new Object();
>         Thread.startVirtualThread(() -> {
>             synchronized (monitor1) {
>                 LockSupport.parkNanos(10_000_000);
>                 synchronized (monitor2) {
>                     System.out.println("Got both locks");
>                 }
>             }
>         });
>         Thread.startVirtualThread(() -> {
>             synchronized (monitor2) {
>                 LockSupport.parkNanos(10_000_000);
>                 synchronized (monitor1) {
>                     System.out.println("Got both locks");
>                 }
>             }
>         }).join();
>     }
> }
>
> If we run this with Java 21-23 and -Djdk.trackAllThreads=false, the 
> carrier thread indicate that they are carrying some virtual threads, 
> but these do not appear in the full dump. In Java 24+24, the virtual 
> threads are parked instead of pinned, and thus they vanish completely 
> from the thread dumps.
>
> Is there any project or workgroup where debugging of virtual threads 
> is being looked at?

The HotSpot VM thread dump will show mounted virtual threads but not 
unmounted virtual threads. So yes, fixing the pinning issue with object 
monitors has an impact on thread dumps because the virtual threads 
unmount. The thread dump taken with jcmd Thread.dump_to_file shows all 
threads, including all unmounted virtual threads. The intention is that 
will be expanded to include information on monitors and ownable 
synchronizers, still some work required on that. Having a parsing thread 
dump with lock information will allow for more diagnostic tooling, 
including deadlock detection.

-Alan



More information about the loom-dev mailing list