Skip to content

Commit

Permalink
Made thread update reactive
Browse files Browse the repository at this point in the history
And avoid calling stacktrace when not suspended (some LS such as
vscode-js-debug may not answer in such case)
  • Loading branch information
mickaelistria committed Oct 5, 2023
1 parent 835b6be commit 46f5fa4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -31,13 +30,13 @@
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -79,7 +78,7 @@
import org.eclipse.lsp4j.debug.TerminatedEventArguments;
import org.eclipse.lsp4j.debug.Thread;
import org.eclipse.lsp4j.debug.ThreadEventArguments;
import org.eclipse.lsp4j.debug.ThreadsResponse;
import org.eclipse.lsp4j.debug.ThreadEventArgumentsReason;
import org.eclipse.lsp4j.debug.launch.DSPLauncher;
import org.eclipse.lsp4j.debug.services.IDebugProtocolClient;
import org.eclipse.lsp4j.debug.services.IDebugProtocolServer;
Expand Down Expand Up @@ -130,10 +129,6 @@ public class DSPDebugTarget extends DSPDebugElement implements IDebugTarget, IDe
* they are up to date (against the {@link #refreshThreads} flag).
*/
private final Map<Integer, DSPThread> threads = Collections.synchronizedMap(new TreeMap<>());
/**
* Set to true to update the threads list from the debug adapter.
*/
private final AtomicBoolean refreshThreads = new AtomicBoolean(true);

private boolean fTerminated = false;
private boolean fSentTerminateRequest = false;
Expand Down Expand Up @@ -556,46 +551,7 @@ public IProcess getProcess() {

@Override
public DSPThread[] getThreads() {
if (!refreshThreads.getAndSet(false)) {
synchronized (threads) {
Collection<DSPThread> values = threads.values();
return values.toArray(new DSPThread[values.size()]);
}
}
try {
var server = getDebugProtocolServer();
if (server == null) {
return new DSPThread[0];
}
CompletableFuture<ThreadsResponse> threads2 = server.threads();
CompletableFuture<DSPThread[]> future = threads2.thenApplyAsync(threadsResponse -> {
synchronized (threads) {
final var lastThreads = new TreeMap<Integer, DSPThread>(threads);
threads.clear();
Thread[] body = threadsResponse.getThreads();
for (Thread thread : body) {
DSPThread dspThread = lastThreads.get(thread.getId());
if (dspThread == null) {
dspThread = new DSPThread(this, thread.getId());
}
dspThread.update(thread);
threads.put(thread.getId(), dspThread);
// fireChangeEvent(DebugEvent.CONTENT);
}
Collection<DSPThread> values = threads.values();
return values.toArray(new DSPThread[values.size()]);
}
});
return future.get();
} catch (RuntimeException | ExecutionException e) {
if (isTerminated()) {
return new DSPThread[0];
}
DSPPlugin.logError(e);
} catch (InterruptedException e) {
java.lang.Thread.currentThread().interrupt();
}
return new DSPThread[0];
return threads.values().toArray(DSPThread[]::new);
}

/**
Expand Down Expand Up @@ -632,8 +588,31 @@ public void exited(ExitedEventArguments args) {

@Override
public void thread(ThreadEventArguments args) {
refreshThreads.set(true);
fireChangeEvent(DebugEvent.CONTENT);
// update according to current event
if (ThreadEventArgumentsReason.EXITED.equals(args.getReason())) {
threads.remove(args.getThreadId());
fireChangeEvent(DebugEvent.CONTENT);
} else if (ThreadEventArgumentsReason.STARTED.equals(args.getReason())) {
var newThread = new DSPThread(this, args.getThreadId());
threads.put(args.getThreadId(), newThread);
fireChangeEvent(DebugEvent.CONTENT);
}
// and trigger a further update
// then update threads
getDebugProtocolServer().threads().thenAcceptAsync(threadsResponse -> {
var threadIds = Arrays.stream(threadsResponse.getThreads()).map(Thread::getId).collect(Collectors.toSet());
if (threads.keySet().removeIf(Predicate.not(threadIds::contains))) {
fireChangeEvent(DebugEvent.CONTENT);
}
for (Thread thread : threadsResponse.getThreads()) {
DSPThread dspThread = threads.get(thread.getId());
if (dspThread == null) {
dspThread = new DSPThread(this, thread.getId());
fireChangeEvent(DebugEvent.CONTENT);
}
dspThread.update(thread);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ public DSPThread(DSPDebugTarget debugTarget, Integer threadId) {
*/
public void update(Thread thread) {
Assert.isTrue(Objects.equals(this.id, thread.getId()));
this.name = thread.getName();
if (!Objects.equals(this.name, thread.getName())) {
fireChangeEvent(DebugEvent.STATE);
this.name = thread.getName();
}
refreshFrames.set(true);
// fireChangeEvent(DebugEvent.STATE);
}

@Override
Expand Down Expand Up @@ -101,7 +103,7 @@ public void continued() {
public void stopped() {
isSuspended = true;
stepping = false;
refreshFrames.set(true);
frames.clear();
}

@Override
Expand Down Expand Up @@ -226,8 +228,10 @@ public IStackFrame getTopStackFrame() throws DebugException {
}
}

@Override
public IStackFrame[] getStackFrames() throws DebugException {
@Override public IStackFrame[] getStackFrames() throws DebugException {
if (!isSuspended()) {
return new IStackFrame[0];
}
if (!refreshFrames.getAndSet(false)) {
synchronized (frames) {
return frames.toArray(new DSPStackFrame[frames.size()]);
Expand Down

0 comments on commit 46f5fa4

Please sign in to comment.