Basic

Closing a server cleanly

A server that is stopped in the middle of a request loses the request. Closing cleanly is a signal that means finish what is started, and the meaning is a contract with the process manager.

What you will see

One line with exit zero. Zero is what tells the manager this was a clean stop.

About 1 min of reading

Try It

Prove the drain finishes

1

Write the drain limit

printf 'drain_ms 5000\n' > /tmp/d.txt; cat /tmp/d.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. The drain is a limit, and a limit is a number.
2

Attempt a request longer than the limit

printf 'request_ms 9000\n' | awk '{print ($1>5000) ? "cut" : "finished"}'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
cut. A request longer than the drain is cut, and the cut is a lost request.
3

Write the exit code

printf 'stop exit=0 drain=5000 date=2026-10-10\n' >> /tmp/stops.txt; tail -1 /tmp/stops.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line with exit zero. Zero is what tells the manager this was a clean stop.

Data Flow

A signal to a clean exit
1 The signal is received
↓
2 New requests are refused
↓
3 Requests in flight are finished
↓
4 The process exits zero inside the limit
Why this works
Why the limit is shorter than the restart

The manager waits a fixed time before the next step. The drain must finish inside that time.

The observable proof

A request in flight completes after the signal is sent. The completion is the proof.

Examples

The signal is a request

Stop is not kill. The process is asked to stop, and the process decides when it is safe.

The drain has a limit

Waiting forever for a request that never finishes is a stop that never happens. The limit is a number.

Why the exit code matters

A clean stop exits zero. A non-zero exit is read as a failure by whatever restarts the process.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Headers are a small contract
Previous Lesson
Headers are a small contract
Next Lesson
Connections are a pool
Connections are a pool →