Repository navigation
Implement task Group for managing multiple spawned tasks - #152
Conversation
Group allows spawning multiple tasks and waiting for or canceling them as a unit: - spawn/spawnBlocking: Add tasks to the group - wait: Wait for all tasks, cancel remaining on parent cancellation - cancel: Cancel all tasks efficiently (trigger all cancellations first, then wait for completion)
WalkthroughA new Group management abstraction is introduced to handle task lifecycle and coordination. The system includes group spawning, waiting, and cancellation semantics. Integration occurs via a new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/core/group.zig (3)
22-29: Consider the edge case where spawned tasks complete immediately.Both methods set
group_node.groupand push to the list after spawning. While the spawn likely holds a reference preventing release, verify there's no race if a task completes extremely quickly (before group linkage is established).Also applies to: 31-38
64-93: The cancel loop handles concurrent spawns, but should it?Line 67's infinite loop accounts for tasks added during cancellation. However, spawning into a group that's being canceled could lead to unexpected behavior. Consider whether Group should track a "closing" state that rejects new spawns once cancel() is called.
96-104: Document the userdata field purpose.Line 103 defines
userdata: usize = undefinedwithout explaining its intended use. Add a comment describing when and how this field should be used, or remove it if it's not needed yet.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/core/awaitable.zig(2 hunks)src/core/group.zig(1 hunks)src/zio.zig(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.zig
📄 CodeRabbit inference engine (CLAUDE.md)
Use
zig envto get the path to the Zig standard library when needed
Files:
src/zio.zigsrc/core/awaitable.zigsrc/core/group.zig
🧠 Learnings (7)
📚 Learning: 2025-11-30T10:31:40.819Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/aio/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:40.819Z
Learning: Applies to src/lib/aio/**/*.zig : Don't depend on `std.posix` in Zig code; create custom wrappers instead as Zig 0.16 will remove it
Applied to files:
src/zio.zig
📚 Learning: 2025-11-30T10:31:51.367Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/coro/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:51.367Z
Learning: Applies to src/lib/coro/**/*.zig : Avoid dependencies on `std.posix` as Zig 0.16 is removing it - create custom wrappers instead
Applied to files:
src/zio.zigsrc/core/awaitable.zigsrc/core/group.zig
📚 Learning: 2025-11-30T10:31:40.819Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/aio/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:40.819Z
Learning: Applies to src/lib/aio/**/build.zig.zon : Release process step 2: Update version in build.zig.zon
Applied to files:
src/zio.zig
📚 Learning: 2025-11-30T10:31:51.367Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/coro/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:51.367Z
Learning: Applies to src/lib/coro/**/*.zig : Do not assume knowledge of std APIs - verify with current Zig documentation instead
Applied to files:
src/zio.zigsrc/core/awaitable.zig
📚 Learning: 2025-11-30T10:31:31.198Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:31.198Z
Learning: Applies to build.zig.zon : Update version in build.zig.zon as part of release process
Applied to files:
src/zio.zig
📚 Learning: 2025-11-30T10:31:40.819Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/aio/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:40.819Z
Learning: Applies to src/lib/aio/**/*.zig : Don't assume you know how Zig std APIs work; verify current behavior instead of relying on outdated information
Applied to files:
src/zio.zig
📚 Learning: 2025-10-16T14:33:19.718Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 48
File: src/core/ConcurrentAwaitableList.zig:254-269
Timestamp: 2025-10-16T14:33:19.718Z
Learning: In the zio repository's `src/core/ConcurrentAwaitableList.zig` and `src/core/Awaitable.zig`, the `in_list` field should remain debug-only (conditional compilation with `builtin.mode == .Debug`). Double-removal detection in `ConcurrentAwaitableList.remove()` uses pointer-based checks (comparing `prev`/`next` against `head`/`tail`) rather than relying on a regular `in_list` field.
Applied to files:
src/core/awaitable.zig
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: test (windows-latest)
🔇 Additional comments (5)
src/core/awaitable.zig (1)
9-9: LGTM - clean integration of group membership.The GroupNode import and field addition integrate smoothly with the Awaitable structure. Default initialization correctly represents standalone awaitables (group is null).
Also applies to: 58-59
src/zio.zig (1)
15-16: LGTM - straightforward re-export.Group is now properly exposed in the public API alongside other core abstractions.
src/core/group.zig (3)
40-62: LGTM - correct cancellation propagation.The wait loop properly handles cancellation by re-attaching the current node, entering shield mode, and canceling remaining tasks. Shield prevents cleanup from being interrupted, which is the right approach.
110-214: LGTM - comprehensive test coverage.The three test cases validate core functionality (spawn/wait), multiple task coordination, and cancellation semantics. The cancellation test properly verifies that tasks are canceled when the waiter is interrupted.
18-20: This type-punning is intentionally safe and properly designed.The cast of
&self.tokento*CompactWaitQueue(GroupNode)is valid because CompactWaitQueue is explicitly designed as an 8-byte queue (documented atsrc/utils/wait_queue.zig:13) with a singlehead: std.atomic.Value(usize)field. Sinceself.tokenis?*anyopaque(also 8 bytes on 64-bit systems), the memory layouts are compatible by design, not by accident.This is an intentional intrusive data structure pattern where the wait queue state is stored within the
tokenfield itself. Unlike other uses of CompactWaitQueue (e.g., inMutex.zigandCondition.zig), Group uses type-punning to overlay the queue on an existing pointer field, which is a valid optimization technique.While the cast is safe, consider adding a
comptimeassertion to document and verify this assumption, similar to the size check insrc/sync/Notify.zig.
Summary
Groupstruct for managing multiple spawned tasks as a unitspawn/spawnBlockingto add tasks to the groupwaitto wait for all tasks, canceling remaining if parent is canceledcancelto cancel all tasks efficiently (triggers cancellations first, then waits)Changes
src/core/group.zigwith Group and GroupNode typesAwaitableto includegroup_nodefield for group membershipGroupfromzio.zigTest plan
Group: spawn- basic spawn and waitGroup: wait for multiple tasks- verifies all tasks completeGroup: cancellation while waiting- verifies proper cleanup on cancellation