09-05

Chapter 09 · Eventually

Deadlock, termination, liveness

  • 45 min core
  • Essentials track
  • Skills: counterexamples, evidence

00Recall

One card from 09-04 (why a bare spec satisfies no liveness property), one from 06-05 (why a guarded Finished beats CHECK_DEADLOCK FALSE), and one from 00-03 (the difference between a TLC error and a violation). TLC has three kinds of bad news: a violation, an error and a deadlock. Today adds a word that is not a report at all: termination.

09-04-a from Doing nothing is legal

Why does a spec of the form Init /\ [][Next]_vars satisfy no liveness property like <>Done?

Show answer

[][Next]_vars allows stuttering steps, so every behavior may stop at any point and repeat its state for ever. Unless Done already holds there, <>Done fails.

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.

00-03-b from Your first TLC run

What is the difference between a TLC error and a violation?

Show answer

A violation means TLC found a reachable state where your invariant is false, and it shows a trace to it (so does a deadlock, a reachable state where no step is possible). An error means TLC could not evaluate, parse or configure something, so there is no verdict.

Skip recall Recall is never required.

01Commit

Two teammates' copies of specs/BoundedBuffer.tla, each differing in one marked place. HaltingBuffer has no Finished disjunct in Next. StuckBuffer has an off-by-one: the producer stops one item short of the batch. Each is checked with the same safety cfg, deadlock checking on, and TLC reports deadlock reached for both. Lesson 06-05 taught you to read the last state: HaltingBuffer stops with the batch done, a finished run; StuckBuffer stops with an item still owed, a jammed one.

HaltingBuffer.live.cfg turns deadlock checking off, and its comment says why and what the switch would also hide. It checks EventuallyDone == <>Done against the fair spec.

Commit a

HaltingBuffer has no Finished step and deadlocks where the batch is done. HaltingBuffer.live.cfg turns deadlock checking off and checks EventuallyDone == <>Done against the fair spec. What does TLC report?

  1. A violation: the behavior stops for ever, and a system that stops makes no progress.
  2. No violation: every behavior reaches Done, and <>Done asks for nothing after that.
  3. A deadlock again: switching the check off in one cfg does not remove it from the spec.

Answer No violation: every behavior reaches Done, and <>Done asks for nothing after that.

A third copy, FussyBuffer, adds a consumer that inspects items: an item that fails inspection goes back into the buffer. Its spec has the fairness assumption from 09-04, and its safety cfg, deadlock checking on, finds no violation.

Commit b

FussyBuffer.live.cfg checks EventuallyDone against the fair spec of a buffer whose consumer may send an item back after inspecting it. What does TLC report?

  1. No violation: there is no deadlock, and fairness keeps the buffer moving.
  2. A violation that stutters: fairness does not cover the new action.
  3. A violation with a loop: taking an item and returning it can repeat for ever.

Answer A violation with a loop: taking an item and returning it can repeat for ever.

02Lab

Six runs, in the order of the table in REASON:

shell
./tla check chapters/09-eventually/specs/BoundedBuffer.cfg
./tla check chapters/09-eventually/specs/HaltingBuffer.cfg
./tla check chapters/09-eventually/specs/HaltingBuffer.live.cfg
./tla check chapters/09-eventually/specs/StuckBuffer.cfg
./tla check chapters/09-eventually/specs/FussyBuffer.cfg
./tla check chapters/09-eventually/specs/FussyBuffer.live.cfg

The halting buffer, judged twice:

Expected output 09-eventually/specs/HaltingBuffer
09-eventually/specs/HaltingBuffer  HaltingBuffer.cfg  (Items=3, Cap=2)
parse ok
model check: deadlock reached; trace has 7 states (6 steps); 9 distinct states
expected outcome met: deadlock
trace (changed variables only; ./tla trace --full shows all):
  1  Init  buf = 0, made = 0, taken = 0
  2  Put   buf: 0 -> 1, made: 0 -> 1
  3  Put   buf: 1 -> 2, made: 1 -> 2
  4  Take  buf: 2 -> 1, taken: 0 -> 1
  5  Put   buf: 1 -> 2, made: 2 -> 3
  6  Take  buf: 2 -> 1, taken: 1 -> 2
  7  Take  buf: 1 -> 0, taken: 2 -> 3
Expected output 09-eventually/specs/HaltingBuffer.live
09-eventually/specs/HaltingBuffer.live  HaltingBuffer.live.cfg  (Items=3, Cap=2)
parse ok
model check: no violation found in 9 distinct states (11 generated, depth 7)
expected outcome met: pass
coverage: all 2 actions fired
witness BufferFull reached (trace of 3 states)
witness Done reached (trace of 7 states)

Reveal for commit a

No violation, in 9 distinct states, with both witnesses reached. The same buffer that the safety cfg reports as a deadlock is live. Its deadlock trace of 7 states ends with every item taken, and <>Done needs nothing after that. A deadlock is about what can happen next in one state; liveness is about what has happened in a whole behavior.

Reveal for commit b

A violation, shown as a lasso of 7 states: the loop takes an item and returns it, and goes round again. The safety run of the same buffer reports no violation and no deadlock in 9 distinct states. A system can be busy for ever and never finish.

03Reason

If you said a buffer that stops makes no progress, you treated a TLC report as a verdict on the behavior. A deadlock report is a question about one state: "the system cannot take another step here; did you mean that?" Liveness asks about the behavior that led there.

Three words get mixed up, and each names something different.

  • Deadlock is a TLC report: a reachable state in which no Next step is possible. It is not part of the logic: the behavior stutters there for ever. TLC checks for it by default, because an unexpected stop can be a bug. Lesson 06-05 told a finished run from a jammed one with a guarded Finished step.
  • Termination is a design intent: the system reached the state where its work is done. In TLA+ a terminated behavior stutters for ever in its final state (chapter 01). TLC cannot tell that from a deadlock unless the spec says so.
  • Liveness is a property you write: something good eventually happens. It is checked only if a cfg names it.

Now put the four buffers side by side. The safety cfg keeps deadlock checking on; the liveness cfg uses the fair spec.

BufferDeadlock check<>Done
BoundedBufferpasspass
HaltingBufferdeadlockpass
StuckBufferdeadlockdeadlock
FussyBufferpassproperty

Two combinations you might have thought impossible are in that table: HaltingBuffer deadlocks and is live, and FussyBuffer never deadlocks and is not live. StuckBuffer's liveness entry comes back at the end of REASON.

Write something before openingcould an invariant have caught StuckBuffer, instead of the deadlock check?

Not an invariant over the values of buf, made and taken alone. Look at the last state of its trace: the buffer empty, two items made and two taken. The working BoundedBuffer passes through a state with exactly the same values, one step before the producer makes the last item, so no predicate over those values can tell the two apart. What differs is what can happen next: in the working buffer a Put follows, and in the stuck one nothing does. A state predicate can say that too: Done \/ ENABLED Next is a legal invariant, it catches StuckBuffer, and it is in effect what the deadlock check is (TLC evaluates ENABLED by looking for a successor). Liveness is different again: it judges whole behaviors, which no state predicate can do.

So read the end of a failing liveness trace like this, starting from ./tla's summary line.

  • then stuttering forever, and a real step is possible in the last state: an assumption is missing, the case lesson 09-04 is about.
  • then stuttering forever, no real step is possible, and the work is not done: a deadlock that something silenced, CHECK_DEADLOCK FALSE or an unguarded Finished. With the check on and nothing silencing it, TLC reports the deadlock instead.
  • then stuttering forever where a guarded Finished lets the system rest: it terminated as designed, and the property wanted something the run never delivered. Look in the stem for what went wrong.
  • looping back to state k: the system keeps taking real steps and never gets there. Read the loop. It is a design bug, or an environment the requirement has to assume away, as in this lesson's Core.

The same reading works on claims an AI makes. Say an assistant writes an agent that retries a failed tool call "until it succeeds", and says the call always succeeds in the end. Model the retry and check the claim. With no fairness, TLC answers with a stutter where a retry is enabled. With fairness and a tool that may fail every time, it answers with a loop of call and fail, the shape of this lesson's Core, FlakyLink. Neither trace is a bug in the retry code. Each names an assumption the claim left out.

04In your editor

The four buffers are in chapters/09-eventually/specs/, each with a .cfg and, where it matters, a .live.cfg. Check this lesson's exercises with:

shell
./tla check 09-05

The buffer with a guarded Finished:

Output 09-eventually/specs/BoundedBuffer
09-eventually/specs/BoundedBuffer  BoundedBuffer.cfg  (Items=3, Cap=2)
parse ok
model check: no violation found in 9 distinct states (12 generated, depth 7)
expected outcome met: pass
coverage: all 3 actions fired
witness BufferFull reached (trace of 3 states)
witness Done reached (trace of 7 states)

05Practice

Warm-up. A printer that finishes its job is reported as a deadlock. Say where the printer may rest, and nowhere else. The course checks that two jammed printers are still reported.

Core. A client retries a request over a network that can lose it, and the retries can go on for ever. Write the liveness property, read the loop, then write down, as a bound in the model, the assumption about the network that makes the property hold.

Stretch. A launch countdown with holds, from a skeleton, including both cfgs.

Before the Core, decide: when TLC shows you the loop, is it a bug in the client, or a fact about the network that the requirement has to assume away?

09-05-ex1Warm-up5 min

09-05-ex1 · Finished means finished

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

A printer works through one job, page by page. When the job is out, nothing else happens, and TLC reports a deadlock in both PrintJob cfgs.

  1. Run ./tla check 09-05-ex1. Four cfgs run. Read where the PrintJob.cfg trace stops. Is the job done there?
  2. Fill gap g1: a Finished step that changes nothing and is allowed only once the whole job is out.
  3. Run the check. PrintJob.cfg and PrintJob.live.cfg must pass. MCPrintJob.cfg and MCPrintJob.idle.cfg run two printers that jam before the job is out, one with the head printing on the second page and one with the head idle and only the last page owed, each with your Finished in its Next; TLC must still report both deadlocks.

If either MCPrintJob cfg passes, your Finished lets a jammed printer rest, and it would hide real deadlocks in this spec as well. Lesson 09-05 calls that CHECK_DEADLOCK FALSE written inside the spec.

All four cfgs must meet their goals: "goal met: pass" for PrintJob.cfg and for PrintJob.live.cfg, and "goal met: deadlock" for MCPrintJob.cfg and for MCPrintJob.idle.cfg. If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Hints: HINTS.md, under "09-05-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/05-ex1-finished-means-finished/; open it after you have a passing answer of your own.

Done when: ./tla check 09-05-ex1 prints "goal met: pass".

./tla check 09-05-ex1

Repo chapters/09-eventually/exercises/05-ex1-finished-means-finished/

Hints

Hint 1

Read the last state of PrintJob.cfg's trace. Is the job done there? Then read what the two MCPrintJob cfgs check, and why each must still deadlock.

Hint 2

Finished changes nothing, so it is UNCHANGED vars plus a condition. The condition is the difference between a finished printer and a jammed one, and one of the jammed printers stops with its head idle.

Hint 3 (nearly the answer)

The guard has to be true only when the whole job is out, and the module already defines a predicate that says so. A guard of "nothing else can happen" is CHECK_DEADLOCK FALSE again: it lets any stuck printer rest.

09-05-ex2Core8 min

09-05-ex2 · The network that always loses

Tier: core. Time: about 8 minutes.

A client sends one request over a network that can lose it, and sends again after every loss. Once a copy gets through, the client is done. The requirement: the request gets through, sooner or later.

  1. Run ./tla check 09-05-ex2. Three of the four cfgs pass, because the property is still TRUE. Notice that the network's losses have no limit yet. MCCountedLink.cfg already reports LossesCounted violated: the starter's Lose does not count its losses.
  2. Fill gap g2, EventuallyDelivered. Run the check: FlakyLink.live.cfg should now fail. Is the trace a stutter or a loop? Which actions are in it?
  3. The loop is not a bug in the client: a network that loses every copy for ever defeats any client. The requirement needs an assumption about the network. Fill gap g1: Lose may fire only while fewer than MaxLosses copies have been lost, and it counts the loss.
  4. Run the check. Both FlakyLink cfgs must pass, with every action firing. MCFlakyLink.cfg runs the network with no bound; your property must fail there. MCCountedLink.cfg counts your Lose steps from outside and checks that losses agrees in every state; its witness, LostEverything, needs the client to retry after the network has lost every copy it may lose.

Chapter 10 meets a second way to write this assumption, without a counter.

All four cfgs must meet their goals: "goal met: pass" for FlakyLink.cfg, FlakyLink.live.cfg and MCCountedLink.cfg, and "goal met: property EventuallyDelivered" for MCFlakyLink.cfg. If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Hints: HINTS.md, under "09-05-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/05-ex2-the-network-that-always-loses/; open it after you have a passing answer of your own.

Done when: ./tla check 09-05-ex2 prints "goal met: property EventuallyDelivered".

./tla check 09-05-ex2

Repo chapters/09-eventually/exercises/05-ex2-the-network-that-always-loses/

Hints

Hint 1

After you write the property, read the trace's end. A loop of Send and Lose is the network winning every time. Could any client do better against that network?

Hint 2

The requirement can only hold if the network does not lose every copy for ever. In this model the assumption is a counter: Lose may fire while losses is below MaxLosses, and each loss adds one. A counter with a fixed bound is a stronger assumption than "the network loses only finitely many copies", but it is the simplest one to write in a finite model. Chapter 10 states the weaker one.

Hint 3 (nearly the answer)

EventuallyDelivered needs one operator from 09-02's table. In Lose, the TRUE becomes a guard that mentions MaxLosses, and the UNCHANGED line becomes one line for losses and one for delivered. If MCCountedLink.cfg does not reach LostEverything, your guard ignores MaxLosses; if it reports LossesCounted violated, a loss does not add exactly one.

09-05-ex3Stretch40 min · mastery path

09-05-ex3 · Launch countdown

Tier: stretch. Time: about 40 minutes.

A blank page, almost. Launch.tla names the variables, the actions and two witnesses; every body is a placeholder, and so are both cfgs. You write the rest.

The countdown:

  • The clock starts at Start and counts down by one while it is running and above zero.
  • The launch director may hold a running count above zero, but no more than MaxHolds times. A held count can resume.
  • At zero, the rocket ignites, once. After that nothing else happens.
  • Safety, NoEarlyLaunch: the rocket never ignites before the count reaches zero.
  • Liveness, EventuallyLaunched: the rocket ignites, sooner or later.

Steps:

  1. Run ./tla check 09-05-ex3. What does TLC say about an Init that is FALSE?
  2. Fill gaps g1 to g10 in Launch.tla. Spec carries the assumption your liveness property needs; Finished says where the system may rest.
  3. Fill gap g1 in Launch.cfg (the safety cfg, deadlock checking on) and in Launch.live.cfg (the liveness cfg), with Start = 3 and MaxHolds = 2.
  4. Run the check until it passes with full coverage and both witnesses. The course adds six checks in MCLaunch.tla. MCLaunch.safety.cfg and MCLaunch.live.cfg run your two properties on your own Spec, whatever your cfgs name; the safety one also checks that a held count is above zero, that no more than MaxHolds holds are called, and that the clock does not move while the count is held. Those two are what grade your properties: your own cfgs would pass even if they named no check at all. MCLaunch.endless.cfg runs your countdown with unlimited holds, MCLaunch.scrub.cfg one that reaches zero and rests on the pad without igniting, and MCLaunch.early.cfg one with ignition at any count; your properties must fail there. MCLaunch.noignite.cfg has no igniter and no rest, so the count reaches zero and stops; your Finished must leave that a deadlock.
  5. Read the MCLaunch.endless.cfg trace. Why does it loop rather than stop, and which conjunct of your own Hold rules that loop out of Spec?

All eight cfgs must meet their goals: "goal met: pass" for Launch.cfg, Launch.live.cfg, MCLaunch.safety.cfg and MCLaunch.live.cfg, "goal met: property EventuallyLaunched" for MCLaunch.endless.cfg and MCLaunch.scrub.cfg, "goal met: violation NoEarlyLaunch" for MCLaunch.early.cfg, and "goal met: deadlock" for MCLaunch.noignite.cfg. If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Hints: HINTS.md, under "09-05-ex3 hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/05-ex3-launch-countdown/; open it after you have a passing answer of your own.

Done when: ./tla check 09-05-ex3 prints "goal met: pass".

./tla check 09-05-ex3

Repo chapters/09-eventually/exercises/05-ex3-launch-countdown/

Hints

Hint 1

Start with TypeOK, Init and Tick, and a cfg with SPECIFICATION Spec, the constants and INVARIANT TypeOK. Run it before you write anything else.

Hint 2

Hold changes two variables: it sets the hold flag and counts the hold. Finished rests only after ignition. Spec needs the assumption from lesson 09-04, and EventuallyLaunched also needs the bound on holds to be true.

Hint 3 (nearly the answer)

NoEarlyLaunch is an implication from launched to a fact about count. EventuallyLaunched needs one operator from 09-02's table, and it is about the launch, not the clock. The safety cfg names TypeOK and NoEarlyLaunch; the liveness cfg names the temporal property after PROPERTY.

09-mxMixed20 min · mastery path

09-mx · Two clerks, one tally

Tier: mixed. Time: about 20 minutes. Off the core clock: this is on the mastery path.

Two clerks each record one sale on a shared tally. Recording takes two steps: read the tally onto a scratch note, then write the note plus one back. When both clerks are done, the shift is over. The requirement: by the end of the shift, the tally shows both sales.

Tally.tla declares a variable lock that nothing uses yet.

  1. Run ./tla check 09-mx. The safety cfg passes. The liveness cfg fails. Read how the trace ends, and what the tally says there. The trace shows only the variables that change; ./tla trace --full shows every variable in every state.
  2. Before you change anything, write down in one sentence what went wrong between the two clerks, in the order the trace shows.
  3. Fill gaps g1 and g2 so that the two clerks cannot interleave the way the trace shows.
  4. Run the check. Both cfgs must pass, with every action firing and the witness reached.

Then ask: this requirement was written as liveness. Could you have written it as an invariant instead, and what would TLC's trace have looked like?

Both cfgs must meet the same goal, "goal met: pass": Tally.cfg and Tally.live.cfg. If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Hints: HINTS.md, under "09-mx hint 1" to "hint 3". The solution is in solutions/chapters/09-eventually/exercises/mx-two-clerks-one-tally/; open it after you have a passing answer of your own.

Done when: ./tla check 09-mx prints "goal met: pass".

./tla check 09-mx

Repo chapters/09-eventually/exercises/mx-two-clerks-one-tally/

Hints

Hint 1

Read the trace in order. Both clerks read before either writes. What does each write back? The default trace shows only the variables that change; ./tla trace --full shows the tally and both notes in every state.

Hint 2

This is the lost update from chapter 06. Reading and writing must happen without the other clerk in between, and the module already has a variable for that.

Hint 3 (nearly the answer)

In Read, wait until the lock is free and take it; in Write, give it back. Each gap replaces the UNCHANGED lock line with a guard and a new value, or with a new value alone.

06Explain it

Open NOTES.md and answer the three prompts under 09-05, 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 "09-05 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "With no bound on losses, the counterexample is a loop of sending and losing: no deadlock, no progress. That is not a client bug but a network that never lets anything through, so the requirement needs an assumption, which I wrote as a counter: the network loses at most MaxLosses copies. With it and the fairness in Spec, the request gets through, with every action firing and the witness reached. A real network that loses more often than that is outside what was checked."

A weak answer: "I bounded the losses so the check passes." It treats the bound as a trick to make TLC quiet, not as an assumption about the network that someone has to be able to defend.

Done?