From ecb17532dc8f3e271ad2d6550127a2253569cf9b Mon Sep 17 00:00:00 2001 From: Chris Plummer Date: Sun, 4 Jun 2023 18:18:11 +0000 Subject: [PATCH] 8309334: ProcessTools.main() does not properly set thread names when using the virtual thread wrapper Reviewed-by: amenkov, lmesnik, sspitsyn, alanb --- .../TestLambdaExceptionInInitializer.java | 4 ++-- test/jdk/ProblemList-Virtual.txt | 3 --- .../jdk/test/lib/process/ProcessTools.java | 20 ++++++------------- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/test/hotspot/jtreg/runtime/BootstrapMethod/TestLambdaExceptionInInitializer.java b/test/hotspot/jtreg/runtime/BootstrapMethod/TestLambdaExceptionInInitializer.java index bc59c08854a46..77c367a0c23db 100644 --- a/test/hotspot/jtreg/runtime/BootstrapMethod/TestLambdaExceptionInInitializer.java +++ b/test/hotspot/jtreg/runtime/BootstrapMethod/TestLambdaExceptionInInitializer.java @@ -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 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)"); diff --git a/test/jdk/ProblemList-Virtual.txt b/test/jdk/ProblemList-Virtual.txt index 1cb3ea0859b79..2cfe6cfd655ed 100644 --- a/test/jdk/ProblemList-Virtual.txt +++ b/test/jdk/ProblemList-Virtual.txt @@ -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 diff --git a/test/lib/jdk/test/lib/process/ProcessTools.java b/test/lib/jdk/test/lib/process/ProcessTools.java index 115f2e96fc023..e49b17febf6c6 100644 --- a/test/lib/jdk/test/lib/process/ProcessTools.java +++ b/test/lib/jdk/test/lib/process/ProcessTools.java @@ -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 { @@ -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) { @@ -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; @@ -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); - } - } }