vscode-java-debug icon indicating copy to clipboard operation
vscode-java-debug copied to clipboard

Debug: Provide graceful shutdown on debug stop action

Open circlesmiler opened this issue 2 years ago • 7 comments
trafficstars

When stopping a debugging session, the application will be killed immediately without running shutdownHook code. There is the same discussion in the GO Lang project (https://github.com/golang/vscode-go/issues/120).

It would be nice to have the chance to decide, how the application will be killed (SIGKILL vs SIGTERM). Both are valid use-cases. Maybe it's even possible to have options on the STOP button of the debug panel. As we have multi-project workspaces, it really depends on the application (even situation) what you need.

Environment
  • Operating System: MacOS (But same on Windows/Linux)
  • JDK version: 11
  • Visual Studio Code version: 1.74.0
  • Java extension version: v0.25.7
  • Java Debugger extension version: v0.47.0
Steps To Reproduce
  1. Made a java application with an shutdownHook (Runtime.addShutdownHook)
  2. Start the application
  3. Stop the application by clicking the STOP button in the debug panel.
Current Result

The shutdownHook will not be executed.

Expected Result

The shutdownHook will be executed. (At least if the user chooses to)

Additional Informations

N/A

circlesmiler avatar Dec 15 '22 13:12 circlesmiler

Since the debugger is using JDI VirtualMachine#exit(int) API to stop the target debuggee, the debugger has little control over this behavior. I'm wondering if other IDEs support the capability you declare?

testforstephen avatar Feb 06 '23 03:02 testforstephen

@testforstephen IntelliJ does. 🤷‍♂️ We noticed because our Spring Boot project does some clean up stuff, when shutting down. The IntelliJ users have no problem, but VSCode users have. I'm not sure how IntelliJ is doing it.

circlesmiler avatar Feb 06 '23 08:02 circlesmiler

@testforstephen Eclipse also shuts down application gracefully

stefanrybacki avatar Jul 03 '23 07:07 stefanrybacki

The debug panel should work like the Node.js debug extension. The first press of STOP uses SIGINT. The second press of stop sends SIGKILL.

The 'restart' command in the debug panel should work similarly - or if the SIGINT doesn't kill the process in X seconds (configurable in the launch.json) a SIGKILL is sent. Then when the process dies and new one is created.

This is a serious problem because not running shutdown hooks can leave many applications in a corrupted state.

Note too, that if you kill the process using the 'delete icon' in the terminal panel it shuts down gracefully - so it seems this should be rather straightforward to fix.

robaho avatar Jul 20 '23 14:07 robaho

I believe the offending code is here - which is in the Java debug server.

This should first attempt to get the Process object, and if non-null, use process.destroy() else use the vm.exit()

robaho avatar Jul 20 '23 17:07 robaho

Yeah, I second this, I couldn't test my graceful shutdown procedure (without some manual hacks) through VS2022 debugging. Namely, my process starts other subprocesses and during its shutdown, is supposed to kill the child processes, but right now the parent just dies immediately leaving the child hanging and need to be killed via task manager.

Edit: Actually, I partly take that back. There is a way to do graceful shutdown, but, it would be nice for the stop button in Visual Studio to try doing a proper shutdown too.

image

totszwai avatar Mar 19 '24 13:03 totszwai