Skip to content

Commit

Permalink
8309334: ProcessTools.main() does not properly set thread names when …
Browse files Browse the repository at this point in the history
…using the virtual thread wrapper

Reviewed-by: amenkov, lmesnik, sspitsyn, alanb
  • Loading branch information
plummercj committed Jun 4, 2023
1 parent ac1597b commit ecb1753
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,7 +40,7 @@ public static void main(String args[]) throws Throwable {
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)");
Expand Down
3 changes: 0 additions & 3 deletions test/jdk/ProblemList-Virtual.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 6 additions & 14 deletions test/lib/jdk/test/lib/process/ProcessTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,8 @@ private void waitForStreams() throws InterruptedException {
}
}

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 {
Expand All @@ -894,7 +896,7 @@ public static void main(String[] args) throws Throwable {
// 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) {
Expand All @@ -903,6 +905,9 @@ public static void main(String[] args) throws Throwable {
tg.uncaughtThrowable = error;
}
});
Thread.currentThread().setName(OLD_MAIN_THREAD_NAME);
vthread.setName("main");
vthread.start();
vthread.join();
if (tg.uncaughtThrowable != null) {
throw tg.uncaughtThrowable;
Expand Down Expand Up @@ -939,17 +944,4 @@ public void uncaughtException(Thread t, Throwable e) {
}
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);
}
}
}

1 comment on commit ecb1753

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.