Deadlock
Two tasks, two mutexes, taken in opposite order. Why neither kernel notices, and how to design it away.
Simulated kernel: FreeRTOS V11.3.3 and Zephyr v4.5.0. This scenario and every other one are in the scheduler playground. The simulator and its data files are not covered by the site's CC BY-SA license; see their license notices.
A deadlock is a set of tasks in which each one waits for something that another one in the set holds. None of them can ever run again. The rest of the system carries on, which makes a deadlock look like a feature that silently stopped working.
The program
Two tasks and two mutexes, A and B.
LowlocksB, works for two ticks, then locksA.Highstarts one tick later, locksA, works for one tick, then locksB.
Each task takes both mutexes, in the opposite order. This is known as the ABBA pattern.
What happens (FreeRTOS)
- Tick 0:
LowlocksB. - Tick 1:
Highwakes, preemptsLowand locksA. - Tick 2:
Highasks forB.Lowholds it, soHighblocks.Lowinherits the priority ofHigh, which does not help. Lowruns its second tick of work and, at tick 3, asks forA.Highholds it, soLowblocks.
From tick 3 the timeline shows both tasks blocked and the idle task running, until the simulation
stops. Under the mutexes, the simulator draws who waits for whom: Low waits for A, held by
High, which waits for B, held by Low. A closed chain is a deadlock.
The kernel does not notice
The simulator reports the deadlock. The kernels do not. To either kernel these are two tasks that are legitimately blocked; all its lists are consistent. A watchdog that is fed by the idle task, or by any task not in the cycle, keeps being fed.
Zephyr can check for this. With CONFIG_MUTEX_DEADLOCK_DETECT, k_mutex_lock() follows the chain
of owners (the same walk it does for priority inheritance)
and fails an assertion if the chain leads back to the caller and every wait in it is without a
timeout. The option is only on by default when assertions are enabled, so a debug build stops at the
deadlock and a release build does not. The simulator models the release configuration.
Priority inheritance is no cure: it shortens waits that will end. Here Low is raised to the
priority of High and still has nothing it can do.
Why it is hard to find in testing
The deadlock needs High to arrive in the window where Low holds B but not yet A. In the
simulator, set the start of High to tick 4. Low is finished with both mutexes by then, and the
program runs to its end. The same code, with different timing, works. On real hardware that window
may be a few microseconds wide and be hit once a month.
How to design it away
- Lock ordering. Give every mutex a rank and always take them in rank order. If both tasks took
AbeforeB, the second task would block onAand wait; a closed chain cannot form. This is the standard answer, and it works as long as everybody follows it. - One lock. If two resources are always used together, protect them with one mutex.
- A timeout. Take the second mutex with a timeout. If it runs out, release the first mutex and try again later. The mutex timeout lesson shows what each kernel does with the inherited priority when a wait times out.
- No lock. Give the resource to one task and let the others send it messages.
What to try
- Move the start of
Highto 4 and watch the program complete; then move it back and step by Event to the moment the chain closes. - Break on Deadlock and press Run to breakpoint.