06-05

Chapter 06 · Concurrency is interleaving

Deadlock as TLC reports it

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

00Recall

One card from 06-04 on attempt 2's deadlock, one from chapter 03 on what CHECK_DEADLOCK FALSE changes, and one from chapter 00 on the question to ask of any piece of evidence.

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.

00-04-a from How this course works

What is the one question this course asks of any piece of evidence?

Show answer

Could this check have failed? A check that cannot fail on the bug you care about tells you nothing about it.

Skip recall Recall is never required.

01Commit

Every model in lessons 06-01 to 06-04 ends Next with a step called Finished:

TLA+chapters/06-concurrency-is-interleaving/specs/CoarseBankTransfer.tla
Finished is defined as
    and for all t in Transfers : pc[t] = "done"
    and UNCHANGED vars

It changes nothing. Time to find out what it is for. specs/MCCoarseBankTransfer.tla takes CoarseBankTransfer, which passed in 06-03, and removes Finished from Next. Nothing else changes.

Commit a

CoarseBankTransfer passed. Without the Finished step, and nothing else changed, what does TLC report?

  1. No violation, as before: a step that changes nothing cannot matter.
  2. A deadlock, in the state where both transfers are done.
  3. A deadlock: the two transfers block each other halfway.

Answer A deadlock, in the state where both transfers are done.

specs/LockedTransfer.tla fixes 06-03's lost updates with a lock per account. A transfer takes its two locks one per step, payer first, moves the money in one step while it holds both, and releases both locks in one step. Its cfg also checks LocksHeld: a transfer that moves money holds both locks. It keeps Finished.

Commit b

TLC checks LockedTransfer, deadlock checking on. What does it report?

  1. No error: the locks stop lost updates, and Finished marks the end.
  2. A deadlock: each transfer holds one lock and waits for the other's.
  3. A deadlock in the finished state, the same report as commit a.

Answer A deadlock: each transfer holds one lock and waits for the other's.

02Lab

shell
./tla check chapters/06-concurrency-is-interleaving/specs/MCCoarseBankTransfer.cfg
./tla check chapters/06-concurrency-is-interleaving/specs/LockedTransfer.cfg

Before you open the reveals, look at the last state of each trace. Where is each process, and does it have a step left that it is waiting to take?

Expected output 06-concurrency-is-interleaving/specs/MCCoarseBankTransfer
06-concurrency-is-interleaving/specs/MCCoarseBankTransfer  MCCoarseBankTransfer.cfg  (Start=10, Amt1=3, Amt2=4)
parse ok
model check: deadlock reached; trace has 5 states (4 steps); 9 distinct states
expected outcome met: deadlock
trace (changed variables only; ./tla trace --full shows all):
  1  Init    acct = [a |-> 10, b |-> 10], pc = <<"debit", "debit">>
  2  Debit   acct: [a |-> 10, b |-> 10] -> [a |-> 7, b |-> 10], pc: <<"debit", "debit">> -> <<"credit", "debit">>
  3  Credit  acct: [a |-> 7, b |-> 10] -> [a |-> 7, b |-> 13], pc: <<"credit", "debit">> -> <<"done", "debit">>
  4  Debit   acct: [a |-> 7, b |-> 13] -> [a |-> 7, b |-> 9], pc: <<"done", "debit">> -> <<"done", "credit">>
  5  Credit  acct: [a |-> 7, b |-> 9] -> [a |-> 11, b |-> 9], pc: <<"done", "credit">> -> <<"done", "done">>

Reveal for commit a

A deadlock, after 5 states. The last state has both transfers at "done" and the books balanced. Nothing went wrong; the run ended.

Expected output 06-concurrency-is-interleaving/specs/LockedTransfer
06-concurrency-is-interleaving/specs/LockedTransfer  LockedTransfer.cfg  (Start=10, Amt1=3, Amt2=4)
parse ok
model check: deadlock reached; trace has 3 states (2 steps); 7 distinct states
expected outcome met: deadlock
trace (changed variables only; ./tla trace --full shows all):
  1  Init       acct = [a |-> 10, b |-> 10], lock = [a |-> 0, b |-> 0], pc = <<"lock1", "lock1">>
  2  LockFirst  lock: [a |-> 0, b |-> 0] -> [a |-> 1, b |-> 0], pc: <<"lock1", "lock1">> -> <<"lock2", "lock1">>
  3  LockFirst  lock: [a |-> 1, b |-> 0] -> [a |-> 1, b |-> 2], pc: <<"lock2", "lock1">> -> <<"lock2", "lock2">>

Reveal for commit b

A deadlock, after 3 states. The transfer paying from "a" holds the lock on "a" and waits for "b"; the other holds "b" and waits for "a". Neither can ever move.

03Reason

If you picked "no violation" for commit a, you reasoned about the formula, and the formula agrees with you. TLC's report is about something else.

Write out what a spec says:

TLA+chapters/06-concurrency-is-interleaving/specs/MCCoarseBankTransfer.tla
EndlessSpec is defined as Init and always[EndlessNext]_vars

[EndlessNext]_vars allows two kinds of step: an EndlessNext step, or a step that leaves vars unchanged, a stuttering step. Stuttering is allowed in every state. So in the logic, no behavior ever gets stuck: when the transfers are done, the behavior goes on stuttering for ever, which is what chapter 01 said termination is.

So TLC's deadlock report is a question, not a verdict: "the system can stop here; did you mean it to?" Lesson 03-05 met the question once. Here it comes in both of its forms. In commit a the answer is yes: done is done. In commit b the answer is no: two transfers with work left are frozen. TLC printed the same word for both.

Read a deadlock trace from the last state up. For each process, write where its pc is and what its next step waits for:

  1. State 1 Initial state acct = [a |-> 10, b |-> 10] lock = [a |-> 0, b |-> 0] pc = <<"lock1", "lock1">>
  2. State 2 LockFirst lock [a |-> 0, b |-> 0] becomes [a |-> 1, b |-> 0] pc <<"lock1", "lock1">> becomes <<"lock2", "lock1">>
  3. State 3 LockFirst lock [a |-> 1, b |-> 0] becomes [a |-> 1, b |-> 2] pc <<"lock2", "lock1">> becomes <<"lock2", "lock2">>

The transfer paying from "a" waits for "b", which the other transfer holds; that one waits for "a". A circle of waiting is a real deadlock. A state where every process is finished is an ending.

Write something before openingthe circle needs each transfer to hold one lock while it waits for another. Name two ways to break that.

Take both locks in one step, so that nobody ever holds one while waiting for the other. That is a coarser step, and 06-03 says what it costs: you must name the mechanism that really takes two locks at once. Or keep one lock per step and make every transfer take them in the same order. Then a transfer only ever waits for a lock later in the order than every lock it holds. Follow a chain of waiting: each lock in it comes later than the one before, so the chain can never come back to where it started. Lock ordering is the standard discipline.

Why have a deadlock check at all, if the logic has no such idea? Many of the systems you will model, such as servers and workers, are not meant to stop, so a stuck state in them is a bug. A check that costs nothing and catches lock circles, missing branches and forgotten wake-ups is worth having on by default, even though it asks you a question every time a model is meant to end.

You have three ways to answer the report, and they are not equal:

AnswerWhat TLC still reports
Fix the causeeverything
Add Finished, guarded by "everyone is done"every stuck state except the intended end
CHECK_DEADLOCK FALSEno stuck state at all
Write something before openingFinished and CHECK_DEADLOCK FALSE both silence the report in commit a. Why does the course always prefer Finished?

Because Finished says which state is the proper end, and only that one. CHECK_DEADLOCK FALSE says "no state is ever a problem", which also hides commit b's circle. One is a sentence about your system; the other is a sentence about TLC.

Here is how bad the switch can look. A teammate saw commit a's report on an early version, added CHECK_DEADLOCK FALSE, and kept it when the locks arrived:

shell
./tla check chapters/06-concurrency-is-interleaving/specs/LockedTransfer.masked.cfg
Output 06-concurrency-is-interleaving/specs/LockedTransfer.masked
06-concurrency-is-interleaving/specs/LockedTransfer.masked  LockedTransfer.masked.cfg  (Start=10, Amt1=3, Amt2=4)
parse ok
model check: no violation found in 17 distinct states (20 generated, depth 9)
expected outcome met: pass
coverage: all 5 actions fired
witness AllDone reached (trace of 9 states)

No violation, every action fired, the witness AllDone was reached, in 17 distinct states. Every green light chapter 03 taught you to ask for is on, and the model still freezes whenever each transfer takes its first lock before either takes its second. The default cfg's trace above shows how little that takes: 3 states, one first lock per transfer, and nothing else. The witness was reached by the runs that did not jam. The same move is easy to accept from a coding agent: one that "fixes" a deadlock report by adding CHECK_DEADLOCK FALSE has made a check pass by editing the check.

Write something before openingwhich of chapter 03's three questions about a pass would have caught this?

The third: if deadlock checking is off, does the comment give a reason about the system? "TLC reports a deadlock when the transfers are finished" was true once. With Finished in the spec, the finished state is no longer a deadlock, so the switch can only hide something else.

When you meet PlusCal next chapter, look at its translation for a step it adds on its own. Its job is the job Finished does here.

04In your editor

The models are in chapters/06-concurrency-is-interleaving/specs/. Check this lesson's exercises with:

shell
./tla check 06-05

The rule this course follows, from here to the last chapter: deadlock checking stays on. When a spec's processes are meant to stop, it says so with a guarded Finished step. A cfg that turns the check off needs a comment naming the system's reason, and the real deadlock that the switch would also hide. The one exception is a deliberate safety-only run, like 06-04's FlagMutex.safety.cfg, whose reason is about TLC: its comment names the deadlock it hides and the cfg that still reports it.

When your own model reports a deadlock, answer TLC's question in writing before you change anything. Either "this is the intended end", and a guarded Finished step now says so in the spec, or the name of the real bug you are about to fix. If you cannot write either sentence, you do not understand the stuck state yet. Read its trace from the last state up until you can.

05Practice

Warm-up. The task board has lost its Finished step. Write it so that it marks the proper end and nothing else.

Core. Make LockedTransfer deadlock-free by changing the order in which transfers take their locks.

Mixed (off the core clock). The task board's rule, restated as a property of steps.

06-05-ex1Warm-up5 min

06-05-ex1 · A proper end

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

TaskBoard.tla is the running task board with its Finished step missing. The cfg checks only TypeOK, and keeps deadlock checking on.

  1. Run ./tla check 06-05-ex1. Read the last state of the TaskBoard.cfg trace: is anyone stuck, or is the work done?
  2. Fill gap g1: once every worker has gone home, the system may take steps that change nothing. Only then.
  3. Run the check. TaskBoard.cfg should pass, with every action firing and the board cleared.

The check also runs MCTaskBoard.cfg, the course's own test of your Finished. In it, one worker never gives up: once the board is full, it stands at "pick" forever while the other worker goes home. That is a real stuck state, and TLC must still report it as a deadlock with your Finished in the spec. Its goal is "deadlock", on purpose. Leave the two MC files as they are.

Hints: HINTS.md, under "06-05-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/06-concurrency-is-interleaving/exercises/05-ex1-a-proper-end/; open it after you have an answer of your own.

Both cfgs must meet their goals: "goal met: pass" for TaskBoard.cfg, and "goal met: deadlock" for MCTaskBoard.cfg.

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

./tla check 06-05-ex1

Repo chapters/06-concurrency-is-interleaving/exercises/05-ex1-a-proper-end/

Hints

Hint 1

Read the last state of the failing trace: both workers are at "done", and the board is cleared. Nothing is wrong except that Next has no step there.

Hint 2

Finished needs a guard and a frame. The guard says when this step is allowed: only when the run is really over. The frame says what it changes: nothing.

Hint 3 (nearly the answer)

Two conjuncts: every worker's pc is "done" (use \A w \in Workers), and UNCHANGED vars. A guard that needs only some worker at home, or no guard, hides the stubborn worker in MCTaskBoard.cfg.

06-05-ex2Core8 min

06-05-ex2 · Same order

Tier: core. Time: about 8 minutes.

LockedTransfer.tla is the lesson's model: two transfers in opposite directions, each taking two account locks one at a time, payer first. TLC reports a deadlock, and this time it is a real one.

  1. Run ./tla check 06-05-ex2. Read the deadlock trace: who holds which lock, and who waits for what?
  2. Fill gap g1: First(t) and Second(t) say which lock transfer t takes first and second. Change them so that every transfer takes the two locks in the same order, whichever way its money goes.
  3. Run the check until it passes, with every action firing and both witnesses reached. Deadlock checking stays on: do not touch the cfg.
  4. In one sentence: why can two transfers that lock in the same order never each hold what the other one needs?

Hints: HINTS.md, under "06-05-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/06-concurrency-is-interleaving/exercises/05-ex2-same-order/; open it after you have an answer of your own.

Done when: ./tla check 06-05-ex2 prints "goal met: pass".

./tla check 06-05-ex2

Repo chapters/06-concurrency-is-interleaving/exercises/05-ex2-same-order/

Hints

Hint 1

In the deadlock, each transfer holds the lock the other wants. Ask what would have to be true for one transfer to wait for a lock without already holding another that someone wants.

Hint 2

What must a transfer that waits for its first lock already hold? Find an order of taking locks under which the answer is "nothing", for every transfer.

Hint 3 (nearly the answer)

Neither definition needs to look at From[t] or To[t].

06-mxMixed15 min · mastery path

06-mx · Names stick

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

The team lead states the board's rule differently from ClaimsHold: "Once a name is on a task, no step ever changes it." That is not a claim about one state. It is a claim about every step: a pair of states, before and after.

  1. Run ./tla check 06-mx. All three cfgs pass, because the starter's OwnerSticks is TRUE.
  2. Fill gap g1 in TaskBoard.tla: OwnerSticks says that in every step, a task that has an owner keeps the same owner. Write it so that PROPERTY OwnerSticks, already in the cfg, checks it on every step of every behavior, including the steps that do not touch the board.
  3. Run the check. Read the trace, and find the step that breaks the rule. Compare it with the trace that breaks ClaimsHold.
  4. In NOTES.md, one sentence: which rule would you rather put in a design review, and why?

The check also runs MCTaskBoard.cfg, the course's own test of your property: one pick at a time, so no claim can land on a task that already has a name. Names are still written there, so a property that forbids every change to the board fails. It also runs MCTaskBoardReuse.cfg, a board that hands out finished tasks again: a name gets overwritten, but never while its owner is working, so ClaimsHold holds there. Your OwnerSticks must report a violation there; a restated ClaimsHold would not. Leave the MC files as they are.

Hints: HINTS.md, under "06-mx hint 1" to "hint 3". The solution is in solutions/chapters/06-concurrency-is-interleaving/exercises/mx-names-stick/; open it after you have an answer of your own.

All three cfgs must meet their goals: "goal met: property OwnerSticks" for TaskBoard.cfg and MCTaskBoardReuse.cfg, and "goal met: pass" for MCTaskBoard.cfg.

Done when: ./tla check 06-mx prints "goal met: property OwnerSticks".

./tla check 06-mx

Repo chapters/06-concurrency-is-interleaving/exercises/mx-names-stick/

Hints

Hint 1

This is a claim about steps, not states: it compares the board before a step with the board after it. owner is before, owner' is after.

Hint 2

Inside, say: for every task, if it has an owner now, its owner after the step is the same. Then put that inside [][ … ]_owner, read "always, every step either does this or leaves owner unchanged", so steps that do not touch the board pass.

Hint 3 (nearly the answer)

The inner formula is \A t \in Tasks : followed by an implication whose left side tests owner[t] against None and whose right side compares owner'[t] with owner[t]. Wrap it in [] and the square brackets with subscript _owner.

06Explain it

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

After you writeCheck your explanation

A good answer. "Both transfers now lock "a" before "b". A deadlock needs a circle of waiting, each transfer holding a lock the other wants; with one global order, a transfer waiting for "a" holds nothing, so no circle can form. TLC found no deadlock and no violation, every action fired, both transfers finished, and Contended shows real waiting. It assumes every piece of code that takes these locks uses the same order. TLC did not check timeouts, livelock, or more accounts."

A weak answer. "I changed the lock order and the deadlock went away, so it is deadlock-free."

Why it is weak. "Deadlock-free" claims more than one finite model shows, and the answer never says why one order prevents the circle.

Done?