Skip to content

Commit

Permalink
fix executor resource warning since 19
Browse files Browse the repository at this point in the history
Potential resource leak: 'executor' may not be closed at this location
  • Loading branch information
jukzi committed Jan 14, 2025
1 parent 7b9da15 commit d809c8e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,18 +449,19 @@ public void accept(CompletionProposal proposal) {

requestor.setAllowsRequiredProposals(CompletionProposal.CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);

final ExecutorService executor= Executors.newSingleThreadExecutor();
try {
Future<?> future= executor.submit(() -> {
try {
fCompilationUnit.codeComplete(fNode.getStartPosition() + SKIP_NEW_KEYWORD, requestor, new CompletionTimeoutProgressMonitor());
} catch (JavaModelException e) {
// do nothing
}
});
future.get(1, TimeUnit.SECONDS);
} catch (final Exception e) {
executor.shutdownNow();
try (ExecutorService executor= Executors.newSingleThreadExecutor()) {
try {
Future<?> future= executor.submit(() -> {
try {
fCompilationUnit.codeComplete(fNode.getStartPosition() + SKIP_NEW_KEYWORD, requestor, new CompletionTimeoutProgressMonitor());
} catch (JavaModelException e) {
// do nothing
}
});
future.get(1, TimeUnit.SECONDS);
} catch (final Exception e) {
executor.shutdownNow();
}
}

IJavaProject project= fCompilationUnit.getJavaProject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,35 +124,36 @@ public void analyzeSelectedExpression(ASTNode selectedExpression) {
}

public boolean hasConflict() {
ExecutorService threadPool= new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10), new ThreadPoolExecutor.CallerRunsPolicy());
try {
for (ASTNode node : fMiddleNodes) {
if (fConflict) {
break;
}
Position pos= new Position(node.getStartPosition(), node.getLength());
if (fPosSet.add(pos)) {
threadPool.execute(() -> {
UpdateVisitor uv= new UpdateVisitor(fDependSet, true);
node.accept(uv);
if (uv.hasConflict()) {
fConflict= true;
}
});
try (ExecutorService threadPool= new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10), new ThreadPoolExecutor.CallerRunsPolicy())) {
try {
for (ASTNode node : fMiddleNodes) {
if (fConflict) {
break;
}
Position pos= new Position(node.getStartPosition(), node.getLength());
if (fPosSet.add(pos)) {
threadPool.execute(() -> {
UpdateVisitor uv= new UpdateVisitor(fDependSet, true);
node.accept(uv);
if (uv.hasConflict()) {
fConflict= true;
}
});
}
}
}
if (!fConflict) {
threadPool.shutdown();
while (!threadPool.isTerminated() && !fConflict) {
threadPool.awaitTermination(10, TimeUnit.MILLISECONDS);
if (!fConflict) {
threadPool.shutdown();
while (!threadPool.isTerminated() && !fConflict) {
threadPool.awaitTermination(10, TimeUnit.MILLISECONDS);
}
}
} catch (InterruptedException e) {
} finally {
threadPool.shutdownNow();
}
} catch (InterruptedException e) {
} finally {
threadPool.shutdownNow();
return fConflict;
}
return fConflict;
}
private class FunctionSearchRequestor extends SearchRequestor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,26 @@ private List<ICompletionProposal> executeCallChainSearch() {

final List<ChainType> expectedTypes= ChainElementAnalyzer.resolveBindingsForExpectedTypes(ctx.getProject(), ctx.getCoreContext());
final ChainFinder finder= new ChainFinder(expectedTypes, Arrays.asList(excludedTypes), invocationType);
final ExecutorService executor= Executors.newSingleThreadExecutor();
try {
Future<?> future= executor.submit(() -> {
if (findEntrypoints()) {
finder.startChainSearch(entrypoints, maxChains, minDepth, maxDepth);
}
});

long timeout;
try (ExecutorService executor= Executors.newSingleThreadExecutor()) {
try {
timeout= Long.parseLong(JavaManipulation.getPreference(PreferenceConstants.PREF_CHAIN_TIMEOUT, ctx.getProject()));
} catch (NumberFormatException e) {
timeout = 1;
Future<?> future= executor.submit(() -> {
if (findEntrypoints()) {
finder.startChainSearch(entrypoints, maxChains, minDepth, maxDepth);
}
});

long timeout;
try {
timeout= Long.parseLong(JavaManipulation.getPreference(PreferenceConstants.PREF_CHAIN_TIMEOUT, ctx.getProject()));
} catch (NumberFormatException e) {
timeout= 1;
}
future.get(timeout, TimeUnit.SECONDS);
} catch (final Exception e) {
finder.cancel();
executor.shutdownNow();
setError("Timeout during call chain computation."); //$NON-NLS-1$
}
future.get(timeout, TimeUnit.SECONDS);
} catch (final Exception e) {
finder.cancel();
executor.shutdownNow();
setError("Timeout during call chain computation."); //$NON-NLS-1$
}
return buildCompletionProposals(finder.getChains());
}
Expand Down

0 comments on commit d809c8e

Please sign in to comment.