Describe the bug
To perform a graceful shutdown of containers we need a way to catch the signals (SIGTERM/SIGINT) that Docker sends when shutting down a container. In yarn v1 the workaround was to use node <script> directly instead of yarn run. However I can't seem to get this to work using yarn node <script> or yarn run in v2 with PnP enabled.
To Reproduce
Use a minimal Dockerfile:
FROM node:12.18.3
WORKDIR /opt/app
COPY . /opt/app
RUN yarn install
CMD ["yarn", "node", "./src"]
Inside src/index.js, listen for signals
const signals = [SIGHUP, SIGINT, SIGTERM]
for (const signal of signals) {
process.on(signal, () => {
console.log(`process received a ${signal} signal`)
})
}
Then when the container kill it using:
docker kill -s SIGTERM <container name>
Environment if relevant (please complete the following information):
Additional context
I also tried using docker-init/tini to play around the PID 1 restriction after reading https://github.com/yarnpkg/berry/issues/991 but that doesn't seem to work either. It would be nice if there is some way to forward signals to the child processes so that yarn v2 works nicely with Docker.
As a workaround you can use
node -r ./.pnp.js ./src
Can you try master (yarn set version from sources)? Should be fixed with https://github.com/yarnpkg/berry/issues/1622
@arcanis hmm no unfortunately it still doesn't work using master (2.1.1-git.20200822.133f4025)
I confused it with https://github.com/yarnpkg/berry/issues/991, which you mentioned in your post, sorry. I think we intended to forward the signals, but it slipped past our radar after the issue got closed. If you're interested to add this logic, I'd be happy to review that. It shouldn't be too hard, there are only two places where we spawn child processes:
https://github.com/yarnpkg/berry/search?q=siginthandler&unscoped_q=siginthandler
Most helpful comment
As a workaround you can use