changeset 909:dfed92d1c32b

runners: detect System.exit() in forked VM.
author shade
date Wed, 16 Jul 2014 21:38:21 +0400
parents adbfd114a90b
children f46c7991c62f
files jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java
diffstat 1 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java	Wed Jul 16 21:19:06 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java	Wed Jul 16 21:38:21 2014 +0400
@@ -34,6 +34,8 @@
  */
 class ForkedMain {
 
+    private static volatile boolean gracefullyFinished;
+
     /**
      * Application main entry point
      *
@@ -64,6 +66,8 @@
                 // run!
                 ForkedRunner runner = new ForkedRunner(options, link);
                 runner.run();
+
+                gracefullyFinished = true;
             } catch (IOException ex) {
                 throw new IllegalArgumentException(ex.getMessage());
             } catch (ClassNotFoundException ex) {
@@ -77,6 +81,11 @@
                 new Thread() {
                     @Override
                     public void run() {
+                        if (!gracefullyFinished) {
+                            System.err.println("<failure: VM prematurely exited before JMH had finished with it, " +
+                                    "explicit System.exit was called?>");
+                        }
+
                         if (link != null) {
                             try {
                                 link.close();
@@ -84,6 +93,13 @@
                                 // swallow
                             }
                         }
+
+                        // If user did System.exit(0), we have to override the exit code
+                        // to let host VM know we encountered a problem. This should be done
+                        // after the link is flushed and down.
+                        if (!gracefullyFinished) {
+                            Runtime.getRuntime().halt(1);
+                        }
                     }
                 }
         );