4646import jdk .vm .ci .runtime .JVMCIRuntime ;
4747
4848import java .io .File ;
49- import java .io .IOException ;
5049import java .lang .reflect .Method ;
5150import java .nio .file .Files ;
5251import java .nio .file .Path ;
5352import java .util .List ;
53+ import java .util .concurrent .TimeUnit ;
5454import java .util .concurrent .atomic .AtomicInteger ;
5555
5656public class TestUncaughtErrorInCompileMethod extends JVMCIServiceLocator {
5757
58- /**
59- * Name of file whose existence implies that a JVMCICompiler has been created.
60- */
61- static String tmpFileName = "ErrorCompilerCreated." + System .nanoTime ();
58+ static volatile boolean compilerCreationErrorOccurred ;
6259
6360 /**
6461 * @param args if args.length != 0, then executing in subprocess
@@ -68,19 +65,11 @@ public static void main(String[] args) throws Exception {
6865 testSubprocess (false );
6966 testSubprocess (true );
7067 } else {
71- File watch = new File (tmpFileName );
7268 int total = 0 ;
73- long start = System .currentTimeMillis ();
74-
75- // Use a 10 sec timeout to prevent endless loop if
76- // JVMCI compiler creation fails
77- while (System .currentTimeMillis () - start < 10_000 ) {
69+ while (!compilerCreationErrorOccurred ) {
70+ // Do some random work to trigger compilation
7871 total += getTime ();
79- if (watch .exists ()) {
80- System .err .println ("saw " + watch + " - exiting loop" );
81- watch .delete ();
82- break ;
83- }
72+ total += String .valueOf (total ).hashCode ();
8473 }
8574 System .out .println (total );
8675 }
@@ -101,7 +90,16 @@ static void testSubprocess(boolean fatalError) throws Exception {
10190 "-XX:+PrintWarnings" ,
10291 "-Xbootclasspath/a:." ,
10392 TestUncaughtErrorInCompileMethod .class .getName (), "true" );
104- OutputAnalyzer output = new OutputAnalyzer (pb .start ());
93+ Process p = pb .start ();
94+ OutputAnalyzer output = new OutputAnalyzer (p );
95+
96+ if (!waitForProcess (p )) {
97+ // The subprocess might not enter JVMCI compilation.
98+ // Print the subprocess output and pass the test in this case.
99+ System .out .println (output .getOutput ());
100+ return ;
101+ }
102+
105103 if (fatalError ) {
106104 output .shouldContain ("testing JVMCI fatal exception handling" );
107105 output .shouldNotHaveExitValue (0 );
@@ -137,6 +135,31 @@ static void testSubprocess(boolean fatalError) throws Exception {
137135 }
138136 }
139137
138+ /**
139+ * @return true if {@code p} exited on its own, false if it had to be destroyed
140+ */
141+ private static boolean waitForProcess (Process p ) {
142+ while (true ) {
143+ try {
144+ boolean exited = p .waitFor (10 , TimeUnit .SECONDS );
145+ if (!exited ) {
146+ System .out .println ("destroying process: " + p );
147+ p .destroy ();
148+ Thread .sleep (1000 );
149+ while (p .isAlive ()) {
150+ System .out .println ("forcibly destroying process: " + p );
151+ Thread .sleep (1000 );
152+ p .destroyForcibly ();
153+ }
154+ return false ;
155+ }
156+ return true ;
157+ } catch (InterruptedException e ) {
158+ e .printStackTrace (System .out );
159+ }
160+ }
161+ }
162+
140163 public TestUncaughtErrorInCompileMethod () {
141164 }
142165
@@ -161,18 +184,10 @@ public JVMCICompiler createCompiler(JVMCIRuntime runtime) {
161184 int attempt = counter .incrementAndGet ();
162185 CompilerCreationError e = new CompilerCreationError (attempt );
163186 e .printStackTrace ();
164- if (attempt == 10 ) {
165- // Delay the creation of the file that causes the
166- // loop in main to exit so that compilation failures
187+ if (attempt >= 10 ) {
188+ // Delay notifying the loop in main so that compilation failures
167189 // have time to be reported by -XX:+PrintCompilation.
168- File watch = new File (tmpFileName );
169- try {
170- System .err .println ("creating " + watch );
171- watch .createNewFile ();
172- System .err .println ("created " + watch );
173- } catch (IOException ex ) {
174- ex .printStackTrace ();
175- }
190+ compilerCreationErrorOccurred = true ;
176191 }
177192 throw e ;
178193 }
0 commit comments