Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8309334

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

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 21
    • 21
    • hotspot
    • b26

    Description

      Normally when a virtual thread wrapper is used to run a test, the main thread is renamed to "old-m-a-i-n" and the new virtual thread that will act as the main thread is named "main". Neither is being done by `ProcessTools.main()`. This can cause problems for tests that expect the main thread that the test is running in to be called "main".

      diff --git a/test/lib/jdk/test/lib/process/ProcessTools.java b/test/lib/jdk/test/lib/process/ProcessTools.java
      index 115f2e96fc0..ed30efbb6fb 100644
      --- a/test/lib/jdk/test/lib/process/ProcessTools.java
      +++ b/test/lib/jdk/test/lib/process/ProcessTools.java
      @@ -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 = createVirtualThread(() -> {
                           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;
      @@ -940,12 +945,12 @@ public final class ProcessTools {
               Throwable uncaughtThrowable = null;
           }
       
      - static Thread startVirtualThread(Runnable task) {
      + static Thread createVirtualThread(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);
      + Method unstarted = clazz.getMethod("unstarted", Runnable.class);
      + return (Thread) unstarted.invoke(builder, task);
               } catch (RuntimeException | Error e) {
                   throw e;
               } catch (Exception e) {

      Attachments

        Issue Links

          Activity

            People

              cjplummer Chris Plummer
              cjplummer Chris Plummer
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: