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 race condition in LanguageServerWrapper resulting in NullPointerException during LS initialization #985

Merged
merged 3 commits into from
May 8, 2024
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
29 changes: 16 additions & 13 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -157,7 +158,7 @@ public void dirtyStateChanged(IFileBuffer buffer, boolean isDirty) {
protected StreamConnectionProvider lspStreamProvider;
private Future<?> launcherFuture;
private CompletableFuture<Void> initializeFuture;
private IProgressMonitor initializeFutureMonitor;
private final AtomicReference<IProgressMonitor> initializeFutureMonitorRef = new AtomicReference<>();
private final int initializeFutureNumberOfStages = 7;
private LanguageServer languageServer;
private LanguageClientImpl languageClient;
Expand Down Expand Up @@ -286,7 +287,7 @@ private synchronized void start(boolean forceRestart) throws IOException {
final Job job = createInitializeLanguageServerJob();
this.launcherFuture = new CompletableFuture<>();
this.initializeFuture = CompletableFuture.supplyAsync(() -> {
advanceinitializeFutureMonitor();
advanceInitializeFutureMonitor();
if (LoggingStreamConnectionProviderProxy.shouldLog(serverDefinition.id)) {
this.lspStreamProvider = new LoggingStreamConnectionProviderProxy(
serverDefinition.createConnectionProvider(), serverDefinition.id);
Expand All @@ -297,11 +298,11 @@ private synchronized void start(boolean forceRestart) throws IOException {
try {
lspStreamProvider.start();
} catch (IOException e) {
throw new RuntimeException(e);
throw new UncheckedIOException(e);
}
return null;
}).thenRun(() -> {
advanceinitializeFutureMonitor();
advanceInitializeFutureMonitor();
languageClient = serverDefinition.createLanguageClient();

initParams.setProcessId((int) ProcessHandle.current().pid());
Expand Down Expand Up @@ -333,18 +334,18 @@ private synchronized void start(boolean forceRestart) throws IOException {
this.launcherFuture = launcher.startListening();
})
.thenCompose(unused -> {
advanceinitializeFutureMonitor();
advanceInitializeFutureMonitor();
return initServer(rootURI);
})
.thenAccept(res -> {
advanceinitializeFutureMonitor();
advanceInitializeFutureMonitor();
serverCapabilities = res.getCapabilities();
this.initiallySupportsWorkspaceFolders = supportsWorkspaceFolders(serverCapabilities);
}).thenRun(() -> {
advanceinitializeFutureMonitor();
advanceInitializeFutureMonitor();
this.languageServer.initialized(new InitializedParams());
}).thenRun(() -> {
advanceinitializeFutureMonitor();
advanceInitializeFutureMonitor();
final Map<URI, IDocument> toReconnect = filesToReconnect;
initializeFuture.thenRunAsync(() -> {
watchProjects();
Expand All @@ -357,7 +358,7 @@ private synchronized void start(boolean forceRestart) throws IOException {
}
});
FileBuffers.getTextFileBufferManager().addFileBufferListener(fileBufferListener);
advanceinitializeFutureMonitor();
advanceInitializeFutureMonitor();
}).exceptionally(e -> {
stop();
final Throwable cause = e.getCause();
Expand All @@ -378,7 +379,8 @@ private synchronized void start(boolean forceRestart) throws IOException {
}
}

private void advanceinitializeFutureMonitor() {
private void advanceInitializeFutureMonitor() {
final var initializeFutureMonitor = initializeFutureMonitorRef.get();
if (initializeFutureMonitor != null) {
if (initializeFutureMonitor.isCanceled()) {
throw new CancellationException();
Expand All @@ -387,11 +389,12 @@ private void advanceinitializeFutureMonitor() {
}
}

private synchronized Job createInitializeLanguageServerJob() {
private Job createInitializeLanguageServerJob() {
return new Job(NLS.bind(Messages.initializeLanguageServer_job, serverDefinition.label)) {
@Override
protected IStatus run(IProgressMonitor monitor) {
initializeFutureMonitor = SubMonitor.convert(monitor, initializeFutureNumberOfStages);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is a silly question, but do we need an AtomicReference? Could we not achieve the same by saving initializeFutureMonitor to a local variable in line 394 (and also on line 382) before saving it to the class field, and then mark the monitor as done using the local variable?

Is there a benefit of AtomicReference over the local variable?

Copy link
Member Author

@sebthom sebthom May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I see it, at a minimum the initializeFutureMonitor needs to be made volatile because it is accessed from two different threads and the Job in createInitializeLanguageServerJob otherwise may never see the assigned value. The nice part of AtomicReference is that it offers compareAndSet() which can avoid another type of race conditions when the wrapper is stopped and restarted in quick succession. Also I find working with AtomicReference is more expressive regarding the intention than dealing with a volatile field.

final var initializeFutureMonitor = SubMonitor.convert(monitor, initializeFutureNumberOfStages);
initializeFutureMonitorRef.set(initializeFutureMonitor);
CompletableFuture<Void> currentInitializeFuture = initializeFuture;
try {
if (currentInitializeFuture != null) {
Expand All @@ -405,7 +408,7 @@ protected IStatus run(IProgressMonitor monitor) {
}
} finally {
initializeFutureMonitor.done();
initializeFutureMonitor = null;
initializeFutureMonitorRef.compareAndSet(initializeFutureMonitor, null);
}
return Status.OK_STATUS;
}
Expand Down