Skip to content

Commit 8ed60f5

Browse files
authored
fix(intelligence): close ExecutorService via try-with-resources (SonarCloud S2095) (#54)
The virtual-thread executor was shut down in a finally block, which is architecturally correct but triggers SonarCloud's S2095 blocker bug. Java 19+ ExecutorService implements AutoCloseable; try-with-resources invokes close() which safely shuts down and awaits termination. Each future.get() already has a 5-minute timeout + cancel, so by the time close() runs, tasks are terminal and close() returns promptly. The manual 10s+5s shutdown timing is superseded by AutoCloseable contract.
1 parent 046ea70 commit 8ed60f5

1 file changed

Lines changed: 4 additions & 15 deletions

File tree

src/main/java/io/github/randomcodespace/iq/intelligence/extractor/LanguageEnricher.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ record FileTask(String filePath, List<CodeNode> fileNodes, LanguageExtractor ext
122122
var edgesAdded = new java.util.concurrent.atomic.AtomicInteger(0);
123123
var typeHintsAdded = new java.util.concurrent.atomic.AtomicInteger(0);
124124

125-
var executor = Executors.newVirtualThreadPerTaskExecutor();
126-
try {
125+
// try-with-resources: ExecutorService#close() handles shutdown + awaitTermination.
126+
// Each future.get() below has its own 5-minute timeout + cancel, so by the time
127+
// close() runs, all tasks are terminal and close() returns promptly.
128+
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
127129
List<Future<?>> futures = new ArrayList<>(tasks.size());
128130
for (FileTask task : tasks) {
129131
futures.add(executor.submit(() -> {
@@ -183,19 +185,6 @@ record FileTask(String filePath, List<CodeNode> fileNodes, LanguageExtractor ext
183185
break;
184186
}
185187
}
186-
} finally {
187-
executor.shutdown();
188-
try {
189-
if (!executor.awaitTermination(10, TimeUnit.SECONDS)) {
190-
executor.shutdownNow();
191-
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
192-
log.warn("Language enrichment executor did not terminate cleanly");
193-
}
194-
}
195-
} catch (InterruptedException e) {
196-
executor.shutdownNow();
197-
Thread.currentThread().interrupt();
198-
}
199188
}
200189

201190
edges.addAll(newEdges);

0 commit comments

Comments
 (0)