LIVELOCK & PRIORITY INVERSION

Everyone is busy, nothing gets done

L215 MINRACE CONDITIONS
PREDICT

L (priority 1) holds lock R. H (priority 3) calls lock(R) and blocks. Then M (priority 2) wakes up. The scheduler always runs the highest-priority runnable thread. Who runs next?

Two shapes of “busy but stuck”. Livelock: two threads each hold one lock, fail to get the other, politely release, sleep the same amount, and retry at the same instant, forever. Nothing completes. With spin-retry the CPU is pegged; with sleep-retry, as in the sim, the time is wasted. Priority inversion: a low-priority thread holds a lock a high-priority thread needs. The high-priority thread blocks, so it is not runnable. A medium-priority thread wakes up and, being the highest-priority runnable thread, takes the CPU from the low one. The high-priority thread now waits for the medium one, which is not even involved. The scheduler did exactly what it was told.

▶ SIMULATION
THREAD A
holds: —
state: idle
failed attempts: 0
LOCKS
L1 held by —
L2 held by —
t = 0 · rounds: 0 · completed: 0
fixed backoff: sleep(1)
THREAD B
holds: —
state: idle
failed attempts: 0
✓ livelock: someone completes within 6 rounds✓ inversion: H finishes by its deadline (t=7)✓ the running thread is the highest-priority runnable thread
SCHEDULE
01[A]A: lock(L1) ✓
02[B]B: lock(L2) ✓
03[A]A: trylock(L2) → held by B, fail
04[B]B: trylock(L1) → held by A, fail
05[A]A: release L1, sleep, retry
06[B]B: release L2, sleep, retry
07[A+B]both wake: grab, collide, back off (one round)
08[A+B]both wake: grab, collide, back off (one round)
09[A+B]both wake: grab, collide, back off (one round)
10[A+B]both wake: grab, collide, back off (one round)
11[A+B]both wake: grab, collide, back off (one round)
12[A+B]both wake: grab, collide, back off (one round)
Start of livelock scenario. 12 steps.
// Livelock: polite, and stuck.           // Inversion: L holds R, H needs it, M hogs the CPU.
while (true) {                              low()  { lock(R); work(); unlock(R); }
  lock(mine);                               high() { lock(R); work(); unlock(R); }   // waits for low()
  if (!trylock(theirs)) {                   mid()  { work(); work(); work(); }      // runs instead of low()
    unlock(mine); sleep(1); continue;       // scheduler: highest-priority RUNNABLE thread runs.
  }                                         // H is blocked, so M beats L, so H waits for M.
  work(); unlock(theirs); unlock(mine); break;
}

In LIVELOCK watch the round counter: both threads collide, back off by the same amount, and collide again until the progress invariant goes red. In PRIORITY INVERSION watch M run while H is blocked, then H’s deadline pass, while the scheduler invariant stays green. The scheduler is not the bug.

BREAK IT NOT YET

The scheduler is strict: a step is refused if a higher-priority thread is runnable. H has a deadline of t=7. Order the steps so H misses it because M ran while H was blocked behind L.

GOAL: H finishes after t=7, and M ran at least one tick while H was blocked

FIX IT NOT YET

Implement priority inheritance in acquire. When the lock is held by a lower-priority thread, boost the holder to the caller's priority before blocking. The grader runs L / M / H on a strict scheduler and checks that H finishes on time, including a run where L blocks on a lock H holds.

Mental model:

progress needs asymmetry. Two threads that react identically to the same conflict will keep producing the same conflict; break the symmetry with randomness or an ordering rule. And a priority only means something if it propagates through the locks: whoever holds what the important thread needs is, for that moment, the important thread.

IN PRODUCTION

Retry loops with fixed backoff between two services that each hold a resource the other wants. Real-time and embedded systems where an interrupt handler or a high-priority task shares a mutex with a logging or housekeeping task. Also Linux PI futexes, RTOS mutexes with a priority-inheritance flag, and database lock queues where a long-running low-priority job blocks an interactive one.

IN INTERVIEWS

“What is the difference between deadlock and livelock?” and “What is priority inversion and how do you fix it?” Describe the three-thread shape (low holds, high waits, medium runs), explain that the scheduler is behaving correctly, and name priority inheritance or a priority ceiling as the fix. Mention Pathfinder if you want the follow-up to be about VxWorks.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: CAPSTONE: CONNECTION POOL →