Skip to content

Commit

Permalink
fix: prevent ConcurrentModificationException in CancellationSupport.c…
Browse files Browse the repository at this point in the history
…ancel

Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon authored and angelozerr committed Aug 25, 2023
1 parent e38cc1d commit a0e94de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
******************************************************************************/
package com.redhat.devtools.intellij.lsp4ij.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;

import com.intellij.openapi.progress.ProcessCanceledException;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;

/**
Expand All @@ -32,20 +33,25 @@ public class CancellationSupport implements CancelChecker {
private boolean cancelled;

public CancellationSupport() {
this.futuresToCancel = new ArrayList<CompletableFuture<?>>();
this.futuresToCancel = new CopyOnWriteArrayList<>();
this.cancelled = false;
}

public <T> CompletableFuture<T> execute(CompletableFuture<T> future) {
this.futuresToCancel.add(future);
if (cancelled) {
future.cancel(true);
throw new ProcessCanceledException();
} else {
this.futuresToCancel.add(future);
}
return future;
}

/**
* Cancel all LSP requests.
*/
public void cancel() {
if (cancelled == true) {
if (cancelled) {
return;
}
this.cancelled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private List<LSPHighlightPsiElement> getTargets(Editor editor, PsiFile file) {
.map(languageServer -> cancellationSupport.execute(languageServer.getServer().getTextDocumentService().documentHighlight(params)))
.map(request -> request.thenAcceptAsync(result -> {
if (result != null) {
result.forEach(hightlight -> highlights.add(hightlight));
highlights.addAll(result);
}
})).toArray(CompletableFuture[]::new))));
while (!future.isDone() || !highlights.isEmpty()) {
Expand Down

0 comments on commit a0e94de

Please sign in to comment.