00-03

Chapter 00 · The bug your tests won't find

Your first TLC run

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

00Recall

Three cards from the first two lessons. One asks what has to happen for the balance to go wrong, one separates a state from an interleaving, and one asks why a common bug can still be rare.

00-01-a from Predict the balance

In transfer(), what must happen for the final balance to be wrong?

Show answer

Both transfers read the balance before either one writes it back. Then both compute from the same old value, and the second write erases the first.

00-02-a from Every interleaving

What is the difference between a state and an interleaving?

Show answer

A state is a snapshot of every variable's value. An interleaving is one order of all the steps of a run; running it passes through a sequence of states. Many interleavings can pass through the same state.

00-02-b from Every interleaving

Most interleavings of the two transfers are bad. Why is the bug still rare in real runs?

Show answer

Interleavings do not happen equally often. The bad ones need the other thread to reach its Read inside the short gap between this thread's Read and Debit, on one core or several, so they are common in the count and rare in time.

Skip recall Recall is never required.

01Commit

specs/Transfer.tla describes the two transfers in a language called TLA+. You do not need to read it yet. It has one claim written in it that we care about, called NoFreeMoney:

TLA+
NoFreeMoney is defined as balance + paid less than or equal to Start

In words: the money left in the account plus the money paid out never adds up to more than the 100 the account started with (Start = 100). A claim that must hold in every state the system can reach is called an invariant. You are about to ask TLC to check this one.

Commit a

TLC checks the invariant NoFreeMoney against Transfer.tla. What will it report?

  1. A violation of NoFreeMoney, with a trace that shows how.
  2. No violation. TLC runs one transfer, then the other, as the tests do.
  3. An error: two transfers write the same variable, and TLC cannot decide which one wins.

Answer A violation of NoFreeMoney, with a trace that shows how.

Commit b

TLC prints the shortest run that breaks NoFreeMoney as a numbered list of states. How many states will that list have, counting the first one?

  1. 4
  2. 5
  3. 6
  4. 7

Answer 6

02Lab

This is your first run on your own machine. You need Java 21 and the pinned TLA+ tools; START.md in the repository root covers both, including a Codespaces option that needs nothing installed. From the repository root, check your setup first:

shell
./tla doctor

It checks the Java version, the checksum of the pinned TLA+ tools, and runs TLC once on a tiny model. It also checks TLC's telemetry opt-out and prints a note if it is missing; START.md explains the note and how to opt out. The last line should be "Ready.". If it is not, the line above it says what is missing. Now run the check for this lesson:

shell
./tla check chapters/00-the-bug-your-tests-wont-find/specs/Transfer.cfg

Every course command starts with ./tla. Here it ran TLC on specs/Transfer.cfg, the configuration that asks for NoFreeMoney. The output looks like this:

Expected output 00-the-bug-your-tests-wont-find/specs/Transfer
00-the-bug-your-tests-wont-find/specs/Transfer  Transfer.cfg  (Start=100, Amt1=30, Amt2=50)
parse ok
model check: invariant NoFreeMoney violated; trace has 6 states (5 steps); 26 distinct states
expected outcome met: violation NoFreeMoney
trace (changed variables only; ./tla trace --full shows all):
  1  Init    balance = 100, paid = 0, pc = <<"read", "read">>, seen = <<0, 0>>
  2  Read    pc: <<"read", "read">> -> <<"debit", "read">>, seen: <<0, 0>> -> <<100, 0>>
  3  Read    pc: <<"debit", "read">> -> <<"debit", "debit">>, seen: <<100, 0>> -> <<100, 100>>
  4  Debit   balance: 100 -> 50, pc: <<"debit", "debit">> -> <<"debit", "credit">>
  5  Debit   balance: 50 -> 70, pc: <<"debit", "credit">> -> <<"credit", "credit">>
  6  Credit  paid: 0 -> 50, pc: <<"credit", "credit">> -> <<"credit", "done">>

Reveal for commit a

TLC reports a violation of NoFreeMoney. Below that line, the course's own check says "expected outcome met": this configuration was written to fail, and it failed in the expected way. In this course a red run is often the goal. A check you have never seen fail tells you nothing.

Reveal for commit b

The trace has 6 states, which means 5 steps. TLC numbers the states starting from one, and the first state is where the run starts, before any step.

03Reason

If you expected no violation, you pictured TLC as a very fast test runner. It is not a test runner. A test runs your code along one path. TLC takes a description of everything the system may do and visits the states that description can reach, one by one, checking the invariant in each. The spec says that at any moment either transfer may take its next step, so TLC takes both options, every time. When it finds a state where the invariant is false, it stops.

Now read the output from the top. Each line answers one question.

  • 00-the-bug-your-tests-wont-find/specs/Transfer Transfer.cfg (Start=100, …): which model, which configuration file, and the constants it used.
  • parse ok: SANY, the TLA+ front end, parsed the module and found no syntax or semantic errors (such as undefined names).
  • model check: …: TLC's verdict. The invariant failed. The line also says how long the trace is and how many distinct states TLC had found when it stopped.
  • expected outcome met: …: the course's verdict. Every configuration file says on its first line what should happen (@expect), and this one expected exactly this violation.
  • The numbered lines: the trace, one state per line. For each state you see the step that led to it and only the variables that changed.

Here is the trace again as a list:

  1. State 1 Initial state balance = 100 paid = 0 pc = <<"read", "read">> seen = <<0, 0>>
  2. State 2 Read pc <<"read", "read">> becomes <<"debit", "read">> seen <<0, 0>> becomes <<100, 0>>
  3. State 3 Read pc <<"debit", "read">> becomes <<"debit", "debit">> seen <<100, 0>> becomes <<100, 100>>
  4. State 4 Debit balance 100 becomes 50 pc <<"debit", "debit">> becomes <<"debit", "credit">>
  5. State 5 Debit balance 50 becomes 70 pc <<"debit", "credit">> becomes <<"credit", "credit">>
  6. State 6 Credit paid 0 becomes 50 pc <<"credit", "credit">> becomes <<"credit", "done">>

The first state is the start: the balance is 100, nothing has been paid, both transfers are about to read, and neither has seen anything. pc holds each transfer's next step. It is a function from Transfers, the set {1, 2}, to each transfer's next step, and TLC prints such a function as a tuple, in the order T1, T2. So <<"debit", "read">> means "T1 will debit next, T2 will read next".

Write something before openingthe trace prints "Read" twice and "Debit" twice. How can you tell which transfer took each step?

Look at which slot of pc changed. In the second state, one slot moves from "read" to "debit": if it is the first slot, T1 read; if it is the second, T2 did. In the third state the other slot moves. The trace prints the step's name without saying which transfer took it, and the variables tell you.

Now find the three-step window from lesson 00-02: one transfer reads, the other reads, and only then does either debit. Look at the top of the trace. Both transfers read 100. Then one of them writes its result, and later the other writes over it, computed from its own old copy of 100. Use pc to tell which was which.

Write something before openingonly one transfer has finished in the last state, and the other has not paid its payee yet. Why does the trace stop here instead of at the end?

Because an invariant must hold in every state, not only at the end. In the last state, add the balance to what has been paid: it comes to more than 100. TLC checks the invariant in each new state it finds and stops at the first state where it is false. Nothing after that state can repair it.

States and steps. Think of a fence: the posts are states and the rails between them are steps. A fence always has one more post than it has rails, because the first post stands before any rail. TLC numbers the states starting from one, so a trace of 6 states has 5 steps. When you describe a trace, say both, and say which one you mean.

Why this trace and not another. Many runs break NoFreeMoney. TLC printed one of the shortest.

Why so few states. TLC reports 26 distinct states here. The configuration the lab uses, Transfer.explore.cfg, does not check NoFreeMoney, so nothing stops it early, and it finds 32. A run that stops at a violation tells you about the states it saw before stopping, and nothing about the rest.

One line of the spec is the whole bug. Here is the Debit step:

TLA+
Debit(t) is defined as
    and pc[t] = "debit"
    and balanceprime = seen[t] - Amount[t]
    and pcprime = [pc EXCEPT ![t] = "credit"]
    and UNCHANGED tuple of paid, seen end tuple

Read /\ as "and", and read balance' (with the tick, said "balance prime") as "the value of balance in the next state". The third line says the new balance is the transfer's own copy, seen[t], minus its amount, which is what the Python code does when it writes balance - amount using its local variable. The last two lines move t's pc on and keep everything else as it was. Chapter 02 explains exactly what these lines mean. For now, notice that the model says the same thing as the code, and TLC found the same race.

04In your editor

The spec and its configurations are in chapters/00-the-bug-your-tests-wont-find/specs/. This command runs every configuration the lesson uses, including the Core practice's, so save it for after the Core:

shell
./tla check 00-03

Two more things to try. ./tla trace --full prints the most recent trace with every variable in every state, not only the changed ones. And the raw TLC output is always saved: in your terminal, the last line of each block, raw log: …, names the file under .tla-out/. (The copy on this page leaves that line out.) In that log, a violation and a real error carry the same mark: the @!@!@STARTMSG line that opens each one ends in :1, TLC's severity for an error-class message. The verdict line of ./tla is what tells them apart.

Open specs/Transfer.explore.cfg too. Its last line turns off TLC's deadlock check, with a comment saying why: when both transfers are done, no step is possible, and here that is the end of the run, not a bug. That switch also hides real deadlocks, such as a transfer stuck waiting for a lock, so a cfg that uses it must say why. Chapter 06 returns to deadlock.

05Practice

Warm-up: a worked example, then your turn. Worked example, for the first Debit in the trace. The second slot of pc changed, from "debit" to "credit", so T2 took this step. Its copy seen says 100, its amount is 50, so it writes 100 − 50 = 50.

Your turn, for the second Debit: T__ debits. Its copy says ____, its amount is ____, so it writes ____. Which earlier write disappears?

Write something before openingyour answers.

T1 debits: the first slot of pc changed. Its copy says 100, because it read before anyone wrote. Its amount is 30, so it writes 70. T2's earlier write of 50 disappears: the balance no longer shows that T2's 50 ever left.

Core: equal amounts. The configuration specs/Transfer.equal.cfg checks the same model with Amt1 = 30 and Amt2 = 30. Predict before you run it: does TLC still find a violation? Is the trace longer, shorter or the same length? Can you still tell from the final balance whose debit was lost? Then run it:

shell
./tla check chapters/00-the-bug-your-tests-wont-find/specs/Transfer.equal.cfg
Write something before openingwhat changed, and why?

TLC still finds a violation, but the trace has 7 states instead of 6. With equal amounts, the lost write leaves 70 in the account no matter who wrote last, and 70 + 30 is not more than 100. The invariant only breaks once both payees have been paid. Look at the second Debit in the trace: balance is not listed at all, because writing 70 over 70 changes nothing you can see. You can no longer tell whose debit was lost, which is why Transfer.tla says the amounts differ on purpose.

06Explain it

Open NOTES.md and answer the three prompts under 00-03. For the first, explain what the trace shows, step by step, in plain words. Then compare with "00-03 check your explanation" in HINTS.md.

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

A good answer: "The trace shows both transfers reading 100 before either writes. Then one writes its result and the other writes over it, computed from its own old copy, and one payee is paid. At that point the account plus the payments come to more than 100, so NoFreeMoney fails, and TLC stops there; the other payee is never reached. In the model, a Debit that subtracted from the current balance would make the read and the write one indivisible step, and the bad state would not be reachable. The Python cannot get that by rewriting the line: ledger.set_balance(src, ledger.balance(src) - amount) is still a read and a later write, so the code needs a lock or a conditional update. TLC checked the model, not transfer.py, and only for two transfers with these amounts."

A weak answer: "TLC found a violation of NoFreeMoney in the trace." It repeats the verdict without walking through the steps, and it says nothing about what TLC did not check.

Done?