changeset 324:11b5f55dcddc

7901806: Gracefully shutdown forked tests Summary: Make sure the binary link is always closed properly. Make sure interrupt tests are running with interruptible tests.
author shade
date Wed, 28 Sep 2016 13:12:06 +0200
parents 62580530cd03
children bfe8579a17fb
files jcstress-core/src/main/java/org/openjdk/jcstress/ForkedMain.java jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java
diffstat 2 files changed, 28 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/ForkedMain.java	Wed Sep 28 11:48:05 2016 +0200
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/ForkedMain.java	Wed Sep 28 13:12:06 2016 +0200
@@ -28,6 +28,8 @@
 import org.openjdk.jcstress.link.BinaryLinkClient;
 import org.openjdk.jcstress.vm.WhiteBoxSupport;
 
+import java.io.IOException;
+
 /**
  * Entry point for the forked VM run.
  *
@@ -51,10 +53,33 @@
         int token = Integer.valueOf(args[2]);
 
         BinaryLinkClient link = new BinaryLinkClient(host, port);
+        Runtime.getRuntime().addShutdownHook(new CloseBinaryLinkHook(link));
+
         TestConfig config = link.nextJob(token);
+        new JCStress(null).runEmbedded(config, link);
+    }
 
-        new JCStress(null).runEmbedded(config, link);
-        link.close();
+    /**
+     * Shutdown hook dedicated to properly closing the binary link.
+     * Can be used for regular or exceptional shutdown.
+     */
+    private static class CloseBinaryLinkHook extends Thread {
+        private final BinaryLinkClient link;
+
+        CloseBinaryLinkHook(BinaryLinkClient link) {
+            this.link = link;
+        }
+
+        @Override
+        public void run() {
+            try {
+                link.close();
+            } catch (IOException e) {
+                // IOException on closing the link means we can only communicate the
+                // failure via the exit code.
+                Runtime.getRuntime().halt(1);
+            }
+        }
     }
 
 }
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java	Wed Sep 28 11:48:05 2016 +0200
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java	Wed Sep 28 13:12:06 2016 +0200
@@ -782,6 +782,7 @@
         pw.println("                    holder.terminated = true;");
         pw.println("                }");
         pw.println("            });");
+        pw.println("            t1.setDaemon(true);");
         pw.println("            t1.start();");
         pw.println();
         pw.println("            while (!holder.started) {");