10-03

Chapter 10 · Fairness, derived

When WF is not enough

  • 43 min core
  • Essentials track
  • Lab: Scheduler Game
  • Skills: intent, hostile-env

00Recall

One card from 10-02 on the word that lets the scheduler starve a process under weak fairness, one from 10-01 on what WF_vars(Next) leaves the scheduler, and one from 06-04 on mutual exclusion. Today you take away the scheduler's last trick on FairLock, and then ask what that cost.

10-02-b from Weak fairness from a starvation trace

FairLock under weak fairness on every Acquire(p) still lets a process starve. Which word in the definition of WF lets the scheduler do it?

Show answer

"Continuously". The other process keeps taking the lock, so the waiting process's Acquire is disabled once per round, and WF asks nothing of an action that is not continuously enabled.

10-01-b from Be the scheduler

What does WF_vars(Next) take away from a hostile scheduler, and what does it leave?

Show answer

It takes away stopping while some step is possible. It leaves favouritism: the scheduler can keep choosing other processes' steps for ever.

06-04-a from Mutual exclusion

Raising your flag before waiting makes two flags safe. What does it break, and how does Peterson repair it?

Show answer

Both can raise their flags and then wait for each other for ever: a deadlock. Peterson adds a turn variable that each gives away after raising its flag; when both want in, the one who gave the turn away last waits.

Skip recall Recall is never required.

01Commit

Under weak fairness the starving process's Acquire is enabled, then disabled, then enabled again, round after round. Weak fairness asks for an action that is enabled continuously. Change one word: ask for an action that is enabled infinitely often.

TLA+
SFSpec is defined as
    and Spec
    and for all p in Procs : SF_vars(Acquire(p))
    and for all p in Procs : WF_vars(Release(p))

SF stands for strong fairness. SF_vars(A) says: if <<A>>_vars is enabled infinitely often, such steps happen infinitely often.

Commit a

FairLock.sf.live.cfg checks NoStarvation and Progress against SFSpec. What does TLC report?

  1. No violation of either.
  2. A violation of NoStarvation: the waiting process's Acquire is still disabled whenever the other holds the lock.
  3. A violation of NoStarvation, with a longer lasso: the scheduler still wins, it just has to let the waiting process see a free lock now and then.

Answer No violation of either.

Commit b

Compared with FairLock.live.cfg, which has no fairness, how many distinct states does the SFSpec run report?

  1. Fewer: the starving states are gone.
  2. The same number.
  3. More: TLC has to track which actions were enabled.

Answer The same number.

02Lab

Lab · Scheduler GameWhat you’re looking at: FairLock's state graph again. Switch between the weak and strong fairness variants and play the favourites trick under each.

NoStarvation violated · loop

● 1● 23● 4567● 8

This is a teaching view. Real checking happens with TLC on your machine.

Trace

  1. State 1 Initial state lock = FALSE pc = (A :> "idle" @@ B :> "idle")
  2. State 2 Request(A) pc (A :> "idle" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "idle")
  3. State 3 Request(B) pc (A :> "waiting" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "waiting")
  4. State 4 Acquire(B) lock FALSE becomes TRUE pc (A :> "waiting" @@ B :> "waiting") becomes (A :> "waiting" @@ B :> "cs")
  5. Loop stutters forever in state 4
Text view: every state and step
States and their successors
StatepclockSteps (action → state)
initial 1(A :> "idle" @@ B :> "idle")FALSERequest(A) → 2; Request(B) → 3
2(A :> "waiting" @@ B :> "idle")FALSERequest(B) → 4; Acquire(A) → 5
3(A :> "idle" @@ B :> "waiting")FALSERequest(A) → 4; Acquire(B) → 6
4(A :> "waiting" @@ B :> "waiting")FALSEAcquire(A) → 7; Acquire(B) → 8
5(A :> "cs" @@ B :> "idle")TRUERequest(B) → 7; Release(A) → 1
6(A :> "idle" @@ B :> "cs")TRUERequest(A) → 8; Release(B) → 1
7(A :> "cs" @@ B :> "waiting")TRUERelease(A) → 3
8(A :> "waiting" @@ B :> "cs")TRUERelease(B) → 2

Under the strong fairness variant, play the trick from 10-02 and count how many times the waiting process's Acquire is shaded in one round of the loop. Then run:

shell
./tla check chapters/10-fairness/specs/FairLock.sf.live.cfg
Expected output 10-fairness/specs/FairLock.sf.live
10-fairness/specs/FairLock.sf.live  FairLock.sf.live.cfg  (A=A, B=B)
parse ok
model check: no violation found in 8 distinct states (15 generated, depth 4)
expected outcome met: pass
coverage: all 3 actions fired
witness Contended reached (trace of 4 states)

Reveal for commit a

No violation of NoStarvation or Progress, with every action firing and the witness Contended reached. The favourites loop enables the waiting process's Acquire in every state except the one where the other process holds the lock, so infinitely often; strong fairness then demands that it happen infinitely often, and a single acquire already ends the wait.

Reveal for commit b

The same: 8 distinct states with strong fairness, 8 with none. The loop that starved a process is still in the graph. It is no longer a behavior the spec allows.

03Reason

If you expected the old lasso back, you were still reading fairness as "continuously enabled". Put the two definitions side by side:

HypothesisThen
WF_v(A)<>[](ENABLED <<A>>_v): enabled at every moment from some point on[]<><<A>>_v
SF_v(A)[]<>(ENABLED <<A>>_v): enabled infinitely often[]<><<A>>_v

You do not have to take the implication on trust. TLC can check a whole spec, fairness included, as a PROPERTY of another spec:

Nothing about the lock changed. A process that spins on the lock and finds it taken every time it looks is still possible in any real machine you could build from this design. Strong fairness is a claim that the machine will not let that go on for ever. Who makes that claim true is the subject of the last two lessons. Keep the question with you through the philosophers below: for each fairness conjunct you write, who at the table would have to keep it, and how?

The same shape appears at a dinner table. In specs/DiningPhilosophers.tla, each philosopher needs both of its neighbouring forks and picks them up one at a time; philosopher 0 reaches the other way round, which is what keeps the table from deadlocking. Under weak fairness on everything a philosopher does once hungry, DiningPhilosophers.wf.live.cfg violates NoStarvation:

  1. State 1 Initial state state = (0 :> "thinking" @@ 1 :> "thinking" @@ 2 :> "thinking") fork = (0 :> -1 @@ 1 :> -1 @@ 2 :> -1)
  2. State 2 GetHungry state (0 :> "thinking" @@ 1 :> "thinking" @@ 2 :> "thinking") becomes (0 :> "thinking" @@ 1 :> "hungry" @@ 2 :> "thinking")
  3. State 3 GetHungry state (0 :> "thinking" @@ 1 :> "hungry" @@ 2 :> "thinking") becomes (0 :> "thinking" @@ 1 :> "hungry" @@ 2 :> "hungry")
  4. State 4 GetHungry state (0 :> "thinking" @@ 1 :> "hungry" @@ 2 :> "hungry") becomes (0 :> "hungry" @@ 1 :> "hungry" @@ 2 :> "hungry")
  5. State 5 TakeFirst state (0 :> "hungry" @@ 1 :> "hungry" @@ 2 :> "hungry") becomes (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "hungry") fork (0 :> -1 @@ 1 :> -1 @@ 2 :> -1) becomes (0 :> -1 @@ 1 :> 0 @@ 2 :> -1)
  6. State 6 TakeFirst state (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "hungry") becomes (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "holding") fork (0 :> -1 @@ 1 :> 0 @@ 2 :> -1) becomes (0 :> -1 @@ 1 :> 0 @@ 2 :> 2)
  7. State 7 TakeSecond state (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "holding") becomes (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "eating") fork (0 :> -1 @@ 1 :> 0 @@ 2 :> 2) becomes (0 :> 2 @@ 1 :> 0 @@ 2 :> 2)
  8. State 8 PutDown state (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "eating") becomes (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "thinking") fork (0 :> 2 @@ 1 :> 0 @@ 2 :> 2) becomes (0 :> -1 @@ 1 :> 0 @@ 2 :> -1)
  9. Loop GetHungry(2) back to state 5
Write something before openingin that loop, which philosopher starves, and why does weak fairness not save it?

Two starve. Philosopher 0 holds fork 1 throughout the loop and waits for fork 0, which philosopher 2 picks up and puts down every round. Philosopher 0's TakeSecond is enabled in every loop state except the one where philosopher 2 holds fork 0. That is the flicker again: enabled infinitely often, never continuously. Philosopher 1 is "hungry" and needs fork 1, which philosopher 0 never lets go, so its TakeFirst is disabled in every loop state: no fairness on it can help, and it eats only once philosopher 0 does. The Core exercise asks you for the fairness that fixes it, and for no more.

Write something before openingSFSpec keeps weak fairness on Release(p). Why is weak enough there?

A process in its critical section can always release. Nothing but its own Release changes its pc, and nobody else can take the lock while it holds it. So from the moment it enters until it leaves, Release(p) is enabled in every state: continuously, which is exactly the hypothesis of weak fairness. Strong fairness there would not change a verdict, but it would claim more than the system needs. A rule of thumb: an action that only its own step can disable needs weak fairness at most. An action that other processes can disable may need strong fairness.

Write something before openingunder SFSpec, what can the hostile scheduler from 10-01 still do?

Delay. Strong fairness says infinitely often, not soon. The scheduler may let one process go round a thousand times before the other gets in, and that behavior satisfies SFSpec and NoStarvation. Nothing in this chapter has a clock. If the requirement is "within three rounds", that is a deadline, and chapter 09 showed that a deadline is a safety property, checked with a counter and an invariant. No fairness condition can give it to you.

Write something before openingif strong fairness is stronger, why not use it everywhere and stop thinking?

Because a stronger assumption is a bigger promise about the world, and every promise in a spec is one the implementation has to keep. Strong fairness on everything also forces things nobody controls, such as users asking for the lock or philosophers getting hungry. Lesson 10-04 runs exactly that experiment and shows what it hides.

04In your editor

Check this lesson's exercises and the specs it uses with:

shell
./tla check 10-03

The weak-fairness philosophers, failing:

Output 10-fairness/specs/DiningPhilosophers.wf.live
10-fairness/specs/DiningPhilosophers.wf.live  DiningPhilosophers.wf.live.cfg  (N=3)
parse ok
model check: temporal property NoStarvation violated; lasso of 8 states, looping back to state 5; 42 distinct states
expected outcome met: property NoStarvation
trace (changed variables only; ./tla trace --full shows all):
  1  Init        state = (0 :> "thinking" @@ 1 :> "thinking" @@ 2 :> "thinking"), fork = (0 :> -1 @@ 1 :> -1 @@ 2 :> -1)
  2  GetHungry   state: (0 :> "thinking" @@ 1 :> "thinking" @@ 2 :> "thinking") -> (0 :> "thinking" @@ 1 :> "hungry" @@ 2 :> "thinking")
  3  GetHungry   state: (0 :> "thinking" @@ 1 :> "hungry" @@ 2 :> "thinking") -> (0 :> "thinking" @@ 1 :> "hungry" @@ 2 :> "hungry")
  4  GetHungry   state: (0 :> "thinking" @@ 1 :> "hungry" @@ 2 :> "hungry") -> (0 :> "hungry" @@ 1 :> "hungry" @@ 2 :> "hungry")
  5  TakeFirst   state: (0 :> "hungry" @@ 1 :> "hungry" @@ 2 :> "hungry") -> (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "hungry"), fork: (0 :> -1 @@ 1 :> -1 @@ 2 :> -1) -> (0 :> -1 @@ 1 :> 0 @@ 2 :> -1)
  6  TakeFirst   state: (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "hungry") -> (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "holding"), fork: (0 :> -1 @@ 1 :> 0 @@ 2 :> -1) -> (0 :> -1 @@ 1 :> 0 @@ 2 :> 2)
  7  TakeSecond  state: (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "holding") -> (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "eating"), fork: (0 :> -1 @@ 1 :> 0 @@ 2 :> 2) -> (0 :> 2 @@ 1 :> 0 @@ 2 :> 2)
  8  PutDown     state: (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "eating") -> (0 :> "holding" @@ 1 :> "hungry" @@ 2 :> "thinking"), fork: (0 :> 2 @@ 1 :> 0 @@ 2 :> 2) -> (0 :> -1 @@ 1 :> 0 @@ 2 :> -1)
     GetHungry(2)  back to state 5

To decide between WF and SF for an action, find the counterexample under WF first. Then look at the action in the loop. If it is enabled in some loop states and disabled in others, SF on it rules out this loop; rerun, because there may be others. If it is disabled in every loop state, fairness on this action cannot help. Find the action that keeps it disabled (for example the holder's Release) and ask whether that action deserves fairness. Only if no honest fairness breaks the loop does the design have a liveness bug.

05Practice

Warm-up. A panel with one microphone. One conjunct of its fairness is too weak; change it.

Core. Feed the philosophers: write the fairness that makes NoStarvation hold, without forcing anyone to get hungry.

10-03-ex1Warm-up5 min

10-03-ex1 · Pass the microphone

Tier: warm-up. Time: about 5 minutes.

A panel discussion with one microphone. A speaker raises a hand, takes the microphone when it is free, speaks, and hands it back. The moderator's promise: everyone who raises a hand gets to speak.

  1. Run ./tla check 10-03-ex1. Microphone.cfg reports EveryoneSpeaks violated. Read the loop: in which of its states could the waiting speaker take the microphone, and in which could it not?
  2. Gap g1 has two conjuncts of fairness. One of them is too weak for what the loop shows. Change it, and only it.
  3. Run the check again. Both cfgs must pass, with every action firing and the witness OneWaits reached.

MCMicrophone.cfg is the course's check that your fairness does not assume too much: no fairness on raising a hand, because nobody has to want to speak. Leave MCMicrophone.tla and its cfg as they are.

Then say, in one sentence, what the moderator would have to do for the fairness you wrote to be true of a real panel.

Hints: HINTS.md, under "10-03-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/10-fairness/exercises/03-ex1-pass-the-microphone/; open it after you have a passing answer of your own.

Done when: ./tla check 10-03-ex1 prints "2 of 2 checks met their expected outcome".

./tla check 10-03-ex1

Repo chapters/10-fairness/exercises/03-ex1-pass-the-microphone/

Hints

Hint 1

In the loop, the waiting speaker's TakeMic is enabled whenever the microphone is free, and disabled while the other speaker holds it. Is that "continuously" or "infinitely often"?

Hint 2

HandBack is fine as it is: a speaker who holds the microphone can always hand it back, so its HandBack is enabled until it happens. Only the conjunct on taking the microphone needs to change.

Hint 3 (nearly the answer)

The new hypothesis has to cover an action that is disabled once per round and so never enabled for good: lesson 10-03 named the kind of fairness with that hypothesis. In the real panel, a moderator who keeps a list of raised hands is what keeps that promise.

10-03-ex2Core8 min

10-03-ex2 · Feed the philosophers

Tier: core. Time: about 8 minutes.

The table from lesson 10-03: three philosophers, three forks, and philosopher 0 reaching for the other fork first. LiveSpec has no fairness yet. Your job is the fairness that makes NoStarvation hold: every hungry philosopher eventually eats.

  1. Run ./tla check 10-03-ex2. DiningPhilosophers.cfg reports NoStarvation violated. The invariant EatersHoldForks holds; the problem is liveness only.
  2. Fill gap g1. Start with weak fairness on every action a philosopher takes once hungry, and run it. Read the new lasso the way lesson 10-03's IN YOUR EDITOR section says: for each action of each starving philosopher, is it enabled in every loop state, in some, or in none?
  3. Strengthen only the conjuncts the lasso shows you need, and run again. It must pass, with every action firing and the witness EatsWhileNeighbourWaits reached.

MCDiningPhilosophers.cfg is the course's check that your fairness does not assume too much. It allows strong fairness on everything a hungry philosopher does, and nothing on GetHungry: nobody can make a philosopher hungry. Leave MCDiningPhilosophers.tla and its cfg as they are.

For each conjunct you wrote, add a comment saying what it promises about the table, in a sentence a waiter would understand.

Hints: HINTS.md, under "10-03-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/10-fairness/exercises/03-ex2-feed-the-philosophers/; open it after you have a passing answer of your own.

Done when: ./tla check 10-03-ex2 prints "2 of 2 checks met their expected outcome".

./tla check 10-03-ex2

Repo chapters/10-fairness/exercises/03-ex2-feed-the-philosophers/

Hints

Hint 1

Never put fairness on GetHungry: the ceiling forbids it, and it would make every philosopher eat whether the table works or not. Start with WF_vars on TakeFirst(p), TakeSecond(p) and PutDown(p) for every p.

Hint 2

In the lasso under weak fairness, find the philosopher who holds one fork and waits for the other. Its pick-up is enabled whenever that fork lies on the table and disabled whenever the neighbour holds it. Strengthen that action, run again, and read the next lasso the same way: it may point at the other pick-up.

Hint 3 (nearly the answer)

The two pick-ups need the same change. PutDown(p) does not, because an eating philosopher can always put the forks down, and only its own PutDown stops it. Keep the quantifier outside every conjunct.

06Explain it

Open NOTES.md and answer the three prompts under 10-03, about the Core exercise.

output
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:

Compare your answers with "10-03 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "Weak fairness failed because a waiting philosopher's next pick-up is enabled whenever its fork is on the table and disabled whenever a neighbour holds it: infinitely often, never continuously. Strong fairness on TakeFirst and TakeSecond forces those pick-ups; PutDown stays weak because an eating philosopher can always put down. Nothing forces anyone to get hungry, and the ceiling cfg checks that. The pass assumes something like a waiter who remembers who has been waiting; no ordinary scheduler gives strong fairness."

A weak answer: "WF wasn't enough so I used SF." It does not say why weak fairness failed, which actions needed the change, or what the strong fairness assumes about the table.

Done?