Deadlock
Дедлок
A deadlock occurs when threads or tasks are stuck waiting on one another. For example, each of two threads might hold a lock that the other needs before it can continue.
Also known as: взаимная блокировка
What gets stuck?
Waiting for a lock is normal if the thread holding it can finish its work and release it. In a deadlock, the holder is also waiting, and following the chain of dependencies brings you back to where you started. None of the participants can move forward on its own. The rest of the program may keep running, so a deadlock can look like one request that never returns rather than a total outage.
A two-lock example
Suppose two threads need exclusive access to resources A and B before they can do their work:
Thread 1: lock(A); lock(B); work(); unlock(B); unlock(A)
Thread 2: lock(B); lock(A); work(); unlock(A); unlock(B)
If thread 1 gets A and thread 2 gets B before either tries to acquire its second lock, both stop at their second lock call. Neither reaches its unlock calls. If one thread gets both locks first, though, it can finish normally. That's why the same code can pass repeated tests and still deadlock in production. Make both paths acquire A before B and the cycle is no longer possible between these locks.
Why a consistent lock order works
Four conditions are necessary in the classic resource model. They're known as the Coffman conditions:
- Mutual exclusion: only one participant can use a resource at a time.
- Hold and wait: a participant holds one resource while requesting another.
- No preemption: the system cannot simply take the resource away from its holder.
- Circular wait: each participant in a chain waits for a resource held by the next, and the last waits for the first.
A global lock order rules out circular wait: every nested acquisition moves forward in that order, so the chain cannot wrap back to its starting point. This only works if every relevant code path follows the rule, including helper functions that acquire locks internally.
Diagnosing and recovering from deadlocks
For a multithreaded application, start with a thread dump that shows lock ownership and blocked threads. Follow the dependencies from waiter to owner and look for a cycle. A long wait isn't enough to diagnose deadlock: the lock holder could be doing slow work and still be able to finish.
Databases can deadlock too. Two transactions might update the same rows in opposite orders, with each holding a row lock the other needs. PostgreSQL can detect the deadlock and abort one transaction to break the cycle. If the operation is safe to retry, rerun the entire transaction rather than just the statement that reported the error.
What interviewers look for
When shown code with nested locks, give a specific execution order that leaves each participant holding one lock and waiting for another. Then explain why your fix prevents that cycle. If you suggest timed lock acquisition, describe how the failure path releases any locks it already holds; a timeout by itself doesn't fix inconsistent lock ordering.