Card 01/ 08

ConceptDifficulty: Intermediate1 min

Deadlock: Two Threads Each Waiting for the Other's Lock

A service stops with idle processors and no error, because of a cycle of waiting.

A deadlock is a set of threads that can never continue, because each one holds a lock that another is waiting for. With two threads and two locks, each holds one and waits for the other, and neither can let go of what it has until it gets what it wants.

A service stops responding. Nothing has crashed, the processors are idle, memory is fine, and the request threads have not moved in eleven minutes. The request threads are all waiting in one closed cycle:

Each thread holds one lock and waits for the other, closing a cycle

A deadlock has two properties worth knowing before anything else. It is permanent: nothing times out, nothing retries, and no thread gives up. And it is silent: no exception is thrown and nothing is logged.

So a deadlock is not a rare mystery. It is a cycle in a graph of who waits for whom, and the way to prevent one is to make sure the cycle can never close in the first place.