Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE when the server is stopped due to a failure during initialization #192

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 49 additions & 52 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,60 +228,52 @@ public synchronized void start() throws IOException {
if (this.initializeFuture == null) {
final URI rootURI = getRootURI();
this.initializeFuture = CompletableFuture.supplyAsync(() -> {
if (LoggingStreamConnectionProviderProxy.shouldLog(serverDefinition.id)) {
this.lspStreamProvider = new LoggingStreamConnectionProviderProxy(
serverDefinition.createConnectionProvider(), serverDefinition.id);
} else {
this.lspStreamProvider = serverDefinition.createConnectionProvider();
}
initParams.setInitializationOptions(this.lspStreamProvider.getInitializationOptions(rootURI));
try {
if (LoggingStreamConnectionProviderProxy.shouldLog(serverDefinition.id)) {
this.lspStreamProvider = new LoggingStreamConnectionProviderProxy(
serverDefinition.createConnectionProvider(), serverDefinition.id);
} else {
this.lspStreamProvider = serverDefinition.createConnectionProvider();
}
initParams.setInitializationOptions(this.lspStreamProvider.getInitializationOptions(rootURI));
lspStreamProvider.start();
} catch (Exception e) {
LanguageServerPlugin.logError(e);
initializeFuture.completeExceptionally(e);
stop();
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}).thenApply(unused -> {
try {
LanguageClientImpl client = serverDefinition.createLanguageClient();
String theardNameFormat = "LS-" + serverDefinition.id + "-launcher-%d"; //$NON-NLS-1$ //$NON-NLS-2$
ExecutorService executorService = Executors
.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat(theardNameFormat).build());
initParams.setProcessId((int) ProcessHandle.current().pid());

if(rootURI != null) {
initParams.setRootUri(rootURI.toString());
initParams.setRootPath(rootURI.getPath());
}

UnaryOperator<MessageConsumer> wrapper = consumer -> (message -> {
consumer.consume(message);
logMessage(message);
final StreamConnectionProvider currentConnectionProvider = this.lspStreamProvider;
if (currentConnectionProvider != null && isActive()) {
currentConnectionProvider.handleMessage(message, this.languageServer, rootURI);
}
});
initParams.setWorkspaceFolders(Arrays.stream(ResourcesPlugin.getWorkspace().getRoot().getProjects())
.filter(IProject::isAccessible).map(LSPEclipseUtils::toWorkspaceFolder)
.filter(Objects::nonNull).collect(Collectors.toList()));
Launcher<LanguageServer> launcher = serverDefinition.createLauncherBuilder().setLocalService(client)//
.setRemoteInterface(serverDefinition.getServerInterface())//
.setInput(lspStreamProvider.getInputStream())//
.setOutput(lspStreamProvider.getOutputStream())//
.setExecutorService(executorService)//
.wrapMessages(wrapper)//
.create();
this.languageServer = launcher.getRemoteProxy();
client.connect(languageServer, this);
this.launcherFuture = launcher.startListening();
} catch (Exception ex) {
LanguageServerPlugin.logError(ex);
stop();
initializeFuture.completeExceptionally(ex);
LanguageClientImpl client = serverDefinition.createLanguageClient();
String theardNameFormat = "LS-" + serverDefinition.id + "-launcher-%d"; //$NON-NLS-1$ //$NON-NLS-2$
ExecutorService executorService = Executors
.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat(theardNameFormat).build());
initParams.setProcessId((int) ProcessHandle.current().pid());

if (rootURI != null) {
initParams.setRootUri(rootURI.toString());
initParams.setRootPath(rootURI.getPath());
}

UnaryOperator<MessageConsumer> wrapper = consumer -> (message -> {
consumer.consume(message);
logMessage(message);
final StreamConnectionProvider currentConnectionProvider = this.lspStreamProvider;
if (currentConnectionProvider != null && isActive()) {
currentConnectionProvider.handleMessage(message, this.languageServer, rootURI);
}
});
initParams.setWorkspaceFolders(Arrays.stream(ResourcesPlugin.getWorkspace().getRoot().getProjects())
.filter(IProject::isAccessible).map(LSPEclipseUtils::toWorkspaceFolder).filter(Objects::nonNull)
.collect(Collectors.toList()));
Launcher<LanguageServer> launcher = serverDefinition.createLauncherBuilder().setLocalService(client)//
.setRemoteInterface(serverDefinition.getServerInterface())//
.setInput(lspStreamProvider.getInputStream())//
.setOutput(lspStreamProvider.getOutputStream())//
.setExecutorService(executorService)//
.wrapMessages(wrapper)//
.create();
this.languageServer = launcher.getRemoteProxy();
client.connect(languageServer, this);
this.launcherFuture = launcher.startListening();
return null;
}).thenCompose(unused -> {
String name = "Eclipse IDE"; //$NON-NLS-1$
Expand Down Expand Up @@ -368,13 +360,16 @@ public synchronized void start() throws IOException {
try {
connect(fileToReconnect.getKey(), fileToReconnect.getValue());
} catch (IOException e) {
LanguageServerPlugin.logError(e);
stop();
initializeFuture.completeExceptionally(e);
throw new RuntimeException(e);
}
}
});
FileBuffers.getTextFileBufferManager().addFileBufferListener(fileBufferListener);
}).exceptionally(e -> {
LanguageServerPlugin.logError(e);
initializeFuture.completeExceptionally(e);
stop();
return null;
});
}
}
Expand Down Expand Up @@ -720,7 +715,9 @@ protected IStatus run(IProgressMonitor monitor) {
waitForInitialization.setSystem(false);
PlatformUI.getWorkbench().getProgressService().showInDialog(UI.getActiveShell(), waitForInitialization);
}
return initializeFuture.thenApply(r -> this.languageServer);
if (initializeFuture != null) {
return initializeFuture.thenApply(r -> this.languageServer);
}
}
return CompletableFuture.completedFuture(this.languageServer);
}
Expand Down