Skip to content

Implement task Group for managing multiple spawned tasks - #152

Merged
lalinsky merged 1 commit into
mainfrom
task-group
Dec 6, 2025
Merged

lalinsky merged 1 commit into
mainfrom
task-group

Conversation

@lalinsky

@lalinsky lalinsky commented Dec 4, 2025

Copy link
Copy Markdown
Owner

Summary

  • Adds Group struct for managing multiple spawned tasks as a unit
  • spawn/spawnBlocking to add tasks to the group
  • wait to wait for all tasks, canceling remaining if parent is canceled
  • cancel to cancel all tasks efficiently (triggers cancellations first, then waits)

Changes

  • New src/core/group.zig with Group and GroupNode types
  • Modified Awaitable to include group_node field for group membership
  • Exported Group from zio.zig

Test plan

  • Group: spawn - basic spawn and wait
  • Group: wait for multiple tasks - verifies all tasks complete
  • Group: cancellation while waiting - verifies proper cleanup on cancellation

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)
@coderabbitai

coderabbitai Bot commented Dec 4, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

A 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 group_node field in Awaitable and API re-export through the main module.

Changes

Cohort / File(s) Change Summary
New Group Management System
src/core/group.zig
New module introducing Group struct with spawn/spawnBlocking/wait/cancel methods and GroupNode for tracking pending tasks. Includes lifecycle control, wait list management, and cancellation flow. Covers test cases for group spawn, multi-task waiting, and cancellation behavior.
Integration & API Export
src/core/awaitable.zig, src/zio.zig
Added group_node field (type GroupNode, default .{}) to Awaitable struct. Re-exported Group in public API module.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Cancel logic with shielded semantics: The cancel() method's interaction with task draining and completion waiting warrants careful inspection
  • Group-Awaitable coupling: Verify the group_node field initialization and usage across the spawn/wait flow
  • Wait list state management: Check CompactWaitQueue handling in edge cases (concurrent access, empty list, partial failures)
  • Test coverage alignment: Confirm test cases adequately cover the cancellation re-queueing behavior and multi-task scenarios

Poem

🎯 Tasks now gather 'round a group,
With spawn and wait in structured loop,
When cancel calls, they bow their heads,
No orphaned work in tangled threads.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly matches the main change: implementing a Group struct for task management with spawn, wait, and cancel operations.
Description check ✅ Passed The description clearly outlines the Group functionality, lists concrete changes to three files, and documents test coverage that aligns with the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch task-group

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.group and 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 = undefined without 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

📥 Commits

Reviewing files that changed from the base of the PR and between 94fc4fd and 189ca89.

📒 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 env to get the path to the Zig standard library when needed

Files:

  • src/zio.zig
  • src/core/awaitable.zig
  • src/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.zig
  • src/core/awaitable.zig
  • src/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.zig
  • src/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.token to *CompactWaitQueue(GroupNode) is valid because CompactWaitQueue is explicitly designed as an 8-byte queue (documented at src/utils/wait_queue.zig:13) with a single head: std.atomic.Value(usize) field. Since self.token is ?*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 token field itself. Unlike other uses of CompactWaitQueue (e.g., in Mutex.zig and Condition.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 comptime assertion to document and verify this assumption, similar to the size check in src/sync/Notify.zig.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant