Search code examples
javaintellij-idea

Using IntelliJ IDEA to debug shutdown code?


I'm noticing an unexpected and undesirable behavior out of IntelliJ's debugger that I'm wondering if anyone knows anything about.

Our Spring app has some fairly complex shutdown logic. We make use of Spring's "Disposable Bean" interface, and we also register our own JVM shutdown hook. I often find myself wanting to stop in the debugger in this code. I'd love to be able to do so by pressing the IntelliJ debugger's Stop button and having my breakpoints be hit. This is the behavior I expected.

When I press the Stop button in the IntelliJ debugger, my breakpoints in my shutdown code are not hit. At first, I thought that maybe IntelliJ was hard-killing my app rather than sending a termination signal (I believe it sends a SIGINT) to the process. But I have since confirmed that my shutdown code runs when I press the Stop button. The behavior is as though IntelliJ unregisters my breakpoints before sending the SIGINT signal to my app.

My workaround is to send a SIGINT to my app from a shell prompt. When I do this, my breakpoints get hit. This is a pain. So I'm wondering if anyone has either a way to cause the breakpoints to be hit when the Stop button is pressed, or at least some information as to why the debugger behaves this way.


Solution

  • Just as @Rogue guessed, when you click the stop button, the IDEA disconnects the debugger and sends SIGINT to the app.

    There is no setting to configure this behavior currently. Check the related issue here for details: https://youtrack.jetbrains.com/issue/IDEA-170313/Breakpoints-not-working-after-stop-signal

    As a workaround, you could open Settings | Tools | External Tools, add a script that could find your java process, and send the SIGINT. Then assign a shortcut for it or add a menu in the IDEA's Toolbar in Settings | Appearance & Behavior | Menus and Toolbars so you could use it easily like the stop button.