How npm start Swallowed SIGTERM and Made Our Graceful Shutdown Code Dead on Every Deploy
← Back
November 21, 2026NodeJS8 min read

How npm start Swallowed SIGTERM and Made Our Graceful Shutdown Code Dead on Every Deploy

Published November 21, 20268 min read

16:08 UTC, Thursday. Support pings the channel: a customer got the same invoice email twice, then a second customer, then a third. Our deploy finished at 16:04.


the scramble

I get paged because the invoice worker is mine. Two other engineers join. The pattern in the support tickets is small but real: 22 duplicate invoice emails, all sent between 16:04 and 16:06. No duplicates the day before, and nothing since.

First theory: the deploy changed something in the job logic. The release was a dependency bump and a copy change in the email template. I diff it twice and find nothing near the send path.

Second theory: the queue redelivered. The worker pulls jobs from a Redis-backed queue with a 60-second lock per job. If a job takes longer than the lock, another worker can claim it. I check job durations. The slowest invoice job that afternoon took 9 seconds. The lock was never in play.


the hunt

Duplicates only during the deploy window means the old pods are involved. A rolling update sends SIGTERM to each old pod, and we have a handler for that. I open it:

let shuttingDown = false; process.on('SIGTERM', async () => { shuttingDown = true; console.log('SIGTERM received, draining'); await worker.close(); // stop taking jobs, wait for active ones await redis.quit(); process.exit(0); });

This is the textbook version and it looks right to me. worker.close() waits for the active job to finish and acknowledge. If this ran, no job would be abandoned mid-send. So I search the logs for the line it prints.

$ kubectl logs invoice-worker-7d9f-xk2lp --previous | grep -c "SIGTERM received" 0

Zero. Across all six old pods. The handler never ran. That kills the "bug in the handler" theory and leaves a stranger one: the process never got the signal.

Pod events give the next clue. Each old pod shows Killing at 16:04:02 and the container exiting at 16:04:32 with exit code 137. That is exactly 30 seconds, our terminationGracePeriodSeconds. Exit 137 is SIGKILL. Kubernetes asked nicely, waited the full grace period, then killed the container. During those 30 seconds the worker kept pulling new jobs as if nothing had happened.

The kill is the duplicate source. A job that had sent the email but not yet acknowledged was cut off by SIGKILL, its lock expired, and a new pod ran it again. About 20 jobs were in flight at that moment across six pods. That matches the 22 duplicates.

Now, why no signal? I check the image's entrypoint:

FROM node:20-slim WORKDIR /app COPY . . RUN npm ci --omit=dev CMD ["npm", "start"]

And package.json:

"scripts": { "start": "node dist/worker.js" }

I exec into a running pod and look at the process tree:

$ ps -o pid,ppid,args PID PPID ARGS 1 0 npm start 18 1 sh -c node dist/worker.js 19 18 node dist/worker.js

the find

PID 1 is npm, not node. Kubernetes sends SIGTERM to PID 1 in the container. npm does not forward signals to the child it spawns, and the sh -c layer in between does not either. Our Node process, PID 19, sat two levels down and never heard anything.

That also explains why nobody noticed for months. The handler worked on every developer laptop, because locally we run node dist/worker.js directly and press Ctrl+C. The path that production used was never the path anyone tested. Until the previous release it was also rare for a job to be mid-send at the moment of a deploy. The release before had doubled the send volume.


the fix

Node has to be the process that receives the signal. First change: run node directly in the image so there is no npm or shell in between:

FROM node:20-slim WORKDIR /app COPY . . RUN npm ci --omit=dev USER node CMD ["node", "dist/worker.js"]

With the exec form of CMD, Node becomes PID 1 and receives SIGTERM directly. One caveat I checked rather than assumed: a process running as PID 1 gets no default signal handlers from the kernel, so the handler we wrote must exist, and it does. We do not need an init process for this worker because it spawns no children, but if it did we would add tini.

Second, the handler needed two corrections I only noticed on re-reading it with the real timeline in mind. It had no upper bound, so a stuck job could still run into the SIGKILL, and it did not stop taking jobs before waiting:

const DRAIN_TIMEOUT_MS = 25_000; // grace period is 30s, leave headroom let shuttingDown = false; async function shutdown(signal) { if (shuttingDown) return; shuttingDown = true; console.log(`${signal} received, draining`); const force = setTimeout(() => { console.error('drain timed out, exiting with active jobs'); process.exit(1); }, DRAIN_TIMEOUT_MS); force.unref(); try { await worker.close(); // pauses fetching, waits for active jobs await redis.quit(); process.exit(0); } catch (err) { console.error('shutdown failed', err); process.exit(1); } } process.on('SIGTERM', () => shutdown('SIGTERM')); process.on('SIGINT', () => shutdown('SIGINT'));

I also made the send path idempotent, since a graceful shutdown narrows the window but does not remove it. A pod can still be OOMKilled or lose its node. Each invoice email now writes an invoice_id row into a sent_emails table with a unique constraint before calling the provider, and skips the send if the insert conflicts.

To verify, I deployed to staging with a script that enqueued 200 slow jobs, then triggered a rollout mid-run. Logs showed SIGTERM received, draining on every old pod, exit code 0 in about 6 seconds, and no job processed twice.


the aftermath

  • Impact: 22 duplicate invoice emails to 19 customers over two minutes. Nobody was billed twice, but we got 3 support tickets, one from a customer asking exactly that.
  • Root cause: CMD ["npm", "start"] made npm PID 1. SIGTERM never reached Node, so the graceful shutdown code had been dead in production since the day it was written.
  • Added a CI check that fails the build if a Dockerfile's final CMD or ENTRYPOINT starts with npm, yarn, or sh -c.
  • Added a startup log line that prints process.pid, and an alert on any worker pod that exits with 137 during a rollout. A 137 on deploy now means the signal path is broken.
  • Runbook line: if a handler never logs, run ps inside the container before reading the handler again.

The handler was fine the whole time. We spent forty minutes reading it before anyone thought to check whether it was being called at all.

Share this
← All Posts8 min read