changeset 326:d48af41e33bf

Runners: Make sure we shutdown all handlers, regardless where they block. Also fixes ClassCastException in terminate().
author shade
date Fri, 07 Oct 2016 11:26:33 +0200
parents bfe8579a17fb
children 3b07f039cca7
files jcstress-core/src/main/java/org/openjdk/jcstress/link/BinaryLinkServer.java
diffstat 1 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/link/BinaryLinkServer.java	Wed Sep 28 15:02:15 2016 +0200
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/link/BinaryLinkServer.java	Fri Oct 07 11:26:33 2016 +0200
@@ -31,7 +31,8 @@
 
 import java.io.*;
 import java.net.*;
-import java.util.List;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.concurrent.*;
 
 /**
@@ -51,6 +52,7 @@
     private final TestResultCollector out;
     private final ConcurrentMap<Integer, TestConfig> configs;
     private final ExecutorService executor;
+    private final Collection<Handler> outstandingHandlers;
 
     public BinaryLinkServer(int workers, TestResultCollector out) throws IOException {
         this.out = out;
@@ -60,6 +62,7 @@
         server = new ServerSocket(LINK_PORT, 50, listenAddress);
         server.setSoTimeout(LINK_TIMEOUT_MS);
         executor = Executors.newFixedThreadPool(workers);
+        outstandingHandlers = Collections.newSetFromMap(new ConcurrentHashMap<>());
     }
 
     private InetAddress getListenAddress() {
@@ -77,15 +80,19 @@
     }
 
     public void terminate() {
+        // no more Handlers to schedule; the Handlers in queue had not
+        // opened the socket yet.
+        executor.shutdownNow();
+
+        // all existing Handlers blocked on accept() should exit now
         try {
             server.close();
         } catch (IOException e) {
             // do nothing
         }
 
-        List<Runnable> outstanding = executor.shutdownNow();
-        for (Runnable r : outstanding) {
-            Handler h = (Handler) r;
+        // all existing Handlers blocked on socket read should exit now:
+        for (Handler h : outstandingHandlers) {
             h.close();
         }
     }
@@ -114,6 +121,8 @@
 
         @Override
         public void run() {
+            outstandingHandlers.add(this);
+
             TestConfig config = null;
             try {
                 socket = server.accept();
@@ -154,6 +163,7 @@
                 tr.addAuxData(e.getMessage());
                 out.add(tr);
             } finally {
+                outstandingHandlers.remove(this);
                 close();
             }
         }