To put it simply, a deadlock is what happens when two programs each hold something the other one needs, and both sit there waiting forever.
When a program freezes, most people assume it crashed. A deadlock is sneakier than a crash. Nothing has broken. Nothing has thrown an error. Every piece of the program is still alive and well — it is just waiting. Waiting for something that will never arrive, because the thing it waits for is held by the other waiter. Yesterday I wrote about race conditions, where two threads rush ahead and step on each other's toes. A deadlock is the opposite failure. Instead of rushing, everything stops. The program looks fine from the outside. The logs just go quiet.
The short version: a deadlock is two people in a narrow doorway, each waiting for the other to step aside first. Nobody moves. Nobody is wrong. That is the whole problem.
It takes four ingredients, and all four have to be there
Back in 1965, a computer scientist named Edsger Dijkstra described a puzzle that makes the whole thing click: five philosophers sit around a table, each with a plate of spaghetti and a fork on either side. To eat, a philosopher needs two forks. Every philosopher picks up the fork on the left, and then waits for the fork on the right — which is in someone else's hand. Everyone holds one fork, everyone waits for a second, and nobody ever eats. That is a deadlock, dressed up as a dinner party.
Researchers later boiled it down to four conditions, and a deadlock needs all four at once. First, exclusive use: only one thread can hold a resource at a time, like a file or a lock. Second, hold and wait: a thread holds one resource while asking for another. Third, no taking things away: nobody can snatch a resource from someone holding it — the holder has to give it up willingly. Fourth, a circle: thread A waits on thread B, thread B waits on thread A, and the waiting goes round in a loop.
Break any one of those four and the deadlock cannot happen. That is the whole trick to preventing them, and I will come back to it. But first, look at how small the code is that causes one.
Here is the smallest version that works:
import threading
lock_a = threading.Lock()
lock_b = threading.Lock()
def worker_one():
with lock_a:
with lock_b: # waits here — worker_two holds lock_b
print("worker one done")
def worker_two():
with lock_b:
with lock_a: # waits here — worker_one holds lock_a
print("worker two done")
threading.Thread(target=worker_one).start()
threading.Thread(target=worker_two).start()
Each worker grabs one lock, then asks for the second. If they run at just the wrong moment, worker one holds lock A and waits for lock B, while worker two holds lock B and waits for lock A. The program prints nothing. It does not crash. It just sits there, politely, forever. Two lines of code each. That is all it takes.
Why it is so hard to spot
The cruel part is that this code works most of the time. Run it a hundred times and it might finish a hundred times, because the two workers usually do not line up badly. The deadlock only shows up when the timing is exactly wrong — the same kind of timing luck that makes race conditions so hard to reproduce. A bug you cannot reproduce is a bug you cannot be sure you fixed, and that is what makes deadlocks dreaded in a way a plain crash never is.
And there is no alarm. A crash leaves a stack trace, an error message, something to search for. A deadlock leaves silence. The program is still running, still using memory, still answering "I'm fine" to anyone who asks — it is just not doing any work. I once spent an afternoon staring at a program that had stopped halfway through a job, convinced the network was down, before I realised the two halves of my own code were waiting on each other. The network was fine. The code was the traffic jam.
Databases run into this constantly, so they are built to handle it. Two transactions can each lock one row and then ask for the other's row, and the database cannot just wait forever — so it picks one transaction, kills it, and tells it to try again. Annoying when it happens to you. Also the reason the database as a whole keeps moving.
| Deadlock | Race condition | |
|---|---|---|
| What happens | Everything stops | The result is wrong |
| The sign | Silence, no error | Wrong answer, sometimes |
| How often | Only when timing lines up badly | Only when timing lines up badly |
| The fix | Break the waiting circle | Make the shared step atomic |
The verdict: both bugs hide behind the same timing luck, but they fail in opposite directions — one freezes, the other lies.
How real systems avoid it
The simplest fix is also the oldest: always take locks in the same order. If every worker in the program grabs lock A before lock B, the circle can never form. Worker two in my example above breaks this rule by taking B first, and that one choice is the entire bug. It sounds too easy to matter, and it is exactly the kind of rule a team agrees on, writes down, and then forgets at 2 a.m. — which is when the deadlock shows up.
The second fix is a timeout. Instead of waiting forever, a thread waits a few seconds and then gives up, releases what it holds, and tries again later. It does not prevent the deadlock, but it turns a permanent freeze into a hiccup. Databases use a version of this: a transaction that waits too long gets killed and retried.
The third fix is to hold locks for as little time as possible. Do the slow work — the network call, the file read, the heavy calculation — before you take the lock, or after you release it. A lock should guard a quick, small step, not a whole afternoon of work. The shorter the hold, the smaller the window where two threads can cross each other badly.
And sometimes the honest fix is to stop sharing so much. If two threads do not need the same data, give each its own copy. If a job can be split so each worker owns its own piece, there is nothing to fight over. The deadlock disappears not because it was solved, but because the sharing that caused it is gone.
The quiet lesson
Deadlocks taught me something I did not expect from a bug: waiting is not the same as doing nothing. Both threads in my little example are working exactly as written. Each one followed its instructions perfectly. The failure is not in any single line of code — it is in how the lines fit together. That is why deadlocks feel so unfair when you meet your first one. You read the code, every line looks right, and the program still sits there doing nothing.
The next time a program of yours goes silent — no error, no crash, just a log that stops mid-sentence — check the locks before you blame the network. If two pieces of your code are each holding what the other needs, you have found your deadlock. Let one of them go first, and the whole thing starts moving again.
- A deadlock needs all four conditions at once — exclusive use, hold and wait, no taking things away, and a circular wait. Break any one and it cannot happen.
- The simplest prevention is lock ordering: every thread takes the same locks in the same order, every time.
- A frozen program with no error is the signature — check for locks waiting on each other before blaming anything else.
References and further reading
- Deadlock — Wikipedia
- Dining philosophers problem — Wikipedia
- Deadlock — Oracle Java Tutorials
- Livelock — Wikipedia
— Min SiThu Software Engineer & Tech Content Creator https://minsithu.org
About the author
- Nubbad Developer. | Now I accidentally create problems.
Comments
Join the discussionNo comments yet. Be the first to comment.

