]> WPIA git - gigi.git/commitdiff
Fix: SimpleSigner Thread safety
authorBenny Baumann <BenBE@geshi.org>
Sun, 1 Mar 2015 00:45:35 +0000 (01:45 +0100)
committerFelix Dörre <felix@dogcraft.de>
Sun, 1 Mar 2015 01:42:39 +0000 (02:42 +0100)
util-testing/org/cacert/gigi/util/SimpleSigner.java

index a9ed46456a20680b0621d994183190c420b5be3e..eba8f8529d887b81d82505fd51a3c80daaa86884 100644 (file)
@@ -47,7 +47,7 @@ public class SimpleSigner {
 
     private static GigiPreparedStatement finishJob;
 
-    private static boolean running = true;
+    private static volatile boolean running = true;
 
     private static Thread runner;
 
@@ -68,14 +68,17 @@ public class SimpleSigner {
         runSigner();
     }
 
-    public synchronized static void stopSigner() throws InterruptedException {
-        if (runner == null) {
-            throw new IllegalStateException("already stopped");
+    public static void stopSigner() throws InterruptedException {
+        Thread capturedRunner;
+        synchronized (SimpleSigner.class) {
+            if (runner == null) {
+                throw new IllegalStateException("already stopped");
+            }
+            capturedRunner = runner;
+            running = false;
+            SimpleSigner.class.notifyAll();
         }
-        running = false;
-        runner.interrupt();
-        runner.join();
-        runner = null;
+        capturedRunner.join();
     }
 
     public synchronized static void runSigner() throws SQLException, IOException, InterruptedException {
@@ -111,7 +114,7 @@ public class SimpleSigner {
         runner.start();
     }
 
-    private static void work() {
+    private synchronized static void work() {
         try {
             gencrl();
         } catch (IOException e2) {
@@ -119,11 +122,13 @@ public class SimpleSigner {
         } catch (InterruptedException e2) {
             e2.printStackTrace();
         }
+
         while (running) {
             try {
                 signCertificates();
                 revokeCertificates();
-                Thread.sleep(5000);
+
+                SimpleSigner.class.wait(5000);
             } catch (IOException e) {
                 e.printStackTrace();
             } catch (SQLException e) {
@@ -131,6 +136,7 @@ public class SimpleSigner {
             } catch (InterruptedException e1) {
             }
         }
+        runner = null;
     }
 
     private static void revokeCertificates() throws SQLException, IOException, InterruptedException {