Card 05/ 08

ComparisonDifficulty: Advanced1 min

Deadlock, Livelock and Starvation

Three failures that all look like "the work is not getting done" from outside, and need three different fixes. Telling them apart takes about a minute with a thread dump and saves a day.

The three stalls, on what threads are doing and what fixes each
FailureThe threads areProcessor useFixed by
DeadlockBlocked forever, in a cycleIdleA lock ordering
LivelockRunning, reacting to each other, making no progressHighRandomised backoff, or one decider
StarvationRunning, but one of them never gets its turnHighFairness, or priority

Livelock is the one people have not met. Two threads each notice the other is waiting, each politely backs off and retries, and the timing means they collide again — like two people stepping aside in a corridor, repeatedly.

Starvation is one thread losing every race. A lock with no fairness hands itself to whoever asks at the right moment, and a thread that always asks at the wrong moment can wait indefinitely while its neighbours are served.

The decision rule for diagnosing one. Look at the processor first. Idle threads that are not moving is a deadlock. Busy threads that are not finishing is a livelock. Busy threads where most finish and one does not is starvation.