JDK-8322846 : Running with -Djdk.tracePinnedThreads set can hang
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 21,22,23
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2024-01-02
  • Updated: 2024-05-08
  • Resolved: 2024-01-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 21 JDK 22 JDK 23
21.0.4-oracleFixed 22Fixed 23 b05Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
-Djdk.tracePinnedThreads dates from early development in the loom repo to identify pinned threads. It has several issues and will eventually be removed (in favour of JFR of events). Several hangs have been reported when running with this system property set. The "hangs" stem from the onPinned method executing while the virtual thread is in a transition state (typically parking). If the virtual parks while printing the stack trace then it works like a nested park where the thread state is never restored. Contention on the System.out can also lead to deadlock when there are platform and pinned virtual threads printing to System.out around the same time.

This JBS issue tracks some short term changes to the implementation to avoid the hangs that have been reported when running with this system property set. Specifically, it only attempts to print the stack trace when System.out's lock can be acquired without blocking, and restoring the thread state after printing.


Comments
[jdk21u-fix-request] Approval Request from Aleksey Shipilëv Fixes the hang when using a routine diagnostic option. Applies cleanly. New test reliably fails without the fix. All other tests are passing. Risk is medium-low, as it affects the diagnostic path that is not enabled by default.
29-02-2024

A pull request was submitted for review. URL: https://git.openjdk.org/jdk21u-dev/pull/270 Date: 2024-02-19 18:58:09 +0000
28-02-2024

Thanks! I was able to confirm locally that JDK-8315373 makes the test clean. I think that was the change that reworked where run/continue/yield is done.
19-02-2024

[~shade] In JDK 21, the continuation runs/continues and the yielding is done in the context of the carrier. Since JDK 22 it is done in the context of the virtual thread. There's were issues with JVMTI and hiding frames that forced the approach that was in JDK 19-21. So the ClassCastException isn't surprising here as the onPinned callback runs in the context of the carrier. If the changes are going into JDK 21u then the change to VirtualThread.onPinned is a one line change to add the "reason" parameter.
17-01-2024

Note for JDK 21 backports, the test is failing when applied to current jdk21u-dev: ``` % CONF=macosx-aarch64-server-fastdebug make images test TEST=java/lang/Thread/virtual/TracePinnedThreads.java ... STARTED TracePinnedThreads::testPinnedCausedByNativeMethod 'testPinnedCausedByNativeMethod()' java.lang.ClassCastException: class jdk.internal.misc.CarrierThread cannot be cast to class java.lang.VirtualThread (jdk.internal.misc.CarrierThread and java.lang.VirtualThread are in module java.base of loader 'bootstrap') at java.base/java.lang.VirtualThread$VThreadContinuation.onPinned(VirtualThread.java:183) at java.base/jdk.internal.vm.Continuation.onPinned0(Continuation.java:393) at java.base/java.lang.VirtualThread.parkNanos(VirtualThread.java:629) at java.base/java.lang.System$2.parkVirtualThread(System.java:2652) at java.base/jdk.internal.misc.VirtualThreads.park(VirtualThreads.java:67) at java.base/java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:408) at TracePinnedThreads.park(TracePinnedThreads.java:52) at TracePinnedThreads.invokePark(Native Method) at TracePinnedThreads.lambda$testPinnedCausedByNativeMethod$1(TracePinnedThreads.java:81) at jdk.test.lib.thread.VThreadRunner.lambda$run$0(VThreadRunner.java:71) at java.base/java.lang.VirtualThread.run(VirtualThread.java:317) ``` Passes well in current mainline. There are lots of changes between jdk21u-dev and current mainline, and one of them must be missing. It is a bit weird that we have `Thread.currentThread()` as `CarrierThread`, when we are clearly running from `VirtualThread.run`.
17-01-2024

A pull request was submitted for review. URL: https://git.openjdk.org/jdk22/pull/71 Date: 2024-01-13 18:06:11 +0000
15-01-2024

Changeset: faa9c690 Author: Alan Bateman <alanb@openjdk.org> Date: 2024-01-06 18:24:28 +0000 URL: https://git.openjdk.org/jdk/commit/faa9c6909dda635eb008b9dada6e06fca47c17d6
06-01-2024

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/17221 Date: 2024-01-02 13:53:39 +0000
02-01-2024