Skip to content

Cancellation

Request cooperative shutdown, understand when it takes effect, and get cleanup ordering right.

cancel() asks a child task to stop. It returns void, it is idempotent, and it is a no-op on a task that already completed.

fn watch_changes() int {
    println("watching")
    return 0
}

fn should_stop() bool {
    return true
}

fn supervise() void {
    task := spawn { watch_changes() }

    if should_stop() {
        task.cancel()
    }
}

Cancellation is a request, not a join. The call does not wait for the child to finish unwinding and does not produce the child’s T. Lifetime and result handling are separate decisions.

When you do need to wait — “stop it, and do not carry on until it has actually stopped” — pair the request with join(), which suspends until the task is over and answers true if it completed and false if it was cancelled. It takes no value, so it costs no result delivery.

fn watch() int {
    println("watching")
    return 0
}

fn stop_and_wait() bool {
    task := spawn { watch() }
    task.cancel()
    return task.join()
}

The timeline

parent calls cancel()
        ↓
the request is recorded for the child's whole subtree
        ↓
each running descendant reaches a cancellation-aware boundary
        ↓
its continuation stops and its owned frame unwinds
        ↓
defers, Drop bodies, capture releases, descendant cleanup run
        ↓
the task state becomes reclaimable

Every one of those steps happens after your cancel() returned. That gap is exactly why cancellation cannot serve as a lock, a revocation, or a database rollback: by the time it takes effect, the child may already have done the thing you wanted to prevent.

Delivery is cooperative

A child is not interrupted at an arbitrary machine instruction. Where the request lands depends on what the child is doing when it arrives:

  • Parked at a suspension point — waiting on a host call, a join, a stream, a sleep. It unwinds at once, and no call of its runs again.
  • Running, or paused mid-computation — preempted at a loop safepoint, stopped in a debugger, or cancelling itself or an ancestor. It unwinds at its next suspending call. That call’s body runs once; if it would wait, the wait is dropped at once, so nothing is left parked.

For a preempted task the second case is indistinguishable from the request arriving a moment later, since where preemption lands is not something a program can rely on.

That is what preserves compiler-managed cleanup: captured managed values and frame locals are dropped along the cancellation path instead of being abandoned.

struct Conn {
    fd int
}

impl Drop for Conn {
    fn drop(&self) void {
        println("closing fd ${self.fd}")
    }
}

fn connect(addr string) Conn {
    return Conn { fd: addr.len() }
}

fn serve(conn Conn) int {
    println("serving on ${conn.fd}")
    return conn.fd
}

fn run() void {
    task := spawn {
        conn := connect("127.0.0.1:8080")
        serve(conn)
    }

    task.cancel()
}

Conn.drop runs on the cancellation path just as it would on the normal return path. Cleanup is synchronous: Drop.drop is @nosuspend, so a drop body that may suspend is refused (ATOLL1071).

defer covers the case where the cleanup is a statement rather than a type’s responsibility:

fn open_slot() int {
    println("open")
    return 7
}

fn close_slot(slot int) void {
    println("close ${slot}")
}

fn work(slot int) int {
    return slot
}

fn run() void {
    task := spawn {
        slot := open_slot()
        defer close_slot(slot)
        work(slot)
    }

    task.cancel()
}

The defer owns release for the child’s path. Parent code should not separately close a child-owned handle unless the API explicitly shares that ownership — otherwise you get a double release the moment cancellation and normal completion race.

Cleanup that belongs only to a failed path is an errdefer. A cancel runs one that binds no error, and the cancel arm of one with arms; no error type gains a variant for it.

A handle that dies cancels its task

A Task[T] handle is the only thing that can wait for a task or stop it, so letting one go is a decision. It cancels:

fn slow() int {
    println("working")
    return 1
}

fn give_up_early() int {
    task := spawn { slow() }
    return 0
}

fn main() int {
    return give_up_early()
}

The return 0 drops task, and dropping it cancels what it named. That is the structured default and it is the one that cannot go wrong silently: the alternative leaves a child running that nothing can reach, nothing can cancel, and that still counts against the run finishing — so a child that never ends wedges the whole program with no way for the program to say otherwise.

Fire-and-forget is a choice, and it is spelled:

fn background() int {
    println("later")
    return 1
}

fn launch() int {
    task := spawn { background() }
    task.detach()
    return 1
}

fn main() int {
    return launch()
}

detach() gives up the handle’s claim: the task runs on, stops counting against its parent’s completion, and dropping the handle no longer stops it. It is still cancelled when the run itself ends — detaching opts out of structure, not out of the process.

Descendants

Tasks form a parent/child tree, and cancellation walks it. Cancelling a task also stops its subtree, even when the intermediate handles were discarded.

fn leaf(n int) int {
    println("leaf ${n}")
    return n
}

fn run() void {
    outer := spawn {
        spawn { leaf(1) }
        spawn { leaf(2) }
        leaf(0)
    }

    outer.cancel()
}

The two inner spawns have no handles a parent could reach, but they are still in the tree, so outer.cancel() reaches them. An explicit child cancellation and an ancestor cancellation converge on the same subtree model; do not write code that depends on observing which ancestor requested the stop.

That includes the task asking. A handle is an owned value and can be moved — into a stream, into a spawn’s arguments — so a task can come to hold its own handle or an ancestor’s. Cancelling it, or dropping it, cancels a subtree the task is in: the task is running, so it unwinds at its next suspending call.

Race cancels; select does not

race cancels the losing arms automatically — that is the reason to use it.

fn fast() int { return 1 }
fn slow() int { println("slow") return 2 }

fn first() int {
    winner := race {
        fast()
        slow()
    }
    return winner.unwrap_or(0)
}

Because the losers are cancelled, race answers T? rather than T — the same shape await() has, for the same reason. In practice the winner is the arm that did complete, so None only appears when the whole race was cancelled from outside.

select consumes the winning source and leaves everything else running and owned by you. Track which arm won, then cancel or await only the sources you still hold. The winning handle is already consumed and must not be touched again.

fn fetch_left() int { return 1 }
fn fetch_right() int { return 2 }
fn use_value(v int) void { println("got ${v}") }

fn pick() void {
    left := spawn { fetch_left() }
    right := spawn { fetch_right() }
    mut left_won := false

    select {
        v := left => {
            left_won = true
            use_value(v.unwrap_or(0))
        }
        v := right => use_value(v.unwrap_or(0))
    }

    if left_won {
        right.cancel()
    } else {
        left.cancel()
    }
}

An arm binding is T?, not T — the arm materializes its winner through await, so a source that was cancelled arrives as None like anywhere else.

Shutting down a stream producer

For a producer feeding a Stream[T], closing and cancelling do different jobs, and which you want depends on where the producer is parked.

import task

fn produce(out task.Stream[int], count int) void {
    mut i := 0
    for i < count {
        if !out.send(i) {
            return
        }
        i = i + 1
    }
    out.close()
}

fn consumer_stopped() bool {
    return true
}

fn run() void {
    events := task.Stream.new[int](2)
    producer := spawn { produce(events, 1000) }

    if consumer_stopped() {
        events.close()
        producer.cancel()
    }
}

close() wakes a producer parked on backpressure: send returns false and the loop exits through the producer’s own return, which is the cleanest possible stop. It wakes a parked consumer the same way, and there recv answers None. cancel() covers the case where the child is parked on something else entirely — a host call, a nested join. Doing both is belt and braces, and it is cheap because cancel() on a finished task does nothing.

The ordering matters. Closing first gives the producer a chance to stop through its normal path; cancelling first forces an unwind from wherever it happens to be.

Cancellation is not an error channel

Cancellation arrives as the None of await()’s T?, not as a variant injected into T. The two are stacked, never merged: await() answers Some(result) for a task that ran to its end — whatever that result says — and None only for one that never got there.

error TaskError {
    Cancelled
    Backend
}

fn work(n int) int ! TaskError {
    if n < 0 { error Backend }
    return n
}

fn run(n int) int {
    task := spawn { work(n) }
    return match task.await() {
        Some(result) => result.unwrap_or(-1)
        None => -1
    }
}

The Some arm holds the Result[int, TaskError] the child produced, and the error inside it is answered on its own terms; the None arm is the task that was cancelled and therefore produced nothing at all.

That Cancelled variant, meanwhile, is an ordinary application error the code above never produces — the runtime does not raise it. Define one only when task lifetime genuinely belongs to your domain contract, otherwise the type invites callers to handle a case that never occurs.

Cleanup rules

Cancellation can race with normal completion and with an explicit close. Three rules follow:

Make cleanup idempotent. Normal completion, an early error, an explicit cancel, and parent shutdown may all converge on the same resource boundary.

struct Session {
    id     int
    closed bool
}

impl Session {
    fn release(&mut self) void {
        if self.closed {
            return
        }
        self.closed = true
        println("released session ${self.id}")
    }
}

fn open(id int) Session {
    return Session { id: id, closed: false }
}

fn run(id int) void {
    task := spawn {
        mut session := open(id)
        defer session.release()
        println("using session ${session.id}")
    }

    task.cancel()
}

Never put commit logic only in cleanup. Cancellation cleanup should release or roll back owned state. Successful publication belongs on the normal completion path, where you know the work actually finished.

Do not let cleanup depend on suspension. Drop bodies and defers run synchronously, so a “flush to the network before closing” step cannot live there. Flush on the normal path and treat cleanup as a local release.

Effects

Task.cancel() contributes Cancel. race contributes Spawn, Suspend, and Cancel.

select also picks up a conservative Cancel from semantic lowering even though it cancels nothing at runtime. That is an analysis over-approximation, not a promise of loser cancellation — see Effects.

A composed example

A worker that can be stopped from two directions: an explicit shutdown signal, or the supervisor cancelling it outright.

import task

struct Job {
    id     int
    weight int
}

struct Outcome {
    completed int
    weight    int
}

fn process(job Job) int {
    println("processing job ${job.id}")
    return job.weight
}

fn worker(queue task.Stream[Job]) Outcome {
    mut completed := 0
    mut weight := 0
    for Some(job) := queue.recv() {
        weight = weight + process(job)
        completed = completed + 1
    }
    return Outcome { completed: completed, weight: weight }
}

fn enqueue(queue task.Stream[Job], jobs int) void {
    mut i := 1
    for i <= jobs {
        if !queue.send(Job { id: i, weight: i * 2 }) {
            return
        }
        i = i + 1
    }
    queue.close()
}

fn supervise(jobs int, abort bool) Outcome {
    queue := task.Stream.new[Job](4)
    feeder := spawn { enqueue(queue, jobs) }
    runner := spawn { worker(queue) }

    if abort {
        // Close first so the feeder stops through its own `send` check
        // and the worker's `recv` answers `None`, then cancel whatever is
        // still parked elsewhere.
        queue.close()
        feeder.cancel()
        runner.cancel()
        return Outcome { completed: 0, weight: 0 }
    }

    feeder.join()
    return runner.await().unwrap_or(Outcome { completed: 0, weight: 0 })
}

The feeder’s value is nothing anyone wants, only its ending, so it is join()ed rather than awaited. The runner’s value is the point, so it is await()ed — and because that answers Outcome?, the cancelled case has to be spelled out even on the path where nothing cancels it.

Both children get an outcome on both paths: joined on the normal path, closed then cancelled on the abort path. No handle is left without a decision.

Type to search…

↑↓ navigate↵ selectEsc close