00-02

Chapter 00 · The bug your tests won't find

Every interleaving

  • 30 min core
  • Essentials track
  • Lab: Interleaving Explorer
  • Skills: counterexamples, evidence

00Recall

Two cards from the last lesson: what has to happen for the balance to come out wrong, and why sequential tests never see it. Answer each one in your head before you reveal it.

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-01-b from Predict the balance

Why can a suite of sequential tests never see this race?

Show answer

Each test runs one call to completion before starting the next, so two calls are never in flight at once, and the bad orders of steps never happen.

Skip recall Recall is never required.

01Commit

Each transfer takes three steps: Read, Debit, Credit. Its own steps always happen in that order, but the other transfer's steps can slot in anywhere between them. One complete run of both transfers takes six steps in some order. We call that order an interleaving: the two transfers' steps, shuffled together like two halves of a deck of cards, with each half keeping its own order.

Commit a

Each transfer takes three steps, Read, Debit, Credit, and the other transfer's steps can slot in between. Count every interleaving of the two transfers. What share of them end with a wrong balance in account a?

  1. Fewer than a quarter: a race is one unlucky order.
  2. At least a quarter, but no more than half.
  3. More than half, but not all.
  4. All of them: whenever the two transfers' steps mix, the balance goes wrong.

Answer More than half, but not all.

Commit b

Two interleavings are identical except that the two Credit steps at the end come in the opposite order. Do they end in different states?

  1. Yes: a different order of steps is a different run, so it ends in a different state.
  2. No: both end in the same state.

Answer No: both end in the same state.

02Lab

Lab · Interleaving ExplorerWhat you’re looking at: every interleaving of the two transfers, unfolded as a tree from the state TLC starts in. Expand every branch, count the leaves, then count the ones where balance is not 20.

TLC found no violation

1234567891011121314151617181920212223242526272829303132

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

Text view: every state and step
States and their successors
StateseenbalancepaidpcSteps (action → state)
initial 1<<0, 0>>1000<<"read", "read">>Read(1) → 2; Read(2) → 3
2<<100, 0>>1000<<"debit", "read">>Debit(1) → 4; Read(2) → 5
3<<0, 100>>1000<<"read", "debit">>Read(1) → 5; Debit(2) → 6
4<<100, 0>>700<<"credit", "read">>Credit(1) → 7; Read(2) → 8
5<<100, 100>>1000<<"debit", "debit">>Debit(1) → 9; Debit(2) → 10
6<<0, 100>>500<<"read", "credit">>Read(1) → 11; Credit(2) → 12
7<<100, 0>>7030<<"done", "read">>Read(2) → 13
8<<100, 70>>700<<"credit", "debit">>Credit(1) → 13; Debit(2) → 14
9<<100, 100>>700<<"credit", "debit">>Credit(1) → 15; Debit(2) → 16
10<<100, 100>>500<<"debit", "credit">>Debit(1) → 17; Credit(2) → 18
11<<50, 100>>500<<"debit", "credit">>Debit(1) → 19; Credit(2) → 20
12<<0, 100>>5050<<"read", "done">>Read(1) → 20
13<<100, 70>>7030<<"done", "debit">>Debit(2) → 21
14<<100, 70>>200<<"credit", "credit">>Credit(1) → 21; Credit(2) → 22
15<<100, 100>>7030<<"done", "debit">>Debit(2) → 23
16<<100, 100>>500<<"credit", "credit">>Credit(1) → 23; Credit(2) → 24
17<<100, 100>>700<<"credit", "credit">>Credit(1) → 25; Credit(2) → 26
18<<100, 100>>5050<<"debit", "done">>Debit(1) → 26
19<<50, 100>>200<<"credit", "credit">>Credit(1) → 27; Credit(2) → 28
20<<50, 100>>5050<<"debit", "done">>Debit(1) → 28
21<<100, 70>>2030<<"done", "credit">>Credit(2) → 29
22<<100, 70>>2050<<"credit", "done">>Credit(1) → 29
23<<100, 100>>5030<<"done", "credit">>Credit(2) → 30
24<<100, 100>>5050<<"credit", "done">>Credit(1) → 30
25<<100, 100>>7030<<"done", "credit">>Credit(2) → 31
26<<100, 100>>7050<<"credit", "done">>Credit(1) → 31
27<<50, 100>>2030<<"done", "credit">>Credit(2) → 32
28<<50, 100>>2050<<"credit", "done">>Credit(1) → 32
29<<100, 70>>2080<<"done", "done">>
30<<100, 100>>5080<<"done", "done">>
31<<100, 100>>7080<<"done", "done">>
32<<50, 100>>2080<<"done", "done">>

Expand the whole tree. Count the leaves. Then count the leaves where balance is not 20. Write both numbers down before you open the reveal.

Reveal for commit a

More than half the leaves are bad, but not all. The interleavings where one transfer's debit lands before the other transfer's read come out right, and there are some of those even among orders that mix the steps. In the others both reads come first, and one write erases the other. REASON derives both counts by hand, so you can check what you counted.

Reveal for commit b

Same state. The tree draws a state again every time another path reaches it: its leaves, one per interleaving, end in only 4 distinct states. TLC stores each state once, 32 for the whole model.

03Reason

If you picked "fewer than a quarter", you pictured a race as a needle in a haystack: one unlucky order out of millions. This race is not like that. Let's count, and be sure of the count by deriving it, not by trusting the picture.

How many interleavings are there? A run is six slots in a row. Three of the slots belong to T1 and three to T2, and within each transfer the order is fixed: Read, then Debit, then Credit. So a run is fixed the moment you pick which three of the six slots are T1's.

Write something before openinghow many ways are there to choose three slots out of six?

Twenty. The first of T1's slots can go in 6 places, the second in 5 and the third in 4, which gives 6 × 5 × 4 = 120. But the three slots are not ordered among themselves (T1 fills them in its own fixed order), and 3 × 2 × 1 = 6 orderings of the same three slots give the same choice. So 120 ÷ 6 = 20 interleavings.

How many are bad? From the last lesson: the balance is wrong exactly when both reads happen before either debit.

Write something before openingif both reads come before both debits, what do the first two slots hold?

The two reads. A debit cannot come first, because each transfer's debit needs its own read first, and by assumption no debit comes before the other read. A credit cannot come first either, since it needs its own debit. So the first two slots are T1's read and T2's read, in either order.

Now finish the count. The two reads can be in either order: 2 ways. The remaining four slots hold T1's debit and credit and T2's debit and credit, each pair in its fixed order: choosing which two of the four slots are T1's gives 4 × 3 ÷ 2 = 6 ways. That is 2 × 6 = 12 bad interleavings out of 20. Did you count 12 in the lab? If not, find the leaves you missed.

Then why is it rare in practice? Counting interleavings is not the same as measuring how often each one happens. A real machine does not pick an interleaving at random. Between a transfer's Read and its Debit there is only the funds check, and for the bug to show, the other request has to start its Read inside that gap. That holds whether the threads take turns on one core or run side by side on two. In terms of orders, the bad ones are common. In terms of time, the window is tiny.

So you get a bug that is in most of the interleavings and almost none of the test runs. TLC does not wait for the timing to be unlucky. It treats every interleaving as possible, because in principle every one is.

The three-step window. One transfer reads, the other reads, then either debits. Once both reads are in before any debit, the outcome is fixed, whichever debit comes first: the one that lands second erases the other. Everything after that, the second debit and the two credits, only carries out the damage. When you look at the trace TLC prints in lesson 00-03, look for two Reads followed by a Debit.

Interleavings, states and steps. Three words have come up, and they mean different things.

  • A state is a snapshot: a value for every variable. In Transfer.tla the variables are balance, paid, each transfer's next step pc, and the balance each transfer read, seen.
  • A step is a move from one state to the next. Every step here is one transfer doing one of its three things.
  • An interleaving is one order of all six steps. Running it from the start passes through a sequence of states, one more than the number of steps. Chapter 01 calls a run's sequence of states a behavior, and explains why it never really ends.

Different interleavings can pass through the same state. That is what commit b was about. If T1 and T2 have both finished their debits, the order of the two credits does not change where you end up. The tree in the lab is TLC's state graph unfolded, so a state reached by two paths appears twice in the tree.

04In your editor

The model is chapters/00-the-bug-your-tests-wont-find/specs/Transfer.tla, and the lab's configuration is Transfer.explore.cfg next to it. From the repository root:

shell
./tla check 00-02

You should see:

Output 00-the-bug-your-tests-wont-find/specs/Transfer.explore
00-the-bug-your-tests-wont-find/specs/Transfer.explore  Transfer.explore.cfg  (Start=100, Amt1=30, Amt2=50)
parse ok
model check: no violation found in 32 distinct states (43 generated, depth 7)
expected outcome met: pass
coverage: all 3 actions fired
witness LostUpdate reached (trace of 7 states)

The "witness" line says TLC reached a state where LostUpdate is true: both transfers are done and the balance is wrong. Lesson 00-03 explains how to read every line of this output.

05Practice

Warm-up: a worked example, then your turn. Decide good or bad for each order, using only the rule "both reads before either debit".

Worked example, order 1: T1 reads, T1 debits, T2 reads, T2 debits, T1 credits, T2 credits. Find the two reads. Between them sits T1's debit, so the reads are not both before a debit: good. Check with numbers: T1 reads 100 and writes 70, T2 reads 70 and writes 20. The credits mix with the debits, and it does not matter.

Your turn. For each order, find the two reads, decide, and give the final balance.

  • Order 2: T2 reads, T1 reads, T2 debits, T2 credits, T1 debits, T1 credits.
  • Order 3: T1 reads, T2 reads, T1 debits, T1 credits, T2 debits, T2 credits.
  • Order 4: T2 reads, T2 debits, T2 credits, T1 reads, T1 debits, T1 credits.

Then check two of them in the lab.

Write something before openinggood or bad, and the final balance of each.

Order 2 is bad: both reads came first, and T1 debits last, so the balance ends at 100 − 30 = 70. Order 3 is bad: T2 debits last and the balance ends at 100 − 50 = 50. Order 4 is good: one transfer ran start to finish before the other, which is what every sequential test does, and the balance ends at 20.

Core. In the lab, find a state that more than one path reaches, other than the start and the leaves. Write down two different paths into it and say which variables make it the same state.

Write something before openingone possible answer.

"T1 reads, T1 debits, T1 credits, T2 reads" and "T1 reads, T1 debits, T2 reads, T1 credits" end up identical: balance is 70, paid is 30, T1 is done, T2 is about to debit, and both seen values match. The order of T1's credit and T2's read does not show in any variable.

Stretch (optional). Add a third transfer, T3, that sends 10 from the same account. Without the lab, work out how many interleavings there are, and state the rule for when a transfer's debit is lost.

Write something before openingthe count and the rule.

Nine slots, three per transfer: choose T1's three slots, then T2's three from the six left, and T3 takes the rest. That is 84 × 20 = 1,680 interleavings. For the rule, start from the end. The final balance is the last write: its transfer's copy minus its own amount. That copy is what the latest write before its read left, or 100 if nothing had been written yet, and so on back. A debit survives exactly when it is on that chain. Being written over is not enough to lose it: if a third transfer read after the debit and writes later, it carries the debit forward.

06Explain it

Open NOTES.md and answer the three prompts under 00-02. For the first, explain in your own words why most interleavings are bad and yet the bug is rare in practice. Then compare with "00-02 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: "Most interleavings are bad because the only condition is that both reads come before either debit, and more than half of the orders meet it. The bug is still rare in real runs because the orders do not happen equally often: on one core or several, the other thread must read inside the short gap between this thread's read and write. TLC visited every reachable state of one model with two transfers and fixed amounts, which covers every interleaving of that model, because each interleaving passes only through reachable states. It did not say how likely any of them is."

A weak answer: "TLC tries every possibility, so it finds bugs that tests miss." It is true but gives no reason why tests miss this bug, and it leaves out the bounds of the model, which are what the claim depends on.

Done?