Repository navigation
feat: support DAP terminate requests #645
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...re/src/main/java/com/microsoft/java/debug/core/adapter/handler/DebuggeeProcessHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 Microsoft Corporation and others. | ||
| * All rights reserved. This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License v1.0 | ||
| * which accompanies this distribution, and is available at | ||
| * http://www.eclipse.org/legal/epl-v10.html | ||
| * | ||
| * Contributors: | ||
| * Microsoft Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
|
|
||
| package com.microsoft.java.debug.core.adapter.handler; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import com.microsoft.java.debug.core.adapter.IDebugAdapterContext; | ||
|
|
||
| final class DebuggeeProcessHelper { | ||
| private DebuggeeProcessHelper() { | ||
| } | ||
|
|
||
| static void terminate(IDebugAdapterContext context) { | ||
| Process debuggeeProcess = context.getDebuggeeProcess(); | ||
| if (debuggeeProcess != null) { | ||
| debuggeeProcess.destroy(); | ||
| } else if (context.getProcessId() > 0) { | ||
| Optional<ProcessHandle> debuggeeHandle = ProcessHandle.of(context.getProcessId()); | ||
| if (debuggeeHandle.isPresent()) { | ||
| debuggeeHandle.get().destroy(); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
.../src/main/java/com/microsoft/java/debug/core/adapter/handler/TerminateRequestHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 Microsoft Corporation and others. | ||
| * All rights reserved. This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License v1.0 | ||
| * which accompanies this distribution, and is available at | ||
| * http://www.eclipse.org/legal/epl-v10.html | ||
| * | ||
| * Contributors: | ||
| * Microsoft Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
|
|
||
| package com.microsoft.java.debug.core.adapter.handler; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import com.microsoft.java.debug.core.IDebugSession; | ||
| import com.microsoft.java.debug.core.adapter.IDebugAdapterContext; | ||
| import com.microsoft.java.debug.core.adapter.IDebugRequestHandler; | ||
| import com.microsoft.java.debug.core.protocol.Messages.Response; | ||
| import com.microsoft.java.debug.core.protocol.Requests.Arguments; | ||
| import com.microsoft.java.debug.core.protocol.Requests.Command; | ||
|
|
||
| public class TerminateRequestHandler implements IDebugRequestHandler { | ||
| @Override | ||
| public List<Command> getTargetCommands() { | ||
| return Arrays.asList(Command.TERMINATE); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Response> handle(Command command, Arguments arguments, Response response, | ||
| IDebugAdapterContext context) { | ||
| if (context.isAttached()) { | ||
| return CompletableFuture.completedFuture(response); | ||
| } | ||
|
|
||
| IDebugSession debugSession = context.getDebugSession(); | ||
| if (debugSession != null) { | ||
| ThreadsRequestHandler.resumeAllThreads(context); | ||
| debugSession.terminate(); | ||
| } else { | ||
| DebuggeeProcessHelper.terminate(context); | ||
| } | ||
| return CompletableFuture.completedFuture(response); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
...st/java/com/microsoft/java/debug/core/adapter/handler/TerminateRequestHandlerJdiTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 Microsoft Corporation and others. | ||
| * All rights reserved. This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License v1.0 | ||
| * which accompanies this distribution, and is available at | ||
| * http://www.eclipse.org/legal/epl-v10.html | ||
| * | ||
| * Contributors: | ||
| * Microsoft Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
|
|
||
| package com.microsoft.java.debug.core.adapter.handler; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import java.util.Locale; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.junit.Assume; | ||
| import org.junit.Test; | ||
|
|
||
| import com.microsoft.java.debug.core.DebugSettings; | ||
| import com.microsoft.java.debug.core.DebugSessionFactory; | ||
| import com.microsoft.java.debug.core.IBreakpoint; | ||
| import com.microsoft.java.debug.core.IDebugSession; | ||
| import com.microsoft.java.debug.core.adapter.DebugAdapterContext; | ||
| import com.microsoft.java.debug.core.adapter.ProviderContext; | ||
| import com.microsoft.java.debug.core.protocol.Events.DebugEvent; | ||
| import com.microsoft.java.debug.core.protocol.IProtocolServer; | ||
| import com.microsoft.java.debug.core.protocol.Messages.Request; | ||
| import com.microsoft.java.debug.core.protocol.Messages.Response; | ||
| import com.microsoft.java.debug.core.protocol.Requests.Command; | ||
| import com.microsoft.java.debug.core.protocol.Requests.TerminateArguments; | ||
| import com.sun.jdi.event.BreakpointEvent; | ||
|
|
||
| import io.reactivex.disposables.Disposable; | ||
|
|
||
| public class TerminateRequestHandlerJdiTest { | ||
| private static final String PROJECT_NAME = "5.shutdownhook"; | ||
| private static final int READY_LINE = 15; | ||
| private static final int SHUTDOWN_HOOK_LINE = 20; | ||
|
|
||
| @Test | ||
| public void terminateShouldKeepDebuggerAttachedForShutdownHookBreakpoint() throws Exception { | ||
| Assume.assumeFalse(System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win")); | ||
|
|
||
| boolean suspendAllThreads = DebugSettings.getCurrent().suspendAllThreads; | ||
| DebugSettings.getCurrent().suspendAllThreads = true; | ||
| IDebugSession debugSession = null; | ||
| Process debuggeeProcess = null; | ||
| Disposable subscription = null; | ||
| CountDownLatch readyBreakpoint = new CountDownLatch(1); | ||
| CountDownLatch shutdownHookBreakpoint = new CountDownLatch(1); | ||
|
|
||
| try { | ||
| debugSession = DebugSessionFactory.getDebugSession(PROJECT_NAME, "ShutdownHookTest"); | ||
| assertNotNull(debugSession); | ||
| debuggeeProcess = debugSession.process(); | ||
| IBreakpoint ready = debugSession.createBreakpoint("ShutdownHookTest", READY_LINE, 0, null, null); | ||
| IBreakpoint shutdownHook = debugSession.createBreakpoint("ShutdownHookTest", SHUTDOWN_HOOK_LINE, 0, null, null); | ||
| subscription = debugSession.getEventHub().breakpointEvents().subscribe(debugEvent -> { | ||
| int line = ((BreakpointEvent) debugEvent.event).location().lineNumber(); | ||
| if (line == READY_LINE) { | ||
| debugEvent.shouldResume = false; | ||
| readyBreakpoint.countDown(); | ||
| } else if (line == SHUTDOWN_HOOK_LINE) { | ||
| debugEvent.shouldResume = false; | ||
| shutdownHookBreakpoint.countDown(); | ||
| } | ||
| }); | ||
|
|
||
| ready.install(); | ||
| shutdownHook.install(); | ||
| debugSession.start(); | ||
| debugSession.resume(); | ||
| assertTrue("The debuggee did not reach the ready breakpoint.", | ||
| readyBreakpoint.await(10, TimeUnit.SECONDS)); | ||
|
|
||
| DebugAdapterContext context = new DebugAdapterContext(new NoopProtocolServer(), new ProviderContext()); | ||
| context.setDebugSession(debugSession); | ||
| new TerminateRequestHandler() | ||
| .handle(Command.TERMINATE, new TerminateArguments(), new Response(), context).join(); | ||
|
|
||
| assertFalse(context.isVmTerminated()); | ||
| assertTrue("The shutdown hook breakpoint was not hit.", | ||
| shutdownHookBreakpoint.await(10, TimeUnit.SECONDS)); | ||
| } finally { | ||
| try { | ||
| dispose(subscription); | ||
| terminate(debuggeeProcess); | ||
| shutdownDebugSession(); | ||
| } finally { | ||
| DebugSettings.getCurrent().suspendAllThreads = suspendAllThreads; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void dispose(Disposable subscription) { | ||
| if (subscription == null) { | ||
| return; | ||
| } | ||
| try { | ||
| subscription.dispose(); | ||
| } catch (RuntimeException ex) { | ||
| System.err.println("Failed to dispose the breakpoint subscription: " + ex.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private static void terminate(Process debuggeeProcess) { | ||
| if (debuggeeProcess == null) { | ||
| return; | ||
| } | ||
| try { | ||
| if (debuggeeProcess.isAlive()) { | ||
| debuggeeProcess.destroyForcibly(); | ||
| } | ||
| if (!debuggeeProcess.waitFor(5, TimeUnit.SECONDS)) { | ||
| System.err.println("The shutdown-hook test process did not exit within five seconds."); | ||
| } | ||
| } catch (InterruptedException ex) { | ||
| Thread.currentThread().interrupt(); | ||
| System.err.println("Interrupted while waiting for the shutdown-hook test process to exit."); | ||
| } catch (RuntimeException ex) { | ||
| System.err.println("Failed to terminate the shutdown-hook test process: " + ex.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private static void shutdownDebugSession() { | ||
| try { | ||
| DebugSessionFactory.shutdownDebugSession(PROJECT_NAME); | ||
| } catch (Exception ex) { | ||
| System.err.println("Failed to shut down the test debug session: " + ex.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private static class NoopProtocolServer implements IProtocolServer { | ||
| @Override | ||
| public CompletableFuture<Response> sendRequest(Request request) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Response> sendRequest(Request request, long timeout) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| @Override | ||
| public void sendEvent(DebugEvent event) { | ||
| } | ||
|
|
||
| @Override | ||
| public void sendResponse(Response response) { | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.