Skip to content

Commit

Permalink
Support termination of virtual DSPProcess
Browse files Browse the repository at this point in the history
The DSPProcess not always maps to an actual process, but when it does,
we use it to control termination actions and state, and if it doesn't we
failback to an internal state.

This makes that the "Terminate" button on the Console works.
  • Loading branch information
mickaelistria committed Oct 3, 2023
1 parent 9409bcf commit e2bb297
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
package org.eclipse.lsp4e.debug.console;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.lsp4e.debug.debugmodel.DSPDebugTarget;
Expand All @@ -27,6 +31,7 @@ public class DSPProcess implements IProcess {
private final DSPStreamsProxy proxy;
private final ProcessEventArguments processArgs;
private final Optional<ProcessHandle> handle;
private boolean terminated;

public DSPProcess(DSPDebugTarget target) {
this(target, null);
Expand All @@ -50,16 +55,22 @@ public <T> T getAdapter(Class<T> adapter) {

@Override
public boolean canTerminate() {
return target.canTerminate();
return !isTerminated();
}

@Override
public boolean isTerminated() {
return target.isTerminated();
return handle.map(h -> !h.isAlive()).orElse(terminated);
}

@Override
public void terminate() throws DebugException {
terminated = true;
handle.ifPresent(h -> {
h.destroy(); // normal termination
CompletableFuture.runAsync(() -> h.destroyForcibly(), CompletableFuture.delayedExecutor(5, TimeUnit.SECONDS)); // forced termination if normal is not sufficient
});
DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { new DebugEvent(this, DebugEvent.TERMINATE) });
target.terminate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,16 +310,17 @@ private void terminated() {
try {
t.terminate();
} catch (DebugException e) {
// TODO Auto-generated catch block
e.printStackTrace();
DSPPlugin.logError(e);
}
});
fireTerminateEvent();
if (process != null) {
// Disable the terminate button of the console associated with the DSPProcess.
DebugPlugin.getDefault()
.fireDebugEventSet(new DebugEvent[] { new DebugEvent(process, DebugEvent.TERMINATE) });
if (process != null && process.canTerminate()) {
try {
process.terminate();
} catch (DebugException e) {
DSPPlugin.logError(e);
}
}
fireTerminateEvent();
if (breakpointManager != null) {
breakpointManager.shutdown();
}
Expand Down

0 comments on commit e2bb297

Please sign in to comment.