LEARN VOID / CONCURRENCY
Task groups & cancellation
Give related work a lifetime.
Complete program for the explicit async host adapter. The result below is the returned integer.
async func line_count(pages: i32) -> i32:
return pages * 40
async func entry(chapter_pages: i32) -> i32:
var total_lines = 0
with tasks.group() as chapters:
var chapter = line_count(chapter_pages)
var appendix = line_count(1)
total_lines = await chapter + await appendix
return total_linesGive chapter work one scope
The group owns the two line_count tasks created inside it. Each uses a fixed forty-lines-per-page model, so the chapter and one-page appendix have independent integer results. No shared counter is mutated.
Keep the result outside the group
Declare total_lines outside the group so it is available afterward. The parent explicitly awaits both results; normal scope exit also joins any remaining children. Input 5 gives 200 + 40 = 240. The group alias is lexical, not a first-class collection or a jobs.spawn API.
Understand early exits
Early exits from nested scopes request cooperative cancellation of unfinished work and join it before leaving. Dropping a handle never detaches a task. This example shows successful completion through the explicit async host adapter; it does not demonstrate cancellation recovery or change the ordinary CLI adapter.
What completion releases
After a task’s owning resume returns, or its worker completion is safely acquired, the runtime releases the completed execution frame’s GC root. Objects reachable only from that frame can then be collected. A caller can still await the retained result.
Retaining a task keeps its result available for repeated awaits. Once a handle, task or group is no longer observable and its work is quiescent, the Runtime can retire its records. Keeping results or growing lists yourself still consumes memory.