Build an HTTP Server with Graceful Shutdown
An HTTP service that drains in-flight requests on shutdown, stops accepting new connections, runs cleanup hooks, and exits within a bounded timeout, documented with tests.
Project goal
Build an HTTP server that on SIGTERM stops accepting connections, waits for in-flight requests up to a deadline, runs registered cleanup hooks, and exits cleanly without dropping live requests.
Final features
- Stop accepting new connections the moment shutdown begins.
- Let in-flight requests finish within a configurable grace period.
- Run registered cleanup hooks such as closing database handles.
- Force exit after the deadline so a stuck request cannot hang the process.
- Expose a readiness endpoint that flips to unhealthy during drain.
Architecture
A request tracker counts active requests; a lifecycle module owns the shutdown state machine from running to draining to closed; a shutdown registry holds cleanup hooks; and the server wires these to process signals. The state machine is testable without real signals by calling its transitions directly.
Prerequisites
- Node.js 20 LTS or later
- Understanding of process signals and the event loop
- Familiarity with HTTP servers and request lifetimes
Dependencies
- node >=20 — node:http, process signals, and timers
- vitest ^2.1.0 — State machine and end-to-end drain tests
Directory structure
graceful-server/
├── src/
│ ├── tracker.js
│ ├── lifecycle.js
│ ├── hooks.js
│ ├── server.js
│ └── cli.js
├── test/
│ ├── lifecycle.test.js
│ └── drain.test.js
└── package.json
Phases
1. Request tracking
Count active requests and expose a hook to wrap handlers so the count is accurate under concurrency.
Files: src/tracker.js
export function createTracker() {
let active = 0;
return {
active: () => active,
track(handler) {
return async (...args) => {
active += 1;
try { return await handler(...args); }
finally { active -= 1; }
};
},
};
}Expected behavior: The active count returns to zero after every tracked request, including when a handler throws.
2. Lifecycle state machine
Model running, draining, closing, and closed states with an explicit drain and close transition.
Files: src/lifecycle.js
export function waitFor(predicate, timeoutMs, step = 25) {
return new Promise((resolve) => {
const started = Date.now();
const tick = () => {
if (predicate()) return resolve(true);
if (Date.now() - started >= timeoutMs) return resolve(false);
setTimeout(tick, step);
};
tick();
});
}
export function createLifecycle({ deadlineMs = 10000 } = {}) {
let state = 'running';
return {
get state() { return state; },
beginDrain() { if (state === 'running') state = 'draining'; },
async close(active) {
state = 'closing';
const drained = await waitFor(() => active() === 0, deadlineMs);
state = 'closed';
return { drained };
},
};
}Expected behavior: Close resolves with drained true when the active count reaches zero, and false once the deadline passes.
3. Cleanup hooks
Register cleanup hooks and run them in order during close, ensuring they run even if the drain times out.
Files: src/hooks.js
export function createHookRegistry() {
const hooks = [];
return {
register(name, fn) { hooks.push({ name, fn }); },
async runAll() {
const ran = [];
for (const hook of hooks) {
await hook.fn();
ran.push(hook.name);
}
return ran;
},
};
}Expected behavior: Hooks run in registration order, each awaited, and a hook that throws stops the sequence with that error surfaced.
4. Server wiring
Build the HTTP server with tracking, a readiness endpoint, and signal handlers that begin drain before closing.
Files: src/server.js
import { createServer } from 'node:http';
import { createTracker } from './tracker.js';
import { createLifecycle } from './lifecycle.js';
import { createHookRegistry } from './hooks.js';
export function createApp({ deadlineMs = 10000, delayMs = 0 } = {}) {
const tracker = createTracker();
const lifecycle = createLifecycle({ deadlineMs });
const hooks = createHookRegistry();
const handle = tracker.track(async (req, res) => {
if (req.url === '/readyz') {
return send(res, lifecycle.state === 'running' ? 200 : 503, { state: lifecycle.state });
}
if (delayMs) await new Promise((r) => setTimeout(r, delayMs));
send(res, 200, { ok: true });
});
const server = createServer((req, res) => {
if (lifecycle.state !== 'running') return send(res, 503, { error: 'draining' });
return handle(req, res);
});
return { server, lifecycle, hooks, tracker };
}
function send(res, status, body) {
res.writeHead(status, { 'content-type': 'application/json' });
res.end(JSON.stringify(body));
}Expected behavior: While running the readiness endpoint returns 200, after drain begins it returns 503, and a request during drain is refused with 503.
5. Signal handling and exit
On SIGTERM begin drain, stop listening, wait for in-flight work, run hooks, and exit with the right code.
Files: src/cli.js
import { createApp } from './server.js';
const app = createApp({
deadlineMs: Number(process.env.GRACE_MS ?? 10000),
delayMs: Number(process.env.DELAY_MS ?? 0),
});
app.hooks.register('close-db', async () => { /* release handles here */ });
app.server.listen(process.env.PORT ?? 3000, () => console.log('listening'));
async function shutdown(signal) {
console.log('received ' + signal + ', draining');
app.lifecycle.beginDrain();
app.server.close();
const result = await app.lifecycle.close(app.tracker.active);
await app.hooks.runAll();
console.log(result.drained ? 'drained cleanly' : 'deadline reached, forcing exit');
process.exit(result.drained ? 0 : 1);
}
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));Expected behavior: Sending SIGTERM lets an in-flight slow request finish, runs the cleanup hook, and exits zero, or exits one if the deadline is exceeded.
6. Drain tests
Start the server, issue a slow request, trigger drain, and assert the request completed and later requests were refused.
Files: test/drain.test.js
import { createApp } from '../src/server.js';
test('in-flight request completes during drain and new ones are refused', async () => {
const app = createApp({ deadlineMs: 2000, delayMs: 100 });
await new Promise((r) => app.server.listen(0, r));
const port = app.server.address().port;
const inflight = fetch('http://localhost:' + port + '/work').then((r) => r.status);
app.lifecycle.beginDrain();
app.server.close();
const late = await fetch('http://localhost:' + port + '/work').catch(() => 'refused');
expect(await inflight).toBe(200);
expect(late === 'refused' || late === 503).toBe(true);
});Expected behavior: The in-flight request resolves 200 while a request issued after drain begins is either refused or answered 503.
Testing
Run npx vitest. Lifecycle tests call beginDrain and close directly and assert the state sequence and hook execution order. A drain test starts the server, fires a slow request, triggers shutdown, and asserts the request completes while a request initiated after draining starts is refused.
Troubleshooting
The process never exits after close
Call server.close to stop the listener and clear any lingering timers or open handles; the event loop stays alive until every handle is released.
Requests are dropped on deploy
Begin draining before closing the listener and flip readiness to unhealthy first; the load balancer needs the unhealthy signal before it stops routing new traffic.
A stuck request blocks shutdown past the grace period
Bound the wait with a deadline and exit with a failure code if it expires; an unbounded wait turns a deploy into an outage.
Deployment
Set the grace period slightly longer than the orchestrator termination grace so the process exits before the platform sends SIGKILL. Ensure the readiness endpoint is what the load balancer checks, so drain actually removes the instance from rotation.
Known limitations
- Cleanup hooks are best-effort; a hook that hangs still delays exit until the deadline.
- The readiness state is in-process, so a multi-instance deployment relies on the platform polling each instance.
Project overview
The difference between a service that restarts cleanly and one that corrupts work is what happens in the seconds after a termination signal. This project models that window explicitly: stop intake, drain, run hooks, then exit or force.
The server tracks in-flight requests, flips a readiness flag as soon as draining begins, waits for the active count to reach zero before the deadline, and always runs cleanup.
The lifecycle is a small state machine whose transitions are called directly in tests, which is what makes shutdown behaviour deterministic:
export function createLifecycle({ deadlineMs = 10000 } = {}) {
let state = 'running';
const hooks = [];
return {
get state() { return state; },
beginDrain() { if (state === 'running') state = 'draining'; },
registerHook(fn) { hooks.push(fn); },
async close(active) {
state = 'closing';
await waitFor(() => active() === 0, deadlineMs);
for (const hook of hooks) await hook();
state = 'closed';
return { drained: active() === 0 };
},
};
}
Because the transition to draining is separate from the wait, the readiness endpoint can report unhealthy before any request is dropped.
Setup
The service uses only the standard library so the shutdown logic is the only moving part worth reading.
| Dependency | Version | Why it is here |
|---|---|---|
| node | >=20 | node:http, process signals, and timers |
| vitest | ^2.1.0 | State machine and end-to-end drain tests |
Create the project and install:
mkdir graceful-server && cd graceful-server
npm init -y
npm install -D vitest@^2.1.0
Configure the server entry and tests in package.json:
{
"name": "graceful-server",
"type": "module",
"scripts": {
"start": "node src/cli.js",
"test": "vitest run"
}
}
Set the grace period from an environment variable so the test uses a short deadline and production uses a realistic one.
Phase 1: Request tracking
Count active requests and expose a hook to wrap handlers so the count is accurate under concurrency. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.
export function createTracker() {
let active = 0;
return {
active: () => active,
track(handler) {
return async (...args) => {
active += 1;
try { return await handler(...args); }
finally { active -= 1; }
};
},
};
}
Acceptance for this phase: The active count returns to zero after every tracked request, including when a handler throws.
Phase 2: Lifecycle state machine
Model running, draining, closing, and closed states with an explicit drain and close transition. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.
export function waitFor(predicate, timeoutMs, step = 25) {
return new Promise((resolve) => {
const started = Date.now();
const tick = () => {
if (predicate()) return resolve(true);
if (Date.now() - started >= timeoutMs) return resolve(false);
setTimeout(tick, step);
};
tick();
});
}
export function createLifecycle({ deadlineMs = 10000 } = {}) {
let state = 'running';
return {
get state() { return state; },
beginDrain() { if (state === 'running') state = 'draining'; },
async close(active) {
state = 'closing';
const drained = await waitFor(() => active() === 0, deadlineMs);
state = 'closed';
return { drained };
},
};
}
Acceptance for this phase: Close resolves with drained true when the active count reaches zero, and false once the deadline passes.
Phase 3: Cleanup hooks
Register cleanup hooks and run them in order during close, ensuring they run even if the drain times out. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.
export function createHookRegistry() {
const hooks = [];
return {
register(name, fn) { hooks.push({ name, fn }); },
async runAll() {
const ran = [];
for (const hook of hooks) {
await hook.fn();
ran.push(hook.name);
}
return ran;
},
};
}
Acceptance for this phase: Hooks run in registration order, each awaited, and a hook that throws stops the sequence with that error surfaced.
Phase 4: Server wiring
Build the HTTP server with tracking, a readiness endpoint, and signal handlers that begin drain before closing. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.
import { createServer } from 'node:http';
import { createTracker } from './tracker.js';
import { createLifecycle } from './lifecycle.js';
import { createHookRegistry } from './hooks.js';
export function createApp({ deadlineMs = 10000, delayMs = 0 } = {}) {
const tracker = createTracker();
const lifecycle = createLifecycle({ deadlineMs });
const hooks = createHookRegistry();
const handle = tracker.track(async (req, res) => {
if (req.url === '/readyz') {
return send(res, lifecycle.state === 'running' ? 200 : 503, { state: lifecycle.state });
}
if (delayMs) await new Promise((r) => setTimeout(r, delayMs));
send(res, 200, { ok: true });
});
const server = createServer((req, res) => {
if (lifecycle.state !== 'running') return send(res, 503, { error: 'draining' });
return handle(req, res);
});
return { server, lifecycle, hooks, tracker };
}
function send(res, status, body) {
res.writeHead(status, { 'content-type': 'application/json' });
res.end(JSON.stringify(body));
}
Acceptance for this phase: While running the readiness endpoint returns 200, after drain begins it returns 503, and a request during drain is refused with 503.
Phase 5: Signal handling and exit
On SIGTERM begin drain, stop listening, wait for in-flight work, run hooks, and exit with the right code. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.
import { createApp } from './server.js';
const app = createApp({
deadlineMs: Number(process.env.GRACE_MS ?? 10000),
delayMs: Number(process.env.DELAY_MS ?? 0),
});
app.hooks.register('close-db', async () => { /* release handles here */ });
app.server.listen(process.env.PORT ?? 3000, () => console.log('listening'));
async function shutdown(signal) {
console.log('received ' + signal + ', draining');
app.lifecycle.beginDrain();
app.server.close();
const result = await app.lifecycle.close(app.tracker.active);
await app.hooks.runAll();
console.log(result.drained ? 'drained cleanly' : 'deadline reached, forcing exit');
process.exit(result.drained ? 0 : 1);
}
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
Acceptance for this phase: Sending SIGTERM lets an in-flight slow request finish, runs the cleanup hook, and exits zero, or exits one if the deadline is exceeded.
Phase 6: Drain tests
Start the server, issue a slow request, trigger drain, and assert the request completed and later requests were refused. Keeping each phase self-contained is what makes the whole project reviewable one commit at a time.
import { createApp } from '../src/server.js';
test('in-flight request completes during drain and new ones are refused', async () => {
const app = createApp({ deadlineMs: 2000, delayMs: 100 });
await new Promise((r) => app.server.listen(0, r));
const port = app.server.address().port;
const inflight = fetch('http://localhost:' + port + '/work').then((r) => r.status);
app.lifecycle.beginDrain();
app.server.close();
const late = await fetch('http://localhost:' + port + '/work').catch(() => 'refused');
expect(await inflight).toBe(200);
expect(late === 'refused' || late === 503).toBe(true);
});
Acceptance for this phase: The in-flight request resolves 200 while a request issued after drain begins is either refused or answered 503.
Testing strategy
Run npx vitest. Lifecycle tests call beginDrain and close directly and assert the state sequence and hook execution order. A drain test starts the server, fires a slow request, triggers shutdown, and asserts the request completes while a request initiated after draining starts is refused.
The concurrency test is the meaningful one: it proves the server waits for exactly the requests it accepted and turns away the ones it did not, which is the contract that avoids dropped work.
Extending the project
A second signal for immediate shutdown, per-route drain priorities, and a health detail payload are all additions around the same state machine. Because hooks are registered rather than hard-coded, new resources can join cleanup without touching the lifecycle.