On Wed, 9 Sep 2026 12:01:48 GMT, Denghui Dong <[email protected]> wrote:

> Hi,
> 
> Please help review this change that fixes crash in ThreadSnapshot::initialize.
> 
> After [JDK-8323792](https://bugs.openjdk.org/browse/JDK-8323792), we can 
> still see the same crash reported in 
> [JDK-8374820](https://bugs.openjdk.org/browse/JDK-8374820) and 
> [JDK-8346980](https://bugs.openjdk.org/browse/JDK-8346980).
> 
> Here is a reproducer:
> 
> 
> import java.lang.management.ManagementFactory;
> import java.lang.management.ThreadInfo;
> import java.lang.management.ThreadMXBean;
> import java.util.Arrays;
> import java.util.Objects;
> import java.util.concurrent.atomic.AtomicLong;
> import java.util.concurrent.locks.LockSupport;
> 
> public class ThreadSnapshotRace {
> 
>     public static void main(String[] args) throws Exception {
>         Thread producer = new Thread(() -> {
>             AtomicLong counter = new AtomicLong();
>             long total = 0;
>             while (true) {
>                 long c = counter.incrementAndGet();
>                 total++;
>                 Thread.ofVirtual().name("vthread").start(() -> {
>                     counter.decrementAndGet(); 
>                 });
>                 if (total % 10_000_000 == 0) {
>                     System.out.println(total);
>                 }
>                 if (c >= 20_000_000) {
>                     do {
>                         try {
>                             Thread.sleep(50);
>                         } catch (Exception e) {
>                         }
>                     } while (counter.get() > 0);
>                 }
>             }
>         });
>         producer.start();
> 
>         Thread.sleep(1000);
> 
>         Thread consumer = new Thread(() -> {
>             ThreadMXBean bean = ManagementFactory.getThreadMXBean();
>             long[] ids = carrierIds(bean);
>             while (true) {
>                 ThreadInfo[] infos = bean.getThreadInfo(ids);
>                 if (infos.length == 0) {
>                     System.out.println("?");
>                 }
>             }
>         });
>         consumer.start();
>     }
> 
>     static long[] carrierIds(ThreadMXBean bean) {
>         long[] all = bean.getAllThreadIds();
>         long[] carriers = Arrays.stream(bean.getThreadInfo(all))
>                 .filter(Objects::nonNull)
>                 .filter(ti -> 
> ti.getThreadName().startsWith("ForkJoinPool-1-worker"))
>                 .mapToLong(ThreadInfo::getThreadId)
>                 .toArray();
>         return carriers;
>     }
> }
> 
> 
> 
> Run with `java -Djdk.virtualThreadScheduler.parallelism=8 
> ThreadSnapshotRace`. Crash occurs for a few minutes.
> 
> IIUC, the root cause is ...

This pull request has now been integrated.

Changeset: ff6eb14f
Author:    Denghui Dong <[email protected]>
URL:       
https://git.openjdk.org/jdk/commit/ff6eb14f5c9f7478cadc65e133e60d6373b823e2
Stats:     106 lines in 5 files changed: 100 ins; 2 del; 4 mod

8392031: Crash in ThreadSnapshot::initialize

Reviewed-by: pchilanomate, dholmes

-------------

PR: https://git.openjdk.org/jdk/pull/32788

Reply via email to