changeset 907:9d5a70899026

runners: tolerate System.exit in forked VM, gracefully shutdown the binary link.
author shade
date Wed, 16 Jul 2014 21:04:31 +0400
parents 92b43417a72c
children adbfd114a90b
files jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java
diffstat 1 files changed, 20 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java	Wed Jul 16 21:00:01 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/ForkedMain.java	Wed Jul 16 21:04:31 2014 +0400
@@ -53,6 +53,8 @@
 
                 // establish the link to host VM and pull the options
                 link = new BinaryLinkClient(hostName, hostPort);
+                addShutdownHook(link);
+
                 Options options = link.requestOptions();
 
                 // dump outputs into binary link
@@ -66,16 +68,26 @@
                 throw new IllegalArgumentException(ex.getMessage());
             } catch (ClassNotFoundException ex) {
                 throw new IllegalArgumentException(ex.getMessage());
-            } finally {
-                if (link != null) {
-                    try {
-                        link.close();
-                    } catch (IOException e) {
-                        // swallow
-                    }
-                }
             }
         }
     }
 
+    private static void addShutdownHook(final BinaryLinkClient link) {
+        Runtime.getRuntime().addShutdownHook(
+                new Thread() {
+                    @Override
+                    public void run() {
+                        if (link != null) {
+                            try {
+                                link.close();
+                            } catch (IOException e) {
+                                // swallow
+                            }
+                        }
+                    }
+                }
+        );
+
+    }
+
 }