Skip to content

Commit 4e0ae8e

Browse files
committed
fix: Perform updates to Stop Language Server Timer synchronously. (#905)
Fixes #905 Signed-off-by: azerr <[email protected]>
1 parent 2473f4e commit 4e0ae8e

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java

+29-21
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import com.redhat.devtools.intellij.lsp4ij.internal.SupportedFeatures;
2525
import com.redhat.devtools.intellij.lsp4ij.server.ProcessStreamConnectionProvider;
2626
import com.redhat.devtools.intellij.lsp4ij.server.StreamConnectionProvider;
27-
import com.redhat.devtools.intellij.lsp4ij.settings.ServerTrace;
28-
import com.redhat.devtools.intellij.lsp4ij.settings.UserDefinedLanguageServerSettings;
2927
import com.redhat.devtools.intellij.lsp4ij.lifecycle.LanguageServerLifecycleManager;
3028
import com.redhat.devtools.intellij.lsp4ij.lifecycle.NullLanguageServerLifecycleManager;
3129
import org.eclipse.lsp4j.*;
@@ -114,7 +112,11 @@ public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile f
114112
private LanguageServer languageServer;
115113
private LanguageClientImpl languageClient;
116114
private ServerCapabilities serverCapabilities;
117-
private Timer timer;
115+
116+
private final Timer timer = new Timer("Stop Language Server Task Processor"); //$NON-NLS-1$
117+
118+
private TimerTask stopTimerTask;
119+
118120
private final AtomicBoolean stopping = new AtomicBoolean(false);
119121

120122
private final ExecutorService dispatcher;
@@ -295,7 +297,7 @@ public synchronized void start() throws IOException {
295297
messageBusConnection = ApplicationManager.getApplication().getMessageBus().connect();
296298
messageBusConnection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, fileBufferListener);
297299
messageBusConnection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, fileBufferListener);
298-
getLanguageServerLifecycleManager().onStartedLanguageServer(this, null);
300+
getLanguageServerLifecycleManager().onStartedLanguageServer(this, null);
299301
}).exceptionally(e -> {
300302
LOGGER.error("Error while starting language server '" + serverDefinition.id + "'", e);
301303
initializeFuture.completeExceptionally(e);
@@ -372,23 +374,30 @@ private void logMessage(Message message, MessageConsumer consumer) {
372374
getLanguageServerLifecycleManager().logLSPMessage(message, consumer, this);
373375
}
374376

375-
private void removeStopTimer() {
376-
if (timer != null) {
377-
timer.cancel();
378-
timer = null;
379-
getLanguageServerLifecycleManager().onStartedLanguageServer(this, null);
377+
private void removeStopTimerTask() {
378+
synchronized (timer) {
379+
if (stopTimerTask != null) {
380+
stopTimerTask.cancel();
381+
stopTimerTask = null;
382+
getLanguageServerLifecycleManager().onStartedLanguageServer(this, null);
383+
}
380384
}
381385
}
382386

383-
private void startStopTimer() {
384-
timer = new Timer("Stop Language Server Timer"); //$NON-NLS-1$
385-
getLanguageServerLifecycleManager().onStoppingLanguageServer(this);
386-
timer.schedule(new TimerTask() {
387-
@Override
388-
public void run() {
389-
stop();
387+
private void startStopTimerTask() {
388+
synchronized (timer) {
389+
if (stopTimerTask != null) {
390+
stopTimerTask.cancel();
390391
}
391-
}, TimeUnit.SECONDS.toMillis(this.serverDefinition.lastDocumentDisconnectedTimeout));
392+
getLanguageServerLifecycleManager().onStoppingLanguageServer(this);
393+
stopTimerTask = new TimerTask() {
394+
@Override
395+
public void run() {
396+
stop();
397+
}
398+
};
399+
timer.schedule(stopTimerTask, TimeUnit.SECONDS.toMillis(this.serverDefinition.lastDocumentDisconnectedTimeout));
400+
}
392401
}
393402

394403
/**
@@ -413,7 +422,7 @@ public synchronized void stop() {
413422
return;
414423
}
415424
getLanguageServerLifecycleManager().onStoppingLanguageServer(this);
416-
removeStopTimer();
425+
removeStopTimerTask();
417426
if (this.languageClient != null) {
418427
this.languageClient.dispose();
419428
}
@@ -619,7 +628,7 @@ private boolean supportsWorkspaceFolderCapability() {
619628
* @noreference internal so far
620629
*/
621630
private CompletableFuture<LanguageServer> connect(@Nonnull URI absolutePath, Document document) throws IOException {
622-
removeStopTimer();
631+
removeStopTimerTask();
623632
final URI thePath = absolutePath; // should be useless
624633

625634
VirtualFile file = FileDocumentManager.getInstance().getFile(document);
@@ -677,8 +686,7 @@ private void disconnect(URI path, boolean stopping) {
677686
}
678687
if (!stopping && this.connectedDocuments.isEmpty()) {
679688
if (this.serverDefinition.lastDocumentDisconnectedTimeout != 0 && !ApplicationManager.getApplication().isUnitTestMode()) {
680-
removeStopTimer();
681-
startStopTimer();
689+
startStopTimerTask();
682690
} else {
683691
stop();
684692
}

0 commit comments

Comments
 (0)