8309334: ProcessTools.main() does not properly set thread names when using the virtual thread wrapper

Reviewed-by: amenkov, lmesnik, sspitsyn, alanb
This commit is contained in:
Chris Plummer 2023-06-04 18:18:11 +00:00
parent ac1597bcc7
commit ecb17532dc
3 changed files with 8 additions and 19 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -40,7 +40,7 @@ public class TestLambdaExceptionInInitializer {
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("TestPkg.Lambda");
OutputAnalyzer output = new OutputAnalyzer(pb.start());
output.shouldContain("Exception in thread \"main\" java.lang.ExceptionInInitializerError");
output.shouldMatch("Exception in thread \".+\" java.lang.ExceptionInInitializerError");
output.shouldContain("Caused by: java.lang.NullPointerException");
output.shouldContain("at TestPkg.LambdaMetafactory.throwNpe(LambdaMetafactory.java:34)");

View File

@ -29,9 +29,6 @@ com/sun/jdi/EATests.java#id0 8264699 generic-
com/sun/jdi/ExceptionEvents.java 8278470 generic-all
com/sun/jdi/JdbMethodExitTest.java 8285422 generic-all
com/sun/jdi/JdbStepTest.java 8285422 generic-all
com/sun/jdi/JdbStopThreadTest.java 8285422 generic-all
com/sun/jdi/JdbStopThreadidTest.java 8285422 generic-all
com/sun/jdi/MethodEntryExitEvents.java 8285422 generic-all
com/sun/jdi/MultiBreakpointsTest.java 8285422 generic-all
com/sun/jdi/RedefineCrossStart.java 8278470 generic-all

View File

@ -879,6 +879,8 @@ public final class ProcessTools {
}
}
public static final String OLD_MAIN_THREAD_NAME = "old-m-a-i-n";
// ProcessTools as a wrapper
// It executes method main in a separate virtual or platform thread
public static void main(String[] args) throws Throwable {
@ -894,7 +896,7 @@ public final class ProcessTools {
// MainThreadGroup used just as a container for exceptions
// when main is executed in virtual thread
MainThreadGroup tg = new MainThreadGroup();
Thread vthread = startVirtualThread(() -> {
Thread vthread = Thread.ofVirtual().unstarted(() -> {
try {
mainMethod.invoke(null, new Object[] { classArgs });
} catch (InvocationTargetException e) {
@ -903,6 +905,9 @@ public final class ProcessTools {
tg.uncaughtThrowable = error;
}
});
Thread.currentThread().setName(OLD_MAIN_THREAD_NAME);
vthread.setName("main");
vthread.start();
vthread.join();
if (tg.uncaughtThrowable != null) {
throw tg.uncaughtThrowable;
@ -939,17 +944,4 @@ public final class ProcessTools {
}
Throwable uncaughtThrowable = null;
}
static Thread startVirtualThread(Runnable task) {
try {
Object builder = Thread.class.getMethod("ofVirtual").invoke(null);
Class<?> clazz = Class.forName("java.lang.Thread$Builder");
Method start = clazz.getMethod("start", Runnable.class);
return (Thread) start.invoke(builder, task);
} catch (RuntimeException | Error e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}