gradle-node-plugin icon indicating copy to clipboard operation
gradle-node-plugin copied to clipboard

Kill NpmTask when gradle is stopped

Open pmendelski opened this issue 8 years ago • 11 comments

Hi, first of all thanks for a great gradle plugin.

One of my tasks in gradle starts an infinite node task called watch. This task is infinite it means it stops on Ctrl + c. Unfortunately when I kill gradle task the SIGKILL does not propagate to the npm task.

task watchWeb(type: NpmTask, dependsOn: ["installNpmModules"]) {
    args = ["run", "watch"]
}

Terminal execution:

./gradlew watchWeb
<Ctrl+c>
ps aux | grep node
<node task is still running>

Could you tell how to propagate the SIGKILL to npm task?

pmendelski avatar Jun 30 '17 08:06 pmendelski

This issue affects Grails 3.3.0 Angular projects when executed with client:bootRun. After the server is started via the gradle task, stopping the gradle task with Ctrl+C does not stop the node.exe process.

The next time I go to start up the server, I get the "port is already in use" error.

See: https://github.com/grails-profiles/angular/issues/12

rmorrise avatar Aug 24 '17 16:08 rmorrise

Just to summarize it. There is a workaround for this problem described in https://github.com/srs/gradle-node-plugin/issues/143#issuecomment-328406484

./gradlew watchWeb --no-daemon
<Ctrl+c>
ps aux | grep node
<node task is not running>

...although IMHO it's a workaround not a solution. That is why I do not close this issue.

pmendelski avatar Oct 25 '17 11:10 pmendelski

@mendlik this seems to be a limitation of Gradle daemon (gradle/gradle#1128) and I do not believe this can be fixed within the plugin. Maybe we could communicate the impact to the Gradle team to motivate them to provide a solution here.

orange-buffalo avatar Nov 07 '17 13:11 orange-buffalo

@orange-buffalo good idea. I have just created a topic on gradle discuss. Let's start there.

pmendelski avatar Nov 08 '17 08:11 pmendelski

any update on this issue ?

ayZagen avatar Mar 20 '18 13:03 ayZagen

With Gradle 4.8 it should now be possible to fix this issue by implementing the Cancellable Interface: https://github.com/gradle/gradle/pull/5206

rbuehlma avatar Jun 08 '18 14:06 rbuehlma

This bothers us on the CI server (GitLab CI, but it does not matter I guess). If we cancel the build when the Node plugin is running already, it will finish the build anyway. While Gradle does not kill it, CI server waits for it and any next build is pending for many minutes. And we run the builds with --no-daemon, but it does not help in our case - it seems like there is still this one-off daemon: "Daemon will be stopped at the end of the build stopping after processing"

virgo47 avatar Mar 19 '19 11:03 virgo47

Same here. We are running a server using an NPM script (npm start). Even by setting --no-daemon the server keeps running after CTRL+C'ing out. A fix for this would be higly appreciated!

kristian avatar Apr 04 '19 06:04 kristian

any update ?

atali avatar Jul 02 '20 19:07 atali

If anyone is interested in creating a solution to this there's further context in https://github.com/node-gradle/gradle-node-plugin/issues/65#issuecomment-581840403

But this is something that should be fixed in Gradle and with Java 9+ it's supposedly easy to do.

deepy avatar Jul 03 '20 14:07 deepy

I also recently encountered this issue and I made a custom workaround based on Gradle Shared Build Services (https://docs.gradle.org/current/userguide/build_services.html). The idea is that the service is attached to npm tasks and kills them properly (i.e. also killing dependents processes) in case the Gradle task is stopped. If it may be of any use to someone else, I made a small Gradle plugin for it https://github.com/PeredurOmega/GradleNpmPlugin mainly resolving this issue + automatically defining tasks for scripts defined in package.json.

PeredurOmega avatar Feb 23 '23 16:02 PeredurOmega