Lesson 2 of 4
Tell a Wait Apart from Load
Estimated time: 10 minutes
A high priority task can wait on a low priority one
Some waits have nothing to do with load. A high-priority task can block on a shared resource held by a low-priority task that has been scheduled out, leaving the important work idle while the unimportant work simply is not running. It reads like a slow backend or a busy pool, which is why it is missed.
Keep critical sections short
The shorter the time a lock is held, the smaller the window for inversion. Never hold a lock across a slow call, and give every wait a timeout so a block cannot become permanent.
Raise the holder for as long as it holds
When a high-priority task waits on a low-priority holder, raise the holder to the waiter's priority until it releases. The holder finishes sooner instead of sitting scheduled out.
def acquire(lock, priority, wait_limit):
holder = lock.holder()
if holder and holder.priority < priority:
raise_priority(holder, priority)
return lock.wait(timeout=wait_limit)
Tell the wait apart from load
Record who holds the resource and at what priority. A high-priority wait held by a lower-priority task for longer than usual is the signature of inversion, not load.