Matei David
01/24/2025, 4:38 PM./gradlew runApp
(where runApp
is a JavaExec). It appears to me the Java process is killed outright (possibly with SIGKILL). I would much prefer to have the daemon send either TERM or INT, wait a few seconds, then KILL. This would give the Java main a chance to do a graceful shutdown.
My task is not a plain JavaExec due to config cache incompatibility, instead it's a regular Task with a doLast that includes ExecOperations.javaExec. I doubt that's relevant to the question though.Thomas Broyer
01/24/2025, 5:23 PMRuntime.getRuntime().addShutdownHook()
) is correctly called when Ctrl+C'ing a JavaExec task in my testing.Vampire
01/24/2025, 6:35 PMMatei David
01/24/2025, 6:40 PMaddShutdownHook
in build.gradle.kts
, but that adds the hook to the daemon process (2). When we click Ctrl-C in the terminal:
⢠GradleWrapperMain (1) gets the INT and disconnects from the daemon
⢠The daemon (2) prints a message like "client disconnection detected". It then (presumably) KILL-s the AppMain, but it doesn't shut down itself (unless using --no-daemon
I guess)
I don't see how shutdown hooks can help here.Thomas Broyer
01/24/2025, 6:58 PM./gradlew run --debug-jvm
, attached a debugger to my app, set a breakpoint in the shutdown hook, and the breakpoint was hit when I Ctrl+C the gradle wrapper in the terminal.Thomas Broyer
01/24/2025, 6:59 PMVampire
01/24/2025, 7:17 PMVampire
01/24/2025, 7:18 PMdestroyForcibly()
which should do a sigkill but a destroy()
, it should sent or at least try with a sigterm and thus give the process a chance to shutdown cleanly.Vampire
01/24/2025, 7:19 PMMatei David
01/24/2025, 7:22 PM^C
on a new line, then the next prompt.
In the daemon log file, the next line below the app output is thread 22: client disconnection detected, canceling the build
, followed by FAILURE: Build failed with an Exception
.
Actually, my App might get the INT but not have a chance for the output to make it into either the daemon log or the terminal.
Ideally, I would like to:
⢠Have the terminal display whatever my App is doing after receiving the INT (assuming it does, which I'm not sure)
⢠Have the build not marked as FAILED if the app stops in reasonable time say <10sVampire
01/24/2025, 7:29 PMMatei David
01/24/2025, 7:38 PMrunMyApp
. What bothers me is that if I click "stop" in the IDE (which is the functional equivalent of pressing Ctrl-C in the terminal), I expect my app to get the signal and have a chance to do an orderly shutdown.
So yes, technically I manually interrupted the Gradle JavaExec task, but if the app runs a few more seconds and stops cleanly by itself, I would much rather NOT mark the task as FAILED.Vampire
01/24/2025, 7:41 PMMatei David
01/24/2025, 7:41 PMrun-java
, pressing Ctrl-C has desired/expected the effect of showing the app output as it shuts down, followed by terminal exit code 0 success.Matei David
01/24/2025, 9:15 PMephemient
01/24/2025, 9:52 PMMatei David
01/24/2025, 11:48 PMephemient
01/25/2025, 5:53 AMplugin { application }
./gradlew install
build/install/*/bin/*
, or manually with https://docs.gradle.org/current/dsl/org.gradle.jvm.application.tasks.CreateStartScripts.html)Vampire
01/25/2025, 12:40 PMFor your consideration...
We are just users like you. If you want to suggest such changes officially, you need to open a feature request to see what the Gradle folks think.
Matei David
01/28/2025, 2:30 PMVampire
01/28/2025, 3:12 PM⢠Application RunConfig: In a Gradle build, it looks to me as if IntelliJ is creating some custom Gradle scripts to execute a Main class, which results in a (generated) JavaExec task, which has the exact same problem as a Gradle RunConfig.You can disable the default delegation to Gradle for running classes in the Gradle settings of the IDE:
Vampire
01/28/2025, 3:12 PMI'd like to put in a feature request for this, is the process described somewhere?Just open a new issue and select "Feature Request" as type, the template will tell you then what to fill in
Matei David
01/28/2025, 3:46 PMVampire
01/28/2025, 3:50 PMbuild/install/<appname>
that also works fine, I did that myself on some occasions too.Vampire
01/28/2025, 3:50 PMMatei David
01/28/2025, 3:51 PMVampire
01/28/2025, 3:52 PMSystem.exit(0)
or better orderly ends all non-daemon threads, then the JVM can just shut down normally and the Gradle build will be successful.