buildpacks icon indicating copy to clipboard operation
buildpacks copied to clipboard

Improve documentation for Nodejs developers who wish to catch SIGTERM

Open dimmetrius opened this issue 2 years ago • 7 comments

Hello!

found a problem with a simple nodejs application deployed to Cloud Run. The problem is that the Cloud Run container never receives the SIGTERM event on instance Graceful shutdown

see https://github.com/dimmetrius/cloudRunHello

step 1 build container: npm build step 2 deploy container: npm deploy

commands used: gcloud --project ${PROJECT} builds submit --pack=image=gcr.io/${PROJECT}/cloudrunhello,env=GOOGLE_RUNTIME="nodejs"

gcloud --project ${PROJECT} run deploy cloudrunhello --image=gcr.io/${PROJECT}/cloudrunhello --region=us-central1 --platform=managed

dimmetrius avatar Nov 17 '21 21:11 dimmetrius

If we compare deploying this sample with Dockerfile and with Buildpacks

The node app is able to catch the SIGTERM signal when using Dockerfile but not Builpacks, this is why we believe this is an issue with buildpacks

steren avatar Nov 17 '21 22:11 steren

@dimmetrius this is a long standing issue with NPM. By default the nodejs buildpacks will run your app by calling npm run start. Unfortunately this causes npm to swallow SIGTERM. You can override the default startup process by including a Procfile with a web process that invokes node directly. So, for your sample app, the Procfile would be:

web: node index.js

matthewrobertson avatar Nov 18 '21 00:11 matthewrobertson

@matthewrobertson Thank you! After Procfile adding the application started to receive SIGTERM signal

dimmetrius avatar Nov 18 '21 10:11 dimmetrius

Thank you so much for helping resolve our issue!!

In the interest of being charitable to others who inevitably will stub their toe against this also, is there a good way of suggesting a feature request for the nodejs buildpack team or at least having a prominent findable FAQ that addresses the problem and one-line fix?

dweekly avatar Nov 18 '21 16:11 dweekly

I suggest we re-open this issue and only close it when either:

  1. GCP Buildpacks have a better default experience that would result in the SIGTERM signal not being swallowed when possible
  2. the use of Procfile for Node.js startup commands is better documented

steren avatar Nov 18 '21 17:11 steren

@steren I agree on 2. I have re-opened the issue.

On 1, I don't think we can change the default behaviour as npm start has a bunch of side effects that customers might be relying on (eg running prestart scripts). Changing this now would be a breaking change.

matthewrobertson avatar Nov 18 '21 19:11 matthewrobertson

Thanks Steren & Matthew. I wonder; could a build-time or run-time warning be issued when building or running a nodejs image with no Procfile?

"WARNING: No Procfile detected. Will assume 'npm start'; signals like SIGTERM may go untrapped by your script. See https://X.Y.Z/ for more info"

dweekly avatar Nov 18 '21 20:11 dweekly