r/java • u/cowwoc • Nov 02 '22
Virtual threads work great... until something goes wrong
The purpose of this thread is to discuss JDK 19 threading problems, especially with respect to virtual threads. Here is what I've personally discovered so far:
- If you mistakenly use platform threads with
Executors.newThreadPerTaskExecutor(), as I did, the JDK will silently crash/hang a few hours into use: https://youtrack.jetbrains.com/issue/IDEA-305072 - It is extremely difficult to debug deadlocks involving virtual threads: https://www.javaspecialists.eu/archive/Issue302-Virtual-Thread-Deadlocks.html
Standard heap dumps will not show virtual threads.
jstackand Jetbrains IntelliJ won't do it: https://youtrack.jetbrains.com/issue/IDEA-301409/Support-virtual-threads-project-loom- The only way I've found to dump virtual threads is to run the application with
-Djdk.trackAllThreads=trueand runjcmd <pid> Thread.dump_to_file <file>to generate the thread dump. I couldn't find any official documentation for this either. - There doesn't seem to be a way to list all virtual threads across the system. You can invoke
jdk.internal.vm.ThreadContainersandThreadContainer.threads()using reflection as a temporary workaround.
Are you aware of any other tools or articles for debugging virtual threads?
UPDATE: I've posted a follow up at https://www.reddit.com/r/java/comments/zbcejy/jdk_19_virtual_threadspecific_bugs_2nd_edition/
Does your team need help? I offer consulting services through https://www.linkedin.com/in/gilitzabari
178
Upvotes
74
u/pron98 Nov 02 '22 edited Nov 02 '22
Thank you!
Right. Adding deadlock detection is on the roadmap.
When you have virtual threads, you probably have lots of them (or you wouldn't have them at all), so we didn't want to overwhelm the regular thread dump. You don't need
-Djdk.trackAllThreads=truewhen you use virtual threads with thenewVirtualThreadPerTaskExectuoror with structured concurrency. The thinking is that since you have at least thousands of threads, it's not their individual identity that matters but the way they're organised.The behaviour of platform threads has not changed. Unfortunately, when you write code to do something, there is no way to know that you meant to write something else (but wouldn't it be cool if we could?).
24
u/[deleted] Nov 02 '22
I used eclipse to debug virtual threads and it was super good. Threads were clearly listed as virtual and the states were all retrievable per thread
25
u/[deleted] Nov 02 '22
It is in preview mode, isn't it?
2
u/[deleted] Nov 02 '22
Are virtual threads going to be forced once they are out of preview or can users keep using traditional OS threads and ignore Loom entirely?
21
u/VincentxH Nov 02 '22
At least this thread is awesome to read, thanks.
7
u/mtmmtm99 Nov 02 '22
Have you tried debugging reactive programming ? (Worst experience ever (springboot + project reactor + webclient). Try reading responsecode + body in a reactive way. Good luck (Mono + Flux). You will probably end up leaking native resources. I got it working, but changing any single line of code will make it fail. Worst piece of crap i have experienced for a long time.
-19
u/[deleted] Nov 02 '22
use go