microsoft / vscode-java-debug

Java Debugger for Visual Studio Code.
Other
529 stars 339 forks source link

Debug: Provide graceful shutdown on debug stop action #1274

Open circlesmiler opened 1 year ago

circlesmiler commented 1 year ago

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
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

testforstephen commented 1 year ago

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?

circlesmiler commented 1 year ago

@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.

stefanrybacki commented 1 year ago

@testforstephen Eclipse also shuts down application gracefully

robaho commented 1 year ago

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 commented 1 year ago

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()

totszwai commented 6 months ago

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

mozhuanzuojing commented 1 week ago

现在有消息吗?