From 50e70c3f02bf05b24459e9df23e1933d6ef5832e Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 18 Aug 2017 22:08:36 +0200 Subject: [PATCH] SONAR-9734 fix file leaks --- .../issue/TrackerRawInputFactory.java | 4 +++- .../LoadCrossProjectDuplicationsRepositoryStep.java | 12 ++++++++---- .../step/PersistFileSourcesStep.java | 13 +++++-------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/TrackerRawInputFactory.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/TrackerRawInputFactory.java index be172abfac0..9de67f6b610 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/TrackerRawInputFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/TrackerRawInputFactory.java @@ -77,7 +77,9 @@ public class TrackerRawInputFactory { protected LineHashSequence loadLineHashSequence() { List lines; if (component.getType() == Component.Type.FILE) { - lines = newArrayList(sourceLinesRepository.readLines(component)); + try (CloseableIterator linesIt = sourceLinesRepository.readLines(component)) { + lines = newArrayList(linesIt); + } } else { lines = Collections.emptyList(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStep.java index e34bdc8b387..3994aba0a02 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStep.java @@ -25,6 +25,7 @@ import java.util.List; import javax.annotation.Nonnull; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.core.util.CloseableIterator; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.duplication.DuplicationUnitDto; @@ -96,10 +97,13 @@ public class LoadCrossProjectDuplicationsRepositoryStep implements ComputationSt @Override public void visitFile(Component file) { - List cpdTextBlocks = newArrayList(reportReader.readCpdTextBlocks(file.getReportAttributes().getRef())); - LOGGER.trace("Found {} cpd blocks on file {}", cpdTextBlocks.size(), file.getKey()); - if (cpdTextBlocks.isEmpty()) { - return; + List cpdTextBlocks; + try (CloseableIterator blocksIt = reportReader.readCpdTextBlocks(file.getReportAttributes().getRef())) { + cpdTextBlocks = newArrayList(blocksIt); + LOGGER.trace("Found {} cpd blocks on file {}", cpdTextBlocks.size(), file.getKey()); + if (cpdTextBlocks.isEmpty()) { + return; + } } Collection hashes = from(cpdTextBlocks).transform(CpdTextBlockToHash.INSTANCE).toList(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStep.java index e0d324a89c5..79a8aeec726 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistFileSourcesStep.java @@ -122,17 +122,13 @@ public class PersistFileSourcesStep implements ComputationStep { return; } - CloseableIterator linesIterator = sourceLinesRepository.readLines(file); - LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file); - try { + try (CloseableIterator linesIterator = sourceLinesRepository.readLines(file); + LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, duplicationRepository, file)) { ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, lineReaders.readers(), file.getFileAttributes().getLines()); ComputeFileSourceData.Data fileSourceData = computeFileSourceData.compute(); persistSource(fileSourceData, file.getUuid(), lineReaders.getLatestChange()); } catch (Exception e) { throw new IllegalStateException(String.format("Cannot persist sources of %s", file.getKey()), e); - } finally { - linesIterator.close(); - lineReaders.close(); } } @@ -188,7 +184,7 @@ public class PersistFileSourcesStep implements ComputationStep { } } - private static class LineReaders { + private static class LineReaders implements AutoCloseable { private final List readers = new ArrayList<>(); private final List> closeables = new ArrayList<>(); @CheckForNull @@ -223,7 +219,8 @@ public class PersistFileSourcesStep implements ComputationStep { return readers; } - void close() { + @Override + public void close() { for (CloseableIterator reportIterator : closeables) { reportIterator.close(); } -- 2.39.5