On Wed, 25 Jun 2025 10:15:54 GMT, Alan Bateman <al...@openjdk.org> wrote:
>> ThreadDumper/ThreadSnapshot need to handle a failure to resolve the native >> VM JavaThread from a java.lang.Thread. This is hard to reproduce but a >> thread that has since terminated can provoke a crash. Recognise this and >> return a null ThreadSnapshot. > > src/java.base/share/classes/jdk/internal/vm/ThreadDumper.java line 184: > >> 182: if (snapshot == null) { >> 183: return; // Terminated >> 184: } > > Would it be possible to use this instance, otherwise the thread counts will > be confusing. > > > --- a/src/java.base/share/classes/jdk/internal/vm/ThreadDumper.java > +++ b/src/java.base/share/classes/jdk/internal/vm/ThreadDumper.java > @@ -178,8 +178,11 @@ private static void dumpThreads(ThreadContainer > container, TextWriter writer) { > } > > private static void dumpThread(Thread thread, TextWriter writer) { > - ThreadSnapshot snapshot = ThreadSnapshot.of(thread); > Instant now = Instant.now(); > + ThreadSnapshot snapshot = ThreadSnapshot.of(thread); > + if (snapshot == null) { > + return; // thread terminated > + } > Thread.State state = snapshot.threadState(); > writer.println("#" + thread.threadId() + " "" + snapshot.threadName() > + "" " + (thread.isVirtual() ? "virtual " : "") + state + " > " + now); > @@ -284,8 +287,9 @@ private static void dumpThreads(ThreadContainer > container, JsonWriter jsonWriter > Iterator<Thread> threads = container.threads().iterator(); > while (threads.hasNext()) { > Thread thread = threads.next(); > - dumpThread(thread, jsonWriter); > - threadCount++; > + if (dumpThread(thread, jsonWriter)) { > + threadCount++; > + } > } > jsonWriter.endArray(); // threads > > @@ -305,9 +309,12 @@ private static void dumpThreads(ThreadContainer > container, JsonWriter jsonWriter > * Write a thread to the given JSON writer. > * @throws UncheckedIOException if an I/O error occurs > */ > - private static void dumpThread(Thread thread, JsonWriter jsonWriter) { > + private static boolean dumpThread(Thread thread, JsonWriter jsonWriter) { > Instant now = Instant.now(); > ThreadSnapshot snapshot = ThreadSnapshot.of(thread); > + if (snapshot == null) { > + return false; // thread terminated > + } > Thread.State state = snapshot.threadState(); > StackTraceElement[] stackTrace = snapshot.stackTrace(); > > @@ -369,6 +376,7 @@ private static void dumpThread(Thread thread, JsonWriter > jsonWriter) { > } > > jsonWriter.endObject(); > + return true; > } Yes, will do that to fix the threadCount... ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/25958#discussion_r2166363495