From 8a2c4034d79d16c110359f095f453ef217537a03 Mon Sep 17 00:00:00 2001 From: Fred Bricon Date: Thu, 2 Nov 2023 12:40:45 +0100 Subject: [PATCH] fix: prevent TimerTask errors from cancelling their Timer Signed-off-by: Fred Bricon --- .../intellij/lsp4ij/LanguageServerWrapper.java | 7 ++++++- .../classpath/ClasspathResourceChangedNotifier.java | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java index 535f94fff..b9cb8e43f 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java @@ -497,7 +497,12 @@ private void startStopTimer() { timer.schedule(new TimerTask() { @Override public void run() { - stop(); + try { + stop(); + } catch (Throwable t) { + //Need to catch time task exceptions, or it will cancel the timer + LOGGER.error("Failed to stop language server "+LanguageServerWrapper.this.serverDefinition.id, t); + } } }, TimeUnit.SECONDS.toMillis(this.serverDefinition.lastDocumentDisconnectedTimeout)); } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/classpath/ClasspathResourceChangedNotifier.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/classpath/ClasspathResourceChangedNotifier.java index dfb955f6a..9cb40b130 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/classpath/ClasspathResourceChangedNotifier.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/classpath/ClasspathResourceChangedNotifier.java @@ -24,6 +24,8 @@ import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.HashSet; import java.util.Set; @@ -35,6 +37,8 @@ */ public class ClasspathResourceChangedNotifier implements Disposable { + private static final Logger LOGGER = LoggerFactory.getLogger(ClasspathResourceChangedNotifier.class); + private static final long DEBOUNCE_DELAY = 1000; private final Project project; @@ -80,7 +84,12 @@ private void asyncNotifyChanges() { debounceTask = new TimerTask() { @Override public void run() { - notifyChanges(); + try { + notifyChanges(); + } catch (Throwable t) { + //Need to catch time task exceptions, or it will cancel the timer + LOGGER.error("Failed to notify classpath resource change", t); + } } };