10-04
Chapter 10 · Fairness, derived
Fairness hides liveness bugs, never safety bugs
00Recall
One card from 10-03 on why fairness leaves the state count alone, one from 09-01 on what makes a property safety or liveness, and one from 06-05 on what CHECK_DEADLOCK FALSE hides. Today all three meet: an assumption that cannot touch a safety bug, and one that quietly erases a liveness bug.
10-03-b from When WF is not enough
FairLock reports the same number of distinct states with no fairness and with strong fairness. Why?
Show answer
Fairness does not change which states are reachable. It only changes which infinite paths through the graph count as behaviors of the spec.
09-01-a from The prefix judge
What makes a property a safety property, and what makes it a liveness property?
Show answer
Safety: every violating behavior has a finite prefix that already violates it, whatever comes next. Liveness: every finite prefix can still be continued into a behavior that satisfies it.
06-05-b from Deadlock as TLC reports it
Why mark a proper end with a guarded Finished step rather than CHECK_DEADLOCK FALSE?
Show answer
Finished silences the report only in the intended end state, so every other stuck state is still reported. CHECK_DEADLOCK FALSE hides them all.
Skip recall Recall is never required.
01Commit
A teammate copied the philosophers' table from 10-03 into specs/SloppyPhilosophers.tla and changed one line: TakeSecond no longer looks whether the second fork is on the table. Their safety check, EatersHoldForks, now fails. Their fix was to add strong fairness to every action, GetHungry included:
SFEverywhereSpec is defined as
and Spec
and for all p in Phils : SF_vars(GetHungry(p))
and for all p in Phils : SF_vars(TakeFirst(p))
and for all p in Phils : SF_vars(TakeSecond(p))
and for all p in Phils : SF_vars(PutDown(p))Commit a
SloppyPhilosophers.sf.cfg checks EatersHoldForks against SFEverywhereSpec. What does TLC report?
- No violation: strong fairness makes the neighbour put the fork down before anyone else takes it.
- The same violation as with no fairness, with a trace of the same length.
- A violation, but with a longer trace: fairness rules out the shortest one.
Answer The same violation as with no fairness, with a trace of the same length.
- If you picked A
- Fairness on Next's own actions never stops a step from happening. It only insists that some steps happen eventually. The sloppy TakeSecond is still allowed in exactly the states where it was allowed before.
- If you picked C
- The shortest trace is a finite prefix. Ask whether a fair scheduler could have produced those same first few steps. Fairness is about what happens in the infinite tail, after the bad state.
On the correct table, SFEverywhereSpec makes NoStarvation pass, where weak fairness failed. The module also defines a property about the people at the table, not the forks:
HungryAgain is defined as for all p in Phils : alwayseventually(state[p] = "hungry")Commit b
DiningPhilosophers.hunger.live.cfg checks HungryAgain, "every philosopher is hungry infinitely often", against SFEverywhereSpec. What does TLC report?
- A violation: nothing in the spec forces anyone to get hungry.
- No violation: the spec now says every philosopher gets hungry again and again.
- A violation: TLC gives no fairness to GetHungry, because getting hungry is the environment's choice.
Answer No violation: the spec now says every philosopher gets hungry again and again.
- If you picked A
- Look at the first conjunct of SFEverywhereSpec again. GetHungry is enabled whenever a philosopher is thinking, and strong fairness demands that an action enabled infinitely often happens infinitely often.
- If you picked C
- TLC does not know which actions you think of as "the environment". To TLC, SF_vars(GetHungry(p)) is a conjunct like any other, and it is honoured. Keeping fairness off the environment is your job.
02Lab
No browser lab today. Run the two checks from the repository root:
./tla check chapters/10-fairness/specs/SloppyPhilosophers.sf.cfg
./tla check chapters/10-fairness/specs/DiningPhilosophers.hunger.live.cfg10-fairness/specs/SloppyPhilosophers.sf10-fairness/specs/SloppyPhilosophers.sf SloppyPhilosophers.sf.cfg (N=3)
parse ok
model check: invariant EatersHoldForks violated; trace has 7 states (6 steps); 45 distinct states
expected outcome met: violation EatersHoldForks
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 :> "hungry" @@ 1 :> "thinking" @@ 2 :> "thinking")
3 GetHungry state: (0 :> "hungry" @@ 1 :> "thinking" @@ 2 :> "thinking") -> (0 :> "hungry" @@ 1 :> "thinking" @@ 2 :> "hungry")
4 TakeFirst state: (0 :> "hungry" @@ 1 :> "thinking" @@ 2 :> "hungry") -> (0 :> "holding" @@ 1 :> "thinking" @@ 2 :> "hungry"), fork: (0 :> -1 @@ 1 :> -1 @@ 2 :> -1) -> (0 :> -1 @@ 1 :> 0 @@ 2 :> -1)
5 TakeFirst state: (0 :> "holding" @@ 1 :> "thinking" @@ 2 :> "hungry") -> (0 :> "holding" @@ 1 :> "thinking" @@ 2 :> "holding"), fork: (0 :> -1 @@ 1 :> 0 @@ 2 :> -1) -> (0 :> -1 @@ 1 :> 0 @@ 2 :> 2)
6 TakeSecond state: (0 :> "holding" @@ 1 :> "thinking" @@ 2 :> "holding") -> (0 :> "eating" @@ 1 :> "thinking" @@ 2 :> "holding"), fork: (0 :> -1 @@ 1 :> 0 @@ 2 :> 2) -> (0 :> 0 @@ 1 :> 0 @@ 2 :> 2)
7 TakeSecond state: (0 :> "eating" @@ 1 :> "thinking" @@ 2 :> "holding") -> (0 :> "eating" @@ 1 :> "thinking" @@ 2 :> "eating"), fork: (0 :> 0 @@ 1 :> 0 @@ 2 :> 2) -> (0 :> 2 @@ 1 :> 0 @@ 2 :> 2)Reveal for commit a
The same violation. With strong fairness on everything, TLC reports EatersHoldForks violated by a trace of 7 states, after 45 distinct states. SloppyPhilosophers.cfg, with no fairness at all, reports 7 states and 45 distinct states. The fairness changed nothing.
Reveal for commit b
No violation of HungryAgain. Under SFEverywhereSpec every philosopher is hungry infinitely often, because the spec says so: SF_vars(GetHungry(p)). Under WFSpec, which says nothing about getting hungry, DiningPhilosophers.wfhunger.live.cfg finds a lasso of 4 states in which someone thinks for ever.
03Reason
If you picked "no violation" for the first question, you hoped the fairness would make the neighbour wait its turn. Fairness on Next's own actions cannot make anyone wait; the end of the lesson shows why that qualifier matters. Every fairness condition has the shape "if this keeps being possible, it eventually happens". Placed on actions of Next, it adds demands about the end of a behavior and never removes a step.
Now ask the question the definition of safety invites. A safety violation is a finite prefix: a few steps, and then a bad state. Take that prefix. Could a fair scheduler have produced it?
Write something before openingfrom the bad state in TLC's trace, can the scheduler carry on in a way that meets every one of the teammate's fairness conditions?
Yes. Each condition asks for steps of one action, and only while that action is enabled. Each of those actions is part of Next, so whenever a condition makes a demand, a step that meets it is one the spec allows from that very state. No condition can ever demand the impossible, and a careful scheduler can take turns meeting all of them, from any state, for ever; the callout below names the theorem. So the prefix that ends in the bad state is the beginning of a behavior that SFEverywhereSpec allows. The bad state is still reachable, and the invariant still fails.
TLC takes a shortcut that agrees with the theorem: when it checks an invariant, it builds the graph from Init and Next and never uses the fairness conjuncts at all. That is why the counts are identical. Notice that the shortcut is safe only because of the theorem. If fairness could rule out a finite prefix, an invariant checked without it could report a bad state that no behavior of the spec reaches. The end of this lesson builds a spec like that on purpose.
So fairness on subactions of Next can never hide a safety bug. It can hide a liveness bug, and the second question shows how. NoStarvation passes under SFEverywhereSpec and fails under weak fairness, on the same table, with the same 42 distinct states. Nothing about the forks changed. Two kinds of promise were added (strong fairness on PutDown(p) adds nothing, since only a philosopher's own PutDown can disable it):
| Conjuncts | What they promise about the world | Who could keep it |
|---|---|---|
SF_vars(TakeFirst(p)), SF_vars(TakeSecond(p)) | A philosopher whose next fork keeps reappearing eventually wins it | A waiter or a queue that remembers who asked first. Not an ordinary scheduler. |
SF_vars(GetHungry(p)) | Every philosopher wants to eat again and again | Nobody. People, clients and users do what they like. |
The first is a design you have not built. The second is the world doing your job for you. Strip both and the starvation from 10-03 is back, because it never left. A pass that rests on a promise nobody keeps is a liveness bug with the alarm turned off.
Write something before openingstrong fairness on GetHungry did not change NoStarvation's verdict here. Why is it still a bad conjunct?
Because it changes what every other liveness property means. A property such as \A p : []<>(state[p] = "eating"), "every philosopher eats again and again", can pass only because philosophers are forced to keep getting hungry, and a property about a server can pass only because clients are forced to keep sending. Fairness on what the environment does turns "the system responds" into "the system responds, provided everyone keeps asking".
That covers fairness on actions inside Next. What about an action outside it?
The invariant shortcut now misleads too. Every behavior of MergedSpec satisfies ANeverWaits, "A never waits", because the fairness forbids every behavior in which A asks. Yet LostAcquire.inv.cfg reports it violated by a trace of 2 states: TLC checked it without the fairness that rules that state out.
Two of ./tla's checks see through the vacuous pass. The masked run's outcome is coverage-gap Release: Release never fires, and the coverage section of the raw TLC log shows it with a count of 0. And LostAcquire.live.cfg, with deadlock checking left on, reports the stuck state where both processes wait:
- State 1 Initial state pc = (A :> "idle" @@ B :> "idle") lock = FALSE
- State 2 Request pc (A :> "idle" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "idle")
- State 3 Request pc (A :> "waiting" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "waiting")
Here is the rule this lesson leaves you with. Put fairness only on actions A such that every A step is a Next step, such as the named disjuncts of Next. Then fairness cannot hide a safety bug, and every liveness pass is as honest as the promises its fairness makes.
04In your editor
Check this lesson's exercises and the specs it uses with:
./tla check 10-04The merged lock, with deadlock checking on:
10-fairness/specs/LostAcquire.live10-fairness/specs/LostAcquire.live LostAcquire.live.cfg (A=A, B=B)
parse ok
model check: deadlock reached; trace has 3 states (2 steps); 4 distinct states
expected outcome met: deadlock
trace (changed variables only; ./tla trace --full shows all):
1 Init pc = (A :> "idle" @@ B :> "idle"), lock = FALSE
2 Request pc: (A :> "idle" @@ B :> "idle") -> (A :> "waiting" @@ B :> "idle")
3 Request pc: (A :> "waiting" @@ B :> "idle") -> (A :> "waiting" @@ B :> "waiting")Before you add a fairness conjunct, sort its action into one of three groups. Your components, such as the lock, the scheduler or a retry loop: fairness allowed, with the mechanism named. Infrastructure, such as the network, timers, the operating system or the power supply: fairness allowed only with a named mechanism and the thing that breaks it. Users and clients, anyone who decides whether to ask: never. If the subject of the conjunct's sentence (lesson 10-02) is a user, a client or a philosopher, delete the conjunct.
05Practice
Warm-up. A teammate tried to fix shared forks with strong fairness. Fix the action instead.
Core. A polite background job passes NoStarvation under strong fairness on everything. Write the fairness the real system has, and watch the starvation come back.
10-04-ex1Warm-up5 min
10-04-ex1 · Fairness won't fix it
Tier: warm-up. Time: about 5 minutes.
A teammate's dining philosophers. Two neighbours sometimes end up eating with the same fork, and the teammate's answer was SFEverywhereSpec: strong fairness on every action. The cfg checks EatersHoldForks under that spec.
- Run
./tla check 10-04-ex1. Read the trace. Which step puts a fork in a second philosopher's hand? - Before you change anything, write down why the strong fairness did not stop that step. Lesson 10-04 has the answer in one sentence.
- Fill gap g1 in
SharedForks.tla: a philosopher may take its second fork only when that fork is on the table. - Run the check again. It must pass, with every action firing and the witness
EatsWhileNeighbourWaitsreached.
Leave the SPECIFICATION and INVARIANT lines of the cfg as they are. The point is that the fix lives in the action, and that the teammate's fairness, however strong, could not have done its job.
Hints: HINTS.md, under "10-04-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/10-fairness/exercises/04-ex1-fairness-wont-fix-it/; open it after you have a passing answer of your own.
Done when: ./tla check 10-04-ex1 prints "goal met: pass".
./tla check 10-04-ex1
Repo chapters/10-fairness/exercises/04-ex1-fairness-wont-fix-it/
Hints
Hint 1
Look at the last step of the trace. The philosopher who takes its second fork takes one that a neighbour already holds. Which guard of TakeSecond would have stopped it?
Hint 2
TakeFirst already has the guard you need, for the first fork. TakeSecond needs the same guard for the second fork.
Hint 3 (nearly the answer)
Replace the placeholder with a condition that the second fork, Second(p), lies on the table: fork maps it to NoOne. Strong fairness could not have done this, because fairness on Next's actions never removes a step; only a guard does.
10-04-ex2Core8 min
10-04-ex2 · The polite background job
Tier: core. Time: about 8 minutes.
A lock shared by a foreground process A and a background job B. To keep the foreground snappy, B is polite: it takes the lock only while A is idle. A teammate checked NoStarvation under SFSpec, strong fairness on every action, and it passed. They want to ship.
Your job is to write HonestSpec, the fairness the real system has, and see what the pass was resting on. The real system runs on a preemptive scheduler that gives every runnable thread a time slice, so a thread that can run, and stays able to run, eventually runs. That is the scheduler's promise. It is still an assumption, and you will write it down. Nobody has to ask for the lock.
- Run
./tla check 10-04-ex2. Four cfgs run.PoliteLock.cfgpasses under the starter'sHonestSpec == SFSpec.MCPoliteLock.cfgfails: it is the course's check thatHonestSpecassumes no more than a real scheduler gives, andSFSpecassumes more. - Fill gap g1 with the scheduler's promise, quantified over the processes. Decide for each action whether it is the process's own choice or the system's job, and give fairness only to the second kind.
- Run the check. Now
PoliteLock.cfgmust reportNoStarvationviolated: read the lasso and say which process starves, and why weak fairness on itsAcquiredid not save it.MCPoliteLock.cfgmust pass.PoliteLock.progress.cfgandMCPoliteLock.floor.cfgmust pass too: they check that your fairness is not too weak. Under the scheduler's promise, someone waiting means someone gets the lock, and the foreground processA, whichBalways gives way to, never starves.
Leave MCPoliteLock.tla and the three course cfgs as they are.
Then write one sentence for the teammate: what the SFSpec pass was assuming about the world, and which change to the design would remove the starvation.
Hints: HINTS.md, under "10-04-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/10-fairness/exercises/04-ex2-the-polite-background-job/; open it after you have a passing answer of your own.
The goals differ: "goal met: property NoStarvation" for PoliteLock.cfg, and "goal met: pass" for the other three.
Done when: ./tla check 10-04-ex2 prints "4 of 4 checks met their expected outcome".
./tla check 10-04-ex2
Repo chapters/10-fairness/exercises/04-ex2-the-polite-background-job/
Hints
Hint 1
"A thread that can run, and stays able to run, eventually runs" is weak fairness. Which actions are the lock's and the scheduler's to perform, and which is a process deciding it wants the lock?
Hint 2
Ask, for each of the three actions, whose decision it is: the process's own, or the lock's and the scheduler's. Fairness goes only on the second kind, and only as much as the README's scheduler promises. If PoliteLock.progress.cfg or MCPoliteLock.floor.cfg fails, you left something out.
Hint 3 (nearly the answer)
HonestSpec is Spec with two quantified conjuncts of one kind. In the lasso, B's Acquire is enabled only in the moments when A is idle and the lock is free; A asks again each time, which disables it. The design fix is to stop A from overtaking a waiting B, for example by queueing.
06Explain it
Open NOTES.md and answer the three prompts under 10-04, about the Core exercise.
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-04 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "Under weak fairness on Acquire and Release, the scheduler's promise and still an assumption, B starves: A keeps asking again, and B's polite Acquire is enabled only in the moments when A is idle. SFSpec hid that by assuming strong fairness on B's Acquire: a scheduler that eventually runs B, and keeps doing so, if B is runnable infinitely often. Its strong fairness on Request forces everyone to keep asking, an assumption of its own that this pass did not need. Progress still holds, so the lock is used; the bug is who uses it. TLC checked one foreground process and one background job; the fix is a design change, not a conjunct."
A weak answer: "With WF it fails and with SF it passes, so SF is needed." It reads the stronger assumption as the answer instead of as the thing that hid the bug.
Done?