Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 m while holding h, the program took h before m, and the check records the edge h -> 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.
CommandMeaningPrints
thread ACQUIRE mutexthread locks mutexnothing
thread RELEASE mutexthread unlocks mutexnothing
CHECKReport the cycle chosen belowCYCLE ... or NO_CYCLE
EDGESList the graph (Part 2)one line per edge, or NO_EDGES
EXPLAINReport the cycle and who built it (Part 2)the CHECK line, then one line per cycle edge
ORDERRecommend 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_lock can 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 < for std::string and for Python's str.
  • So B10 sorts before B9, and every uppercase letter sorts before every lowercase one.

Part 1: Find the cycle

Building the graph:

  1. An ACQUIRE adds the edge h -> m for every mutex h the thread holds at that moment, not only the one it took most recently. A thread that holds nothing adds no edge.
  2. The graph only grows. A RELEASE removes the mutex from the thread's held set but never removes an edge, because the order was observed and stays observed.
  3. Recording an edge that already exists changes nothing.

Choosing the cycle:

  1. A cycle is a closed path m1 -> m2 -> ... -> mk -> m1 that visits k different mutexes. Its length is k, its number of edges. Edges always join two different mutexes, so the shortest possible cycle has length 2.
  2. Each cycle is written starting from its smallest name.
  3. CHECK reports the shortest cycle. When several have that length, it reports the one whose list m1 m2 ... mk is 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, CHECK prints NO_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 by m.
  • 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 EDGES format and in cycle order, from m1 -> m2 through mk -> 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 -> m has h before m. When the graph has no cycle, such a list exists, and every recorded acquisition already follows it as a global lock order.
  1. Several lists can qualify. ORDER builds 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.
  2. The list includes every mutex that has appeared in an ACQUIRE so far, including mutexes that have no edges.
  3. When the graph has a cycle, no such list exists and ORDER prints NO_ORDER.
  4. Every ORDER comes after at least one ACQUIRE.

Output:

ORDER m1 m2 ... mn

Tests 8–10 cover this part.

Example
Input
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
Output
NO_CYCLE
NO_CYCLE
CYCLE AAPL -> MSFT -> AAPL
Explanation

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.