Lock-Order Deadlock Detector
Prerequisites: Mutexes, Lock Scope, and Contention, Threads, Shared State, and Data Races
Mutexes, Lock Scope, and Contention ended with two threads that lock AAPL and MSFT in opposite orders. A test run usually passes: the operations rarely overlap, so each thread takes both locks and finishes before the other starts.
The deadlock is still in the code, waiting for the run where each thread gets its first lock before the other finishes. Build a tool that finds it in a run that finished.
- Actual deadlock: a state of a running program. Two or more threads are blocked in
lock()around a closed loop, each waiting for a mutex the next one holds, and none can move again. Looking for blocked threads finds it only after the program hangs. - Potential deadlock: a property of the code. Threads take mutexes in orders that some interleaving could turn into an actual deadlock, even though the recorded run finished. A lock-order check finds it in a run that never hung.
- Lock-order graph: one node per mutex. The check tracks the mutexes each thread holds. When a thread acquires
mwhile holdingh, the program tookhbeforem, and the check records the edgeh -> m.
If every thread followed one global lock order, every edge would point forward in it and the graph would have no cycle.
A cycle AAPL -> MSFT -> AAPL means some thread took AAPL then MSFT and some thread took MSFT then AAPL: the article's circular wait.
The graph is conservative. This problem counts two borderline cases as cycles:
- A cycle whose edges were all recorded by one thread still counts, because the same code running on two threads at once could deadlock.
- A cycle can be harmless. If both threads hold a common outer mutex the whole time, their inner sections never run at the same moment, so they cannot deadlock. The graph does not see that outer mutex's effect and reports the cycle anyway.
What to implement
process_commands(commands)returns the lines the queries print, in order.- Each command is one line, either a trace line or a query. On stdin, the first line is the number of commands.
- There are three parts, each adding queries on top of the previous ones. One solution must pass all ten tests.
| Command | Meaning | Prints |
|---|---|---|
thread ACQUIRE mutex | thread locks mutex | nothing |
thread RELEASE mutex | thread unlocks mutex | nothing |
CHECK | Report the cycle chosen below | CYCLE ... or NO_CYCLE |
EDGES | List the graph (Part 2) | one line per edge, or NO_EDGES |
EXPLAIN | Report the cycle and who built it (Part 2) | the CHECK line, then one line per cycle edge |
ORDER | Recommend a global lock order (Part 3) | ORDER m1 m2 ... or NO_ORDER |
The trace
- Trace lines are in the order the operations happened during one run. Lines of different threads may be interleaved.
- The trace is consistent: a thread never acquires a mutex it already holds or releases one it does not hold, and no mutex is held by two threads at once.
- A thread may release its mutexes in any order, not only the reverse of the order it took them (a
std::unique_lockcan unlock before its scope ends, for example). - The trace may end with mutexes still held.
- Each query sees the graph built from every trace line before it.
Comparing names
Wherever the output is sorted or one name is called smaller than another, names are compared as strings, character by character by ASCII code.
- That is the default
<forstd::stringand for Python'sstr. - So
B10sorts beforeB9, and every uppercase letter sorts before every lowercase one.
Part 1: Find the cycle
Building the graph:
- An
ACQUIREadds the edgeh -> mfor every mutexhthe thread holds at that moment, not only the one it took most recently. A thread that holds nothing adds no edge. - The graph only grows. A
RELEASEremoves the mutex from the thread's held set but never removes an edge, because the order was observed and stays observed. - Recording an edge that already exists changes nothing.
Choosing the cycle:
- A cycle is a closed path
m1 -> m2 -> ... -> mk -> m1that visitskdifferent mutexes. Its length isk, its number of edges. Edges always join two different mutexes, so the shortest possible cycle has length 2. - Each cycle is written starting from its smallest name.
CHECKreports the shortest cycle. When several have that length, it reports the one whose listm1 m2 ... mkis lexicographically smallest: compare the first names, then the second names if those are equal, and so on.
Output of CHECK:
CYCLE m1 -> m2 -> ... -> mk -> m1
- The first name repeats at the end.
- If the graph has no cycle,
CHECKprintsNO_CYCLE.
Limits
- Thread names and mutex names are 1 to 8 letters and digits.
- At most 1,000 lines, at most 100 of them queries, and at most 50 distinct mutex names.
Tests 1–4 cover this part. They use only trace lines and CHECK.
Part 2: Name the threads
Each edge also keeps the set of threads that recorded it. A thread that records the same edge twice appears once.
Output of EDGES: one line per edge.
h -> m by t1,t2,...
- The threads are sorted and separated by commas without spaces.
- The lines are sorted by
h, then bym. - A graph with no edges prints
NO_EDGES.
Output of EXPLAIN:
- First, the same line as
CHECK. - If there is a cycle, then one line per edge of that cycle, in the
EDGESformat and in cycle order, fromm1 -> m2throughmk -> m1.
Those lines name the code paths someone has to change to remove the cycle.
Tests 5–7 cover this part.
Part 3: Recommend a lock order
- Topological order: a list of the mutexes in which every edge
h -> mhashbeforem. When the graph has no cycle, such a list exists, and every recorded acquisition already follows it as a global lock order.
- Several lists can qualify.
ORDERbuilds its list one position at a time and always takes the smallest name among the mutexes not yet placed whose incoming edges all come from mutexes already placed. - The list includes every mutex that has appeared in an
ACQUIREso far, including mutexes that have no edges. - When the graph has a cycle, no such list exists and
ORDERprintsNO_ORDER. - Every
ORDERcomes after at least oneACQUIRE.
Output:
ORDER m1 m2 ... mn
Tests 8–10 cover this part.
13 CHECK feed ACQUIRE AAPL feed ACQUIRE MSFT feed RELEASE MSFT feed RELEASE AAPL pnl ACQUIRE AAPL pnl RELEASE AAPL CHECK risk ACQUIRE MSFT risk ACQUIRE AAPL risk RELEASE AAPL risk RELEASE MSFT CHECK
NO_CYCLE NO_CYCLE CYCLE AAPL -> MSFT -> AAPL
The first CHECK sees an empty graph. Thread feed takes AAPL and then MSFT inside it, which records AAPL -> MSFT.
Thread pnl takes AAPL alone, which records nothing, so the second CHECK still finds no cycle. Thread risk then takes MSFT and then AAPL, which records MSFT -> AAPL and closes the cycle. It is printed from AAPL, the smaller name.
The run itself never deadlocked, because each thread released its locks before the next thread started. The cycle says that if feed and risk overlapped, each could hold one lock and wait forever for the other.