How npm start Swallowed SIGTERM and Made Our Graceful Shutdown Code Dead on Every Deploy
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:
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.
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:
And package.json:
I exec into a running pod and look at the process tree:
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:
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:
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
CMDorENTRYPOINTstarts withnpm,yarn, orsh -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
psinside 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.