Basic

Cancellation loses work

When a task is dropped, its progress is thrown away. In a language with no implicit cancellation this is a design decision, and the decision has a cost that is visible.

What you will see

One line. A timeout without a stamp is a number that changes with the network.

About 1 min of reading

Try It

Measure what a cancel throws away

1

Write the progress at cancel

printf 'bytes_done 1048576 unit 4194304\n' > /tmp/c.txt; awk '{printf "%.2f\n", 100*$1/$2}' /tmp/c.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
25.00. A quarter of a unit thrown away is the cost of this cancel.
2

Make the unit restartable

printf 'unit=restartable side_effects=0\n' > /tmp/u.txt; cat /tmp/u.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. Zero side effects is what makes a restart cheap.
3

Stamp the timeout used

printf 'timeout_ms 5000 date=2026-10-10\n' > /tmp/t.txt; cat /tmp/t.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. A timeout without a stamp is a number that changes with the network.

Data Flow

A timeout to a lost unit
1 The task is running a unit
↓
2 The timeout fires
↓
3 The future is dropped and progress is lost
↓
4 The unit is restarted from the beginning
Why this works
Why timeouts are the common cancel

A timeout drops the task. That is why every timeout in a system is a cancellation policy with a number.

The rule that makes cancellation safe

Idempotent units. A unit that can be repeated from the start is a unit that can be cancelled in the middle.

Examples

The work already done is gone

A future dropped at the tenth byte of a download has downloaded ten bytes that are not kept.

Why partial work is the hazard

A side effect that already happened cannot be undone. Cancellation is not a rollback.

The guard is a boundary

The unit of work is the thing that is safe to lose. Work smaller than a unit is work that cannot be cancelled halfway.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← The executor is the loop
Previous Lesson
The executor is the loop
Next Lesson
Compiling to a module
Compiling to a module →