Issue #1237: Racing tasks as a policy of async: async(race) instead of select, and the first that ends or the first that succeeds

Open and answer this issue on GitHub.

async, decision, future · opened 2026-11-04 · updated 2026-11-22 · 0 comments

The question

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.

How it works today

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:

  1. The first that succeeds. Asking three mirrors for the same file, a mirror that fails fast should not win. With select, the first arm to end wins, failure or not.
  2. Whether a failure counts as “finished” at all. Part II 12.4 says “the first branch to finish wins” and does not say what happens when the first branch to end fails. The intended reading is probably that the failure wins and arrives from the select, as failures arrive elsewhere; the text does not state it.

The idea

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.

Which task wins

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.

Open points for the decision

  1. What 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.
  2. The value of the block when the tasks have different types. Either the block has no value and each line carries out its own effect (as in the examples above), or its value is a sum of the tasks’ types.
  3. What becomes of select. Part II 12.4 is rewritten for async(race), with a rule for moving existing code.

Recommendation

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.

What a decision changes

No build work starts before the owner decides.


Generated by Claude Code

Back to Open Issues