From 614f659835e0010f70cc8a0c4ccc332e6d06b4aa Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Fri, 20 Feb 2015 22:00:56 +0100 Subject: [PATCH] SONAR-6036 Close the shared memory sooner --- .../org/sonar/process/ProcessCommands.java | 8 ++++++- .../java/org/sonar/process/StopWatcher.java | 22 +++++++++++-------- .../java/org/sonar/process/StopperThread.java | 1 + 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/server/sonar-process/src/main/java/org/sonar/process/ProcessCommands.java b/server/sonar-process/src/main/java/org/sonar/process/ProcessCommands.java index 0e7edb39af8..0edd51d3456 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ProcessCommands.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ProcessCommands.java @@ -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 { * */ 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; } diff --git a/server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java b/server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java index 65ffb533c78..08e2f5f45c5 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java +++ b/server/sonar-process/src/main/java/org/sonar/process/StopWatcher.java @@ -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(); } } diff --git a/server/sonar-process/src/main/java/org/sonar/process/StopperThread.java b/server/sonar-process/src/main/java/org/sonar/process/StopperThread.java index 23819212cfc..764d551a93c 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/StopperThread.java +++ b/server/sonar-process/src/main/java/org/sonar/process/StopperThread.java @@ -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(); } } -- 2.39.5