Repository navigation
Description
Context
WaitGroup is a low-level lifetime barrier: cloned RAII handles represent outstanding work, and waiters observe when all handles are gone. It deliberately does not own tasks, collect results, propagate errors, or define cancellation and parent-child relationships.
Some use cases need a higher-level abstraction for a group of related asynchronous tasks. That should be designed as a separate primitive rather than changing WaitGroup. ForkJoin was an initial placeholder, not a proposed public name.
Ecosystem survey
- Rust's
tokio::task::JoinSet<T>owns runtime-spawned tasks, yields homogeneous results in completion order, and aborts remaining tasks when dropped. It is primarily controlled by one mutable collection owner. - Rust's
tokio_util::task::TaskTrackeris cloneable and supports registration without mutable access. Waiting requires the tracker to be both closed and empty; completed tasks release their storage immediately, results are not retained, and dropping the tracker does not abort tasks. - Go's
errgroup.Groupgroups goroutines belonging to one operation, waits for them, returns the first non-nil error, can cancel a derived context on failure, and optionally limits concurrency. - Java's
StructuredTaskScopemakes one owner responsible for a lexical task scope. It forks subtasks, joins them under an explicit policy, prevents new forks after joining, and closes by cancelling and waiting for unfinished subtasks. - Swift's
TaskGroupis a non-escaping structured scope for dynamically created child tasks. Children inherit parent context, results form an asynchronous sequence, and the scope cannot return while children remain. - Kotlin's
coroutineScopeestablishes a parent-child task tree: the parent waits for children, while failure and cancellation propagate through the scope.
These examples separate into at least three families: result-owning task collections (JoinSet), shareable lifecycle trackers (TaskTracker), and structured lexical scopes (StructuredTaskScope, Swift TaskGroup, Kotlin coroutineScope). Go's errgroup combines lifecycle tracking with failure policy.
Questions to answer
- Is the target a runtime-agnostic future tracker, or may it spawn and therefore depend on a runtime?
- Is registration shareable with child tasks, restricted to one owner, or lexically scoped so handles cannot escape?
- Does joining only signal quiescence, return one result at a time, collect all results, or reduce them through a policy?
- How are task errors and panics represented, and should the first failure cancel siblings?
- What does dropping the owner do: detach, cancel, abort, or synchronously/asynchronously wait?
- Is there an explicit
closetransition that prevents new top-level work? Can running children register nested work after close? - Is bounded concurrency part of this primitive or an orthogonal concern?
- Does the design require one join owner, or are multiple completion observers meaningful?
Naming
The name should follow the selected contract rather than lead it:
TaskGroupsuggests structured child ownership and possibly result collection.JoinSetsuggests an owned collection with per-task results.TaskTrackersuggests shared lifecycle observation without task ownership.TaskScopesuggests lexical structure and non-escaping children.
No name or API is proposed yet. The next step is to identify concrete Asyncband use cases and choose which semantic family, if any, belongs in a runtime-agnostic synchronization crate.
Activity
[-]Explore WaitGroup internals and a separate ForkJoin primitive[/-][+]Explore a higher-level task group primitive[/+]Colerar commentedon Sep 11, 2026
I just took a dive into this issue, and here are my thoughts. :D
For this issue, I currently lean toward a runtime-agnostic
TaskGroup, rather than a full lexical task scope.Why not a lexical task scope
As in the Moro README - why do moro spawns only run concurrently, not parallel, under Rust’s current
Futuremodel, it is difficult for a completely safe, general-purpose API to provide all three of the following:'staticlocal data;There are two main approaches to implementing a lexical task scope.
Have the scope poll child futures itself
Pros:
Cons:
Spawn children onto an executor
Pros:
Cons:
'static.For these reasons, I do not currently favor making a lexical scope. A lexical scope API would still be possible but their use cases are limited.
TaskGroup shape
TaskGroupwould not spawn tasks itself. Instead, it would wrap futures and let the caller choose the executor:Pros
close()distinguishes “currently empty” from “no more tasks will be added.”Cons
TaskGroupdoes not own the executor’s task handles.Task results
I do not think completion-only tracking and result-aware task groups need to be separate primitives. The same
TaskGroupcould support both:For completion-only use cases, tracked futures can simply return
().This means completion-only use cases may pay a small unnecessary result-delivery cost, but it keeps the API simpler and avoids substantial duplication. The implementation could be optimized later if benchmarks justify it.
Result policies
I favor an approach similar to
futures-concurrency:TaskGroupprovides completed outputs, while callers choose the desired policy.join_next()wait()join()try_join()race()race_ok()join_next()could be the foundational API, with the other operations built on top of it.Because
TaskGroupdoes not own executor task handles, determining the final result early is not the same as the group having finished. To preserve structured-concurrency semantics,try_join()andrace()should:Cancellation
Cancellation can remain the separate primitive proposed in #223, while
TaskGroupcomposes with it at the API level:The responsibilities remain separate and orthogonal.
Summary
runtime-agnostic
Registration is provided through a cloneable registrar, allowing child tasks to register additional work. A single owner manages results, closing, and cancellation; no lexical non-escape guarantee is provided.
The same
TaskGroupcan providewait(),join_next(), andjoin().join_next()should be the foundation, allowing callers to select or implement their own result policy.Application errors are represented by the future’s output type, such as
TaskGroup<Result<T, E>>, and callers decide whether the first error cancels siblings. BecauseTaskGroupdoes not own executor task handles, precise panic information may still depend on the executor.Dropping the owner requests cooperative cancellation, but cannot abort tasks or wait asynchronously. Callers that require complete cleanup must explicitly call
cancel()followed bywait().await.An explicit
close()transition is provided. To keep the first version simple, closing the group rejects all new registrations, including nested work initiated by running child tasks.Bounded concurrency remains orthogonal and can be provided by
Semaphoreor another higher-level primitive.There is a single owner that consumes task results. If multiple completion-only observers prove useful, they can be added later through a separate completion watch.
Feedback is very welcome! Going to sleep now, so I'll reply later. I’m planning to put together a PR this weekend.
tisonkun commentedon Sep 12, 2026
@Colerar Thanks for your feedback! Feel free to submit PR. I may find some time the next week to consider this issue or your upcoming PR.