A server runs three clustered Node workers behind a process manager. A deploy reloads the workers one at a time. The code has a graceful shutdown: on the stop signal, a worker stops taking requests, waits for what is in flight, then exits. The wait may last up to 90 seconds, and the process manager allows 100 before it kills.
That looks solid, and it still cut jobs on deploys. Graceful shutdown waits for what you registered, not for what is running.
Key facts
- In one 24-hour window, 481 background jobs ran and 7 never finished.
- All 7 sat at a process death: 3 at one out-of-memory kill, 4 at three deploy reloads.
- The drain counted only HTTP requests. On each reload it logged “0 job(s) in flight” while jobs ran inside the worker.
- One client saw a frozen list for 3 minutes. The other client’s row still said “processing” a day later.
- The fix has three parts: jobs register with the drain, a fast retryable end, and a sweep every 30 seconds.
What happened
The work that mattered was not a request. A client app uploads photos with a POST. The server answers at once with a job id, then keeps working for 20 seconds to 2 minutes. The client polls for rows as they arrive. The job runs fire-and-forget, after the response has gone out.
The drain kept a registry of things in flight. Only HTTP requests were put in it, plus one unrelated long job that someone had registered by hand. The photo jobs were never in it.
So at each reload the old worker logged no longer accepting requests, 0 job(s) in flight, then drained after 1 s, exiting. It said zero and exited, with jobs still running inside it.
Seven of 481, and every one at a restart
Seven failures in 481 jobs looks like noise. It stops looking like noise when you put each failure next to the moments a process died.
| Cause | Events | Jobs lost |
|---|---|---|
| Out-of-memory kill | 1 | 3 |
| Deploy reloads | 3 | 4 (1, 2 and 1) |
| Total | 4 process deaths | 7 |
The out-of-memory kill has its own story. The reloads are the part a graceful shutdown was meant to cover.
Nine reloads happened in the window. Six of them hit no running job, so nothing was lost. That is why it looked rare and random: most deploys landed in a quiet moment.
What the two clients saw
One reload cut jobs for two clients, and they saw two different things.
Client A got the rows of the first photo. Then it got the same 117 KB answer about 66 times over 3 minutes: a frozen list. Then the server answered with an error. The client set the job aside, re-sent it by itself and got the full result. The user watched a frozen list for 3 minutes and still got there.
Client B never polled that job again. So nothing ever told it, and nothing told the server either. The row still said “processing” a day later.
Why a stuck row lasted a day
A dead job was only noticed lazily. When a poll reached a worker that did not hold the job, and the row had no write for 3 minutes, the row was marked failed. The logs show it: no update for 182 s, no update for 213 s.
That check only ran inside a poll. No poll, no verdict. No timer looked at old rows, so a row whose client had gone away stayed “processing” for good.
The fix
- Every background job registers with the drain. It registers when it starts and releases in a
finally. A reload now keeps the old worker alive until its jobs end. - A fast, retryable end. A job still running shortly before the drain gives up is ended at once with a retryable error. The client re-sends right away instead of showing frozen rows for 3 minutes.
- A sweep every 30 seconds. It ends rows that are still “running” with no write for 3 minutes, whether or not anyone polls.
A sketch, with made-up names:
// Work that runs after a response.
const inFlight = new Set();
function begin(job) {
inFlight.add(job);
}
function end(job) {
inFlight.delete(job);
}
async function runJob(job) {
begin(job);
try {
await job.work();
} finally {
end(job); // even if it throws
}
}
async function shutdown(limitMs) {
stopAccepting();
const until = Date.now() + limitMs;
while (inFlight.size > 0 &&
Date.now() < until) {
await sleep(250);
}
// Out of time: end the rest
// so clients retry now.
for (const job of inFlight) {
job.markInterrupted({
retryable: true,
});
}
process.exit(0);
}
// Close rows nobody polls.
setInterval(
() => failStaleRows(3 * 60_000),
30_000,
);
After the fix, so far
This is the first hours only. The sweep ended the 5 rows that had been stuck for about a day, and one fresh row at 180 seconds. No row has been stuck since.
The deploy that shipped the fix still ran under the old drain. It cut 2 jobs one last time, and both clients retried them.
What has not been seen yet is the drain actually waiting for a running job. That needs a later deploy with a job in flight. Until then, part one of the fix is written and shipped, but not yet proven.
Check your own
- Find work that runs after the response. Search your code for fire-and-forget promises, queues in memory and timers. Each one must tell the shutdown it exists.
- Read your own shutdown log line on a deploy. “0 in flight, exited after 1 s” on a busy server is the tell.
- Give a job that cannot finish a fast, retryable end. A client that retries at once beats a client that waits for a timeout.
- Do not let “dead” be found only when someone asks. A small periodic sweep closes the rows nobody polls.
- Check both limits against your longest job. The drain has its own limit and the process manager has a kill timeout. Here they were 90 s and 100 s, against jobs of up to 2 minutes. The fast retryable end covers the gap.
- Count failures against process deaths, not the hour of day. Seven in 481 looks like noise until each one lines up with a restart. Deploys are a cause of their own, like another thing that only breaks on deploy days.
Common questions
Why not just raise the drain timeout?
The process manager still kills at 100 seconds, and a job can run for up to 2 minutes. Raising one limit means raising both, and some job will still be longer. The fast retryable end covers the gap.
Is a queue in memory the same problem?
Yes. Any work started after the response, a queue in memory, a timer or a promise nobody awaits, is invisible to a counter of open requests.
Why did it look random?
Six of nine reloads hit no running job. Failures only show up when a deploy lands while a job is running, so they follow the deploys, not the clock.
Melta is free; get it on the App Store or Google Play.