---
title: "Race"
description: "Start every arm as a task, take the first to complete, and cancel the rest."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.atoll.workreef.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Race

> **This surface runs**
>
> `race` desugars to `spawn` on every arm, a select over their handles, a cancel
> of the losers, and an `await` of the winner — all of it lowered by
> `crates/atoll-codegen` and driven by the scheduler in `crates/atoll-runtime`,
> and covered by tests that place every arm and run the program.

`race` is a value expression. It starts every arm as a child task, waits for one
to complete, cancels the others, and evaluates to the winner's value as a `T?`.

```atoll
fn from_cache(key int) int {
    println("cache lookup ${key}")
    return key
}

fn from_origin(key int) int {
    println("origin lookup ${key}")
    return key + 1
}

fn lookup(key int) int {
    winner := race {
        from_cache(key)
        from_origin(key)
    }
    return winner.unwrap_or(key)
}
```

The `Option` is there because the last step is an `await`, and `await` answers
`T?`: `None` means the winning task was itself cancelled, and a cancelled body
never returned a value to hand back. The race cancels only the *losers*, so a
`None` here means somebody outside reached in and stopped the winner.

Arms are expressions in a brace block with no separators — one per line reads
best. Commas are accepted but add nothing:

```atoll
fn a() int { return 1 }
fn b() int { return 2 }

fn either() int {
    winner := race { a(), b() }
    return winner.unwrap_or(0)
}
```

Every arm starts before the wait begins, so their side effects overlap and must
be safe even for an arm that later loses.

## Result types

Arms with the same type unify into that type, and the race answers it under a
`?`. That is the case you want for public code.

```atoll
struct Response {
    status int
    body   string
}

fn fetch(host string) Response {
    println("fetching from ${host}")
    return Response { status: 200, body: "ok" }
}

fn fastest() Response {
    winner: Response? = race {
        fetch("primary")
        fetch("replica")
    }
    return winner.unwrap_or(Response { status: 504, body: "" })
}
```

Heterogeneous arms widen to an anonymous union under the same rules as any other
branching expression — here the arms unify to `int | string`, so the race
answers that union under a `?`:

```atoll
fn as_number() int { return 1 }
fn as_text() string { return "one" }

fn describe() string {
    winner := race {
        as_number()
        as_text()
    }
    match winner {
        Some(value) => {
            match value {
                int(n) => return "number ${n}"
                string(s) => return s
            }
        }
        None => return "cancelled"
    }
}
```

The arms are heads, not bindings with annotations: a union arm names the member
**type** and binds inside it, `int(n)` rather than `n: int`. A type-group arm
over an anonymous union is `ATOLL2010 non-exhaustive` whatever the members are —
see [Match](https://docs.atoll.workreef.dev/language/control-flow/match/#unions).

Prefer a single explicit arm type at an API boundary. A caller should not have to
match on which implementation strategy happened to win.

## Completion, not success

The current lowering is:

1. spawn every arm;
2. select the first task to complete;
3. cancel every losing task;
4. await and return the winner.

That is a **completion** race. If the first arm to finish fails, its failure
wins — even when another arm would have succeeded a moment later.

```atoll
error FetchError { Timeout, Refused, Cancelled }

fn primary() int ! FetchError {
    println("primary")
    return 1
}

fn replica() int ! FetchError {
    error Timeout
}

fn fetch() int ! FetchError {
    outcome := race {
        primary()
        replica()
    }
    match outcome {
        Some(result) => return result?
        None => error Cancelled
    }
}
```

`replica` returns immediately, so it is very likely to win, and the `?` in the
`Some` arm propagates `Timeout` while `primary` is cancelled mid-flight. Both
envelopes are open here: the `Option` says whether the winner reached an end at
all, the `Result` says how it ended.

"First **success**" is a different algorithm and is not what `race` does. Build
it explicitly: spawn the candidates yourself, `select` in a loop, keep the
failures, and stop when a success arrives or the candidates run out.

```atoll
import task

error FetchError { Timeout }

fn attempt(candidate int) int ! FetchError {
    if candidate % 2 == 0 { error Timeout }
    return candidate * 10
}

fn first_success(candidates []int) int {
    mut running: []task.Task[Result[int, FetchError]] = []
    for c in candidates {
        running.push(spawn { attempt(c) })
    }

    mut answer := -1
    mut failures := 0
    for Some(t) := running.pop() {
        match t.await() {
            Some(Ok(value)) => {
                if answer < 0 { answer = value }
            }
            Some(Err(err)) => failures = failures + 1
            None => failures = failures + 1
        }
    }
    println("failures: ${failures}")
    return answer
}
```

That version costs full fan-out but never mistakes the fastest failure for the
answer. A cancelled candidate lands in the `None` arm and counts with the
failures, because it produced no answer either — but it is a separate arm, since
it is not a failure anyone reported.

## Identifying the winner

The result is a value, not a label. If the caller needs to know *which* arm won,
put the identity in the value:

```atoll
enum Source {
    Cache
    Origin
}

struct Answer {
    source Source
    value  int
}

fn from_cache() Answer {
    return Answer { source: Source.Cache, value: 1 }
}

fn from_origin() Answer {
    return Answer { source: Source.Origin, value: 2 }
}

fn lookup() int {
    winner := race {
        from_cache()
        from_origin()
    }

    match winner {
        Some(answer) => {
            match answer.source {
                Source.Cache => println("served from cache")
                Source.Origin => println("served from origin")
            }
            return answer.value
        }
        None => return 0
    }
}
```

## Cancellation of losers

Losers are cancelled for you — this is the reason to reach for `race` instead of
[`select`](https://docs.atoll.workreef.dev/language/concurrency/select/), which leaves its sources alone.

Cancellation is cooperative, so a losing task stops at a scheduler or suspension
safepoint, not at an arbitrary instruction. Its owned locals still get cleanup
paths, and a `Drop` implementation still runs:

```atoll
struct Lease {
    id int
}

impl Drop for Lease {
    fn drop(&self) void {
        println("releasing lease ${self.id}")
    }
}

fn slow_path(id int) int {
    lease := Lease { id: id }
    println("slow path holding ${lease.id}")
    return lease.id
}

fn fast_path(id int) int {
    return id
}

fn lookup(id int) int {
    winner := race {
        fast_path(id)
        slow_path(id)
    }
    return winner.unwrap_or(id)
}
```

If `fast_path` wins, `slow_path` is cancelled and its `Lease` is still released.
A cancelled loser is never the value the race answers, so the `None` case here
is still only "the winner was cancelled from outside".

Avoid irreversible external side effects before the point where an arm is safe to
abandon, or make those operations idempotent. A cancelled arm may have already
sent a request, written a row, or charged a card.

## Empty races

`race {}` is accepted as a degenerate `void` expression with no runtime work.

```atoll
fn nothing() void {
    race {}
}
```

The checker records the race effect row before recognizing the empty case, so it
still carries `Spawn`, `Suspend`, and `Cancel`. It is also the one race that is
not an `Option`: with no arm there is no winner to await, so the type is plain
`void` rather than `void?`. Require at least one arm in real code.

## Effects

A non-empty race contributes all three concurrency effects:

- `Spawn`, because each arm becomes a child task;
- `Suspend`, because the caller waits for a winner;
- `Cancel`, because the losers are stopped.

## Ordering

When multiple arms are already complete at the readiness scan, an earlier handle
can win. `race` is not a randomness primitive and promises nothing about the
distribution between equally fast arms.

## Choosing between the three constructs

| You want... | Use |
| --- | --- |
| Every result | `spawn` each, then `await` each |
| One interchangeable result, losers stopped | `race` |
| One of several *different* sources, losers kept | `select` |

`race` fits hedged requests, primary/replica lookups, and any "these are two ways
to compute the same thing" pair. Once the arms mean different things, or once you
need the losers to survive, you want `select`.

Source: https://docs.atoll.workreef.dev/language/concurrency/race/index.mdx
