Open and answer this issue on GitHub.
async, decision, future · opened 2026-11-04 · updated 2026-11-22 · 0 comments
Should racing tasks become a policy of async (async(race) { … }) instead of a separate form select, and should a race be able to say which task wins: the first that ends, or the first that succeeds?
This is not needed for the current focus. It is filed so the reasoning is not lost, and labelled future.
select (Part II 12.4) starts every arm at once, keeps the result of the first that finishes and cancels the others:
select {
msg = inbox.recv() => handle(msg)
_ = shutdown.wait() => return
}
ADR-async-5 D4 already says each form states only its group’s policy, and lists select as the group whose policy is “the first wins”. So select is in effect an async group with a different policy, written as its own form.
Two things select cannot say today:
select, the first arm to end wins, failure or not.select, as failures arrive elsewhere; the text does not state it.Make the race a policy of async:
async(race) {
handle(inbox.recv())
shutdown.wait()
}
This also covers the case that led here: stopping a whole group of tasks on a shutdown signal. An option such as cancel_on: shutdown would add a second way to cancel a task beside its handle, which D14 rules out. A race against the signal needs no new word:
async(race) {
async { serve_clients(); flush_logs(); heartbeat() }
shutdown.wait()
}
When the signal ends first, the inner group is cancelled, the same cancellation a losing select arm gets today.
Both kinds are common, and one cannot be built from the other: a failure cannot be made “not to end”, and “the first that succeeds” needs its own answer for when every task fails.
| Option | Who wins | Example |
|---|---|---|
async(race) |
the first task that ends, a failure too | racing a signal or a deadline |
async(race: task::Race::FirstOk) |
the first task that succeeds | the same request to several mirrors |
The option is written qualified, as every std option’s case is (order: par::Order::Any, on_timeout: task::OnTimeout::Cancel).
Why “the first that ends” should be the default. It swallows no failure. With “the first that succeeds” as the default, the shutdown example above would be dangerous:
async(race) {
serve_clients() // crashes
shutdown.wait()
}
With “the first that ends”, the crash arrives at once. With “the first that succeeds”, it would be passed over and the program would wait silently for the signal.
FirstOk gives when every task fails. The last failure, the first failure, or all failures together as one error type. Recommendation: all together, because the other two lose information.select. Part II 12.4 is rewritten for async(race), with a rule for moving existing code.async(race) replaces select, with “the first that ends” as the default and task::Race::FirstOk as the second policy; no cancel_on:. Until it is decided, Part II 12.4 should say that a failing arm counts as finished.
async; select goes, with a migration rule.std: task::Race, and the error for “every task failed”.async { }.No build work starts before the owner decides.
Generated by Claude Code