Home / Learn / 15 Concurrency and parallelism
15.3 What the result can depend on: failures and order
Page 4 of 9
15.3.1 What it means for a result to depend on something
A result depends on something when changing that thing can change the result. The total of an order depends on its lines: change a quantity, and the total changes. A well-made program's result depends on its inputs, and on the answers its services give, and on nothing else.
With concurrent tasks, there is one more candidate: timing. Which task finishes first can change from run to run, with the load of the computer or the speed of the network. In Una, the result of a block never depends on it. This section states the guarantee, the three rules that give it, and what timing can still change.
15.3.2 Why shared changes go wrong: races
Picture two clerks who keep one tally on a whiteboard. Each reads the number, adds the boxes they counted, and writes the new number. If both read 10 at the same moment, one adds 3 and writes 13, the other adds 5 and writes 15, and the 3 boxes are lost. Had one clerk waited for the other, the board would read 18. This is a race: two tasks change the same value, and the result depends on which one gets there first. It happens only on some runs, which is what makes a race so hard to find.
In many languages, tasks share values that both may change, and a programmer must add locks or other care to keep them from racing. In Una, tasks never share a value that changes, so a race cannot be written.
15.3.3 The guarantee: the result of a run one after another
The result of a together block, and of a together for, is the result of running its tasks one after another, in the order they are written, and stopping at the first one that fails. You can always predict the result by reading the tasks in order, as if together were not there. Three rules give that guarantee.
15.3.4 Rule 1: tasks share nothing that changes
A task captures the values of the names around it that it reads: it gets their values as they are when the block starts, as a lambda does (chapter 10). It cannot change a name declared outside it. Here stock_lines counts the SKUs it was asked about in an outer let edit name:
--- a/src/stock.una
+++ b/src/stock.una
@@ -31,10 +31,13 @@
uses @south: Warehouse[on_hand]
fails StockError
=
- together for sku in skus
+ let edit asked = 0
+ let lines = together for sku in skus
+ asked = asked + 1
let units = total(sku)
if error pass
"{sku}: {units} units"
+ lines.append(item: "{asked} SKUs asked")
pub main() -> Unit
uses @console: Console[print][U2023] Error: `asked` is declared outside this `together` task, which captures its value and cannot change it, so it cannot be assigned here; return the new value from the task instead
╭─[ src/stock.una:36:5 ]
│
36 │ asked = asked + 1
│ ──┬──
│ ╰──── `asked` is declared outside this `together` task, which captures its value and cannot change it, so it cannot be assigned here; return the new value from the task instead
│
│ Note: run `una explain U2023` for an explanation
────╯The marks point at asked, the name being assigned. The three tasks run at the same time, so asked = asked + 1 would be the whiteboard of the two clerks, and the count could come out 1, 2 or 3. The message gives the repair: return the new value from the task. Here, each task already returns its line, and the number of SKUs asked is the length of the list:
--- a/src/stock.una
+++ b/src/stock.una
@@ -31,10 +31,11 @@
uses @south: Warehouse[on_hand]
fails StockError
=
- together for sku in skus
+ let lines = together for sku in skus
let units = total(sku)
if error pass
"{sku}: {units} units"
+ lines.append(item: "{lines.len} SKUs asked")
pub main() -> Unit
uses @console: Console[print]LAMP-01: 6 units
DESK-02: 3 units
CHAIR-09: 0 units
3 SKUs askedPassing an outer name to a function that changes it for its caller (edit, section 16.2) or takes it over (take, section 16.3) is U2023 too. A task's own names, made in its body, are its own: it may declare them let edit and change them, as any block may.
15.3.5 Rule 2: the first failure in order wins
When one task fails, the block fails too, and the failure it gives is the failure that a run one after another would give: the failure of the first task, in the order written, that fails.
- The tasks after it are canceled. A task written after a failing one is canceled if it is still running: it is stopped, and its result is dropped, since the block will fail anyway. One that has already ended keeps what it did, and its result is dropped too. A task that has not started yet never starts.
- The tasks before it run on. A task written before the failing one keeps running to its end, since a run one after another would have run it first. It may fail in turn, and then its failure is the one that wins.
- Canceling waits for a safe point. A canceled task stops at its next call, operation or loop turn. An operation already in progress, such as a question a warehouse is answering, completes, and what it already did stays done.
- The block still waits. It fails only after every task has ended, failed or stopped.
This step asks two warehouses with total, first with the south one offline, then with both offline:
module failures
--- Why a warehouse gave no count.
kind StockError = | Offline(site: Text)
--- A warehouse's stock service: the units of a SKU on its shelves.
capability Warehouse
op on_hand(sku: Text) -> Int
fails StockError
--- A warehouse whose stock is held in a map: none of a SKU it does not stock.
handler shelves(stock: Map[Text, Int]) for Warehouse
op on_hand(sku) = stock.get(key: sku).or_else(default: 0)
--- A warehouse that does not answer.
handler offline(site: Text) for Warehouse
op on_hand(sku) = fail Offline(site)
--- The units of `sku` in both warehouses, both asked at the same time.
total(sku: Text) -> Int
uses @north: Warehouse[on_hand]
uses @south: Warehouse[on_hand]
fails StockError
=
together
let in_north = @north.on_hand(sku)
if error pass
let in_south = @south.on_hand(sku)
if error pass
in_north + in_south
pub main() -> Unit
uses @console: Console[print]
=
let north = empty_map().with(key: "LAMP-01", value: 4)
let south_down = attempt total("LAMP-01") with @north = shelves(north), @south = offline("south")
@console.print("south offline: {south_down}")
let both_down = attempt total("LAMP-01") with @north = offline("north"), @south = offline("south")
@console.print("both offline: {both_down}")
example "the north warehouse is asked even though the south one fails"
given @north = shelves(empty_map().with(key: "LAMP-01", value: 4))
given @south = offline("south")
do total("LAMP-01")
expect fails Offline
expect performed @north.on_handuna run . prints:
south offline: Err(Offline("south"))
both offline: Err(Offline("north"))With both warehouses offline, both tasks fail, perhaps the south one first. The block still fails with Offline("north"), the failure of the first branch, as a run one after another would. The example checks the second point of the list: when the south warehouse fails, the north one, the branch before it, was still asked. una test . prints:
example failures "the north warehouse is asked even though the south one fails" ... ok
1 example: 1 passed, 0 failedUnder una test, which runs the tasks one after another, a task after a failing one never starts, so an example could state that its warehouse was not asked, and pass every time. But that is the interpreter's schedule, not a promise of the language: in an executable, that task may have asked its warehouse before it was canceled. So expect performed and expect not performed belong on the tasks before a failing one, and on blocks that do not fail (chapter 14).
15.3.6 Rule 3: effects are recorded in task order
What a program does to the world outside it, such as asking a service or printing a line, is an effect (chapter 12). Una keeps a record of the operations a run performs, the trace that expect performed reads (section 14.3). In the trace, the operations of a block's tasks are listed task by task, in the order of the tasks, each task's operations in its own order, whatever order they really happened in. The trace of a canceled task ends where it stopped, with a mark that it was canceled.
What the record cannot fix is the moment each effect reaches the outside world. Here each task prints a line:
module announce
--- Prints a line for each SKU from its own task, and gives back the length of each SKU.
announce(skus: List[Text]) -> List[Int]
uses @console: Console[print]
=
together for sku in skus
@console.print("checking {sku}")
sku.len
pub main() -> Unit
uses @console: Console[print]
=
let lengths = announce(["LAMP-01", "RUG-07", "DESK-02"])
@console.print("lengths: {lengths}")una run . prints:
checking LAMP-01
checking RUG-07
checking DESK-02
lengths: [7, 6, 7]una run gives the lines in task order every time, for the reason of The interpreter and an executable. But tasks that run at the same time may print in any order: a program built into an executable may print checking RUG-07 first. Each task's own lines keep their order, and the result, lengths: [7, 6, 7], is always the same. When the order of the lines matters, print after the block, from the results, as every other program of this chapter does.
15.3.7 The interpreter and an executable
una run and una test run your program in Una's interpreter, which follows the program step by step. It runs the tasks of a block one after another, in their order: that is one of the ways the tasks may run, and it is the one the guarantee describes, so the interpreter is the reference. Its runs are the same every time.
una build turns a program into an executable, a file the computer runs directly (section 18.5). There, the tasks run on a small pool of threads, by default one for each core of the processor. A thread is a line of work that the computer runs at the same time as others, on another core when it has one. Each task gets its own copy of the values it reads when it starts, and its result is copied back when it ends. The tasks may really run at the same time, but nothing promises it: an executable starts a block's tasks a group at a time, so a very long list does not run all at once, and how many run together is the implementation's choice. The executable gives the same result, the same failure and the same exit status as the interpreter, as long as the run stays within its limits. Each task's copies count toward the run's memory limit, so an executable can stop with U6003 where una run does not (section 16.4). Within the limits, what may differ is only what timing decides: the order in which the lines of different tasks appear, and how far a canceled task got.
15.3.8 What Una leaves out
In edition 0.2, which this book uses, together and together for are the whole of concurrency. There are no task values to keep and wait for later, no race between tasks to take the first answer and no timeouts: edition 0.4, a preview, adds these three (chapter 19). In no edition is there a task that runs on after its block, or a channel for tasks to send each other messages. async and await, which some languages use for waiting on a service, are not part of Una: una check answers U0103, "not supported; use together" (Writing await (U0103)). A task that never ends keeps its block from ending: Una does not check that a task ends.
15.3.9 Code whose result does not depend on timing
Which to use:
| You want… | Use |
|---|---|
| A count or a total over the tasks | return a value from each task, then combine the list after the block |
| One service failure to fail everything | if error pass in the task: the first failure in order wins |
| To go on without a service that fails | if error use in the task, or attempt (chapter 7) |
| Lines printed in a fixed order | print after the block, from its results |
| An example about the services a block asked | expect performed on tasks before the failing one, or on a block that does not fail |
15.3.10 Check yourself: the first failure
close_sites closes each site at once, and a site with no count fails:
module closing
kind CloseError = | NotCounted(site: Text)
--- The closing count of a site, from the counts that came in. A site with no count fails.
closing_count(site: Text, counted: Map[Text, Int]) -> Int
fails CloseError
=
counted.get(key: site)
if none fail NotCounted(site: site)
--- The closing count of every site, one task per site.
close_sites(sites: List[Text], counted: Map[Text, Int]) -> List[Int]
fails CloseError
=
together for site in sites
closing_count(site, counted: counted)
if error pass
pub main() -> Unit
uses @console: Console[print]
=
let counted = empty_map().with(key: "north", value: 12)
let outcome = attempt close_sites(["north", "south", "east"], counted: counted)
@console.print("{outcome}")Only north has a count, so the tasks for south and east both fail. What does una run . print, every time?
Show the answer
Err(NotCounted("south"))The block fails with the failure of the first task, in list order, that fails: south, as a run one after another would. In an executable the east task may fail first, and the answer is the same.