Skip to content

Commit

Permalink
fix: propagate LanguagServerWrapper startup exception to consumers
Browse files Browse the repository at this point in the history
If there is an exception in the startup of the LanguageServerWrapper,
consumers get a cancellation exception rather than the actual exception
that occurred.
With this commit, this behavior is changed. As a consequence of the
change, calls to LanguageServerWrapper.start() will do nothing after a
failure. To test for this case (and attempt a restart() if feasible),
the startupFailed() method is added.
  • Loading branch information
ava-fred committed Aug 30, 2024
1 parent 75d300b commit 2c30b2a
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ private List<WorkspaceFolder> getRelevantWorkspaceFolders() {
}

/**
* Starts a language server and triggers initialization. If language server is
* started and active, does nothing. If language server is inactive, restart it.
* Starts a language server and triggers initialization. If language server has been started
* before, does nothing.
*/
public synchronized void start() {
start(false);
Expand Down Expand Up @@ -278,7 +278,7 @@ private synchronized void start(boolean forceRestart) {
stop();
}
}
if (this.initializeFuture == null) {
if (this.initializeFuture == null || forceRestart) {
final URI rootURI = getRootURI();
final Job job = createInitializeLanguageServerJob();
this.launcherFuture = new CompletableFuture<>();
Expand Down Expand Up @@ -356,7 +356,7 @@ private synchronized void start(boolean forceRestart) {
FileBuffers.getTextFileBufferManager().addFileBufferListener(fileBufferListener);
advanceInitializeFutureMonitor();
}).exceptionally(e -> {
stop();
shutdown();
final Throwable cause = e.getCause();
if (cause instanceof CancellationException c) {
throw c;
Expand Down Expand Up @@ -486,6 +486,13 @@ public synchronized boolean isActive() {
return this.launcherFuture != null && !this.launcherFuture.isDone() && !this.launcherFuture.isCancelled();
}

/**
* @return whether the last startup attempt has failed
*/
public synchronized boolean startupFailed() {
return this.initializeFuture != null && this.initializeFuture.isCompletedExceptionally();
}

private void removeStopTimerTask() {
synchronized (timer) {
if (stopTimerTask != null) {
Expand Down Expand Up @@ -521,6 +528,14 @@ boolean isWrapperFor(LanguageServer server) {
}

public synchronized void stop() {
if (this.initializeFuture != null) {
initializeFuture.cancel(true);
this.initializeFuture= null;
}
shutdown();
}

private void shutdown() {
final boolean alreadyStopping = this.stopping.getAndSet(true);
if (alreadyStopping) {
return;
Expand All @@ -531,11 +546,6 @@ public synchronized void stop() {
this.languageClient.dispose();
}

if (this.initializeFuture != null) {
this.initializeFuture.cancel(true);
this.initializeFuture = null;
}

this.serverCapabilities = null;
this.dynamicRegistrations.clear();

Expand Down

0 comments on commit 2c30b2a

Please sign in to comment.