Berry: [Bug] Unable to catch signals using Docker

Created on 23 Aug 2020  路  4Comments  路  Source: yarnpkg/berry

  • [ ] I'd be willing to implement a fix

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):

  • OS: Docker
  • Node version 12.18.3
  • Yarn version 2.1.1

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.

bug good first issue

Most helpful comment

As a workaround you can use

node -r ./.pnp.js ./src

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bradleyayers picture bradleyayers  路  3Comments

thealjey picture thealjey  路  4Comments

GaoxinHuang picture GaoxinHuang  路  3Comments

IanVS picture IanVS  路  4Comments

larixer picture larixer  路  4Comments