]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6036 Close the shared memory sooner
authorEric Hartmann <hartmann.eric@gmail.com>
Fri, 20 Feb 2015 21:00:56 +0000 (22:00 +0100)
committerEric Hartmann <hartmann.eric@gmail.com>
Fri, 20 Feb 2015 21:00:56 +0000 (22:00 +0100)
server/sonar-process/src/main/java/org/sonar/process/ProcessCommands.java
server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java
server/sonar-process/src/main/java/org/sonar/process/StopperThread.java

index 0e7edb39af82bfa9a1be3f17820b8cd3670659e4..0edd51d34565770dea8853d048edd5347e4be84f 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.process;
 
+import org.apache.commons.io.IOUtils;
 import org.slf4j.LoggerFactory;
 
 import java.io.File;
@@ -60,6 +61,7 @@ public class ProcessCommands {
    * </ul>
    */
   final MappedByteBuffer mappedByteBuffer;
+  private final RandomAccessFile sharedMemory;
   private static final int MAX_PROCESSES = 50;
   private static final int BYTE_LENGTH_FOR_ONE_PROCESS = 1 + 1 + 8;
   private static final int MAX_SHARED_MEMORY = BYTE_LENGTH_FOR_ONE_PROCESS * MAX_PROCESSES; // With this shared memory we can handle up to MAX_PROCESSES processes
@@ -80,7 +82,7 @@ public class ProcessCommands {
     }
 
     try {
-      RandomAccessFile sharedMemory = new RandomAccessFile(new File(directory, "sharedmemory"), "rw");
+      sharedMemory = new RandomAccessFile(new File(directory, "sharedmemory"), "rw");
       mappedByteBuffer = sharedMemory.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, MAX_SHARED_MEMORY);
     } catch (IOException e) {
       throw new IllegalArgumentException("Unable to create shared memory : ", e);
@@ -125,6 +127,10 @@ public class ProcessCommands {
     return mappedByteBuffer.get(offset() + 1) == STOP;
   }
 
+  public void endWatch() {
+    IOUtils.closeQuietly(sharedMemory);
+  }
+
   int offset() {
     return BYTE_LENGTH_FOR_ONE_PROCESS * processNumber;
   }
index 65ffb533c7865bb8d6fe854d0fbd1926c363090c..08e2f5f45c5bdd3b1926d23f300c1881ded73eda 100644 (file)
@@ -45,18 +45,22 @@ public class StopWatcher extends Thread {
 
   @Override
   public void run() {
-    while (watching) {
-      if (commands.askedForStop()) {
-        LoggerFactory.getLogger(getClass()).info("Stopping process");
-        stoppable.stopAsync();
-        watching = false;
-      } else {
-        try {
-          Thread.sleep(delayMs);
-        } catch (InterruptedException ignored) {
+    try {
+      while (watching) {
+        if (commands.askedForStop()) {
+          LoggerFactory.getLogger(getClass()).info("Stopping process");
+          stoppable.stopAsync();
           watching = false;
+        } else {
+          try {
+            Thread.sleep(delayMs);
+          } catch (InterruptedException ignored) {
+            watching = false;
+          }
         }
       }
+    } finally {
+      commands.endWatch();
     }
   }
 
index 23819212cfc67df8cdebdfe29a9dffa762c90a76..764d551a93c623a7e9b1202814fd7ad987f683af 100644 (file)
@@ -57,5 +57,6 @@ class StopperThread extends Thread {
       LoggerFactory.getLogger(getClass()).error(String.format("Can not stop in %dms", terminationTimeout), e);
     }
     executor.shutdownNow();
+    commands.endWatch();
   }
 }