Repository navigation
Implement std.Io.Group operations - #154
Conversation
- Add Closure.Start tagged union for regular vs group task functions - Update AnyTask and AnyBlockingTask to handle group start functions - Add standalone groupWait and groupCancel functions in group.zig - Make Group.wait/cancel compatible with std.Io.Group (idempotent, clears token) - Implement groupAsyncImpl, groupConcurrentImpl, groupWaitImpl, groupCancelImpl - Add comptime assertions for std.Io.Group binary compatibility - Add CompactWaitQueue.fromPtr/fromInt helper functions - Add tests for std.Io.Group async/wait, concurrent/wait, and cancel
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe PR refactors task start callbacks from direct function pointers to a discriminated Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
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 |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/core/group.zig (1)
68-69: Unusedgroupparameter ingroupWait.The
groupparameter is passed but immediately discarded with_ = group;. If it's not needed, consider removing it from the signature. If it's reserved for future use, a brief comment would clarify the intent.-pub fn groupWait(rt: *Runtime, group: *Group, list: *CompactWaitQueue(GroupNode)) Cancelable!void { - _ = group; +pub fn groupWait(rt: *Runtime, _: *Group, list: *CompactWaitQueue(GroupNode)) Cancelable!void {Or if you plan to use it later, keep it as-is with a comment.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
src/core/blocking_task.zig(3 hunks)src/core/group.zig(5 hunks)src/core/task.zig(5 hunks)src/stdio.zig(5 hunks)src/utils/wait_queue.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/core/task.zigsrc/utils/wait_queue.zigsrc/stdio.zigsrc/core/group.zigsrc/core/blocking_task.zig
🧠 Learnings (9)
📓 Common learnings
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/coro/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:51.380Z
Learning: Applies to src/lib/coro/**/*.zig : Avoid dependencies on `std.posix` as Zig 0.16 is removing it - create custom wrappers instead
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/aio/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:40.830Z
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
📚 Learning: 2025-11-04T10:29:56.949Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 118
File: src/core/task.zig:33-115
Timestamp: 2025-11-04T10:29:56.949Z
Learning: In zio's task implementation (src/core/task.zig), the Closure struct intentionally caps result_len and context_len at 4096 bytes (using u12 fields) and alignment at 16 bytes (using u4 padding fields). This is a deliberate design decision to avoid bloating task structures. Users needing larger result or context storage should heap-allocate and pass pointers instead. These limits are more generous than std lib's fixed storage (64 bytes for results, 1024 bytes for args).
<!-- [add_learning]
The task storage limits in zio (4 KiB for result/context, 16-byte alignment) are intentional design constraints to prevent task bloat, not correctness issues. The maintainer's position is that larger data should be heap-allocated.
Applied to files:
src/core/task.zigsrc/stdio.zigsrc/core/blocking_task.zig
📚 Learning: 2025-11-30T10:31:51.380Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/coro/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:51.380Z
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/core/task.zigsrc/stdio.zig
📚 Learning: 2025-11-30T10:31:51.380Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/coro/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:51.380Z
Learning: Applies to src/lib/coro/**/*.zig : Do not assume knowledge of std APIs - verify with current Zig documentation instead
Applied to files:
src/core/task.zigsrc/stdio.zig
📚 Learning: 2025-11-30T10:31:40.830Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/aio/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:40.830Z
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/stdio.zig
📚 Learning: 2025-11-30T10:31:40.830Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/lib/aio/CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:40.830Z
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/stdio.zig
📚 Learning: 2025-11-30T10:31:31.222Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:31.222Z
Learning: Applies to **/*.zig : Use `zig env` to get the path to the Zig standard library when needed
Applied to files:
src/stdio.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/group.zig
📚 Learning: 2025-10-03T11:09:46.846Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 18
File: src/net.zig:18-25
Timestamp: 2025-10-03T11:09:46.846Z
Learning: In zio's `Runtime.BlockingTask`, calling `deinit()` on a blocking task only frees the task wrapper (TaskData) itself, not the result returned by the blocking function. The result remains valid and owned by the caller, who is responsible for its cleanup. This is the intended design for async blocking tasks.
Applied to files:
src/core/group.zigsrc/core/blocking_task.zig
🔇 Additional comments (14)
src/utils/wait_queue.zig (1)
650-658: LGTM - useful constructors for external state reconstruction.These helpers enable creating a
CompactWaitQueuefrom raw pointers or integers stored externally (likestd.Io.Group.token). The lack of validation is fine here since these are internal primitives where the caller owns the responsibility.src/core/group.zig (3)
70-88: Clean cancellation handling ingroupWait.The pattern of pushing the current node back before entering shield mode and calling
groupCancelensures no tasks are leaked. The shield prevents recursive cancellation during cleanup. Solid implementation.
91-117: Solid two-phase cancel implementation.The outer
while(true)loop handles the edge case where tasks are added to the group while cancellation is in progress. The two-phase approach (request cancellation first, then wait for completion) is the right pattern. UsingwaitUntilComplete(uncancelable) ensures the cancel operation always completes.
38-50: Good idempotent design forwaitandcancel.Clearing
group.tokenbefore delegating ensures these methods are idempotent - calling them multiple times is safe. The token-to-list reconstruction viafromPtrcorrectly recovers the queue state.src/core/blocking_task.zig (2)
85-93: Switch onClosure.Startvariant handles both paths correctly.The
.regularpath maintains the existing behavior. The.grouppath correctly accesses the group fromawaitable.group_node.group. The.?unwrap is safe here because group tasks are only created viagroupConcurrentImplwhich setsgroup_node.groupbefore scheduling.
190-194: Correct adaptation to newClosure.Starttype.Wrapping the start function in
.{ .regular = ... }maintains the existing behavior for regular blocking tasks while enabling the new group-aware path.src/stdio.zig (5)
47-74: Thorough binary compatibility checks.These comptime assertions catch ABI mismatches at compile time rather than causing subtle runtime corruption. Checking size, alignment, field offsets, and field sizes covers all the bases. Good defensive programming.
166-205:groupConcurrentImplimplementation looks solid.The sequence is correct: create task → register awaitable → set group reference → add to group list → increment ref count → schedule. The
@ptrCaston line 184 is necessary because the std.Io callback signature uses*std.Io.Groupwhile zio internally uses*anyopaque.One observation: after
zio_grp.getList().push()on line 198, there's noerrdeferto remove from the list. However, sinceref_count.incr()is infallible andscheduleTaskdoesn't return errors, this isn't an issue in practice.
159-164: Sync fallback ingroupAsyncImplensures reliability.If concurrent scheduling fails (e.g., runtime shutting down), falling back to synchronous execution ensures the work still gets done. This matches the existing pattern in
asyncImpl.
207-219:groupWaitImplandgroupCancelImplcorrectly delegate to zio internals.The
catch {}on line 211 silently handles cancellation, which matches the expected behavior -groupWaitin std.Io returns void, not an error. The list reconstruction from token works correctly.
1344-1441: Good test coverage for the new Group operations.The three tests cover async/wait, concurrent/wait, and cancel paths through the std.Io interface. The cancel test verifies that cancellation completes quickly rather than waiting for the full sleep duration.
src/core/task.zig (3)
30-35: Clean discriminated union for task start semantics.The
Startunion is a good design choice - it makes the two task modes explicit at the type level rather than overloading a single function signature. Theregularandgroupvariants have clearly different signatures matching their respective use cases.
304-312:startFncorrectly dispatches based on task type.The switch handles both variants appropriately. The
.?unwrap ongroup_node.groupis safe because the group reference is set bygroupConcurrentImplbefore the task is scheduled.
415-423: Backward-compatible wrapping inTask.create.Existing code using
Task.createcontinues to work unchanged since the internal wrapper uses the.regularvariant.
- Use state field as counter + flags (bits 62-63 for canceled/failed) - Use context field as ResetEvent for waiters - Early signal when task returns error (sets canceled or failed flag) - ResetEvent now uses CompactWaitQueue to fit in single pointer - Simplify groupWait to use ResetEvent.wait()
- Add fail_fast_flag (bit 61) to Group state encoding - Add setFailFast() and isFailFast() methods - setFailed()/setCanceled() now signal event early only if fail_fast is set - Default behavior: wait for all tasks regardless of errors
Add Closure.call() method that handles both regular and group cases, including counter decrement and event signaling for group tasks. Both AnyTask.startFn and blocking_task.workFunc now use this shared method.
* Implement std.Io.Group operations - Add Closure.Start tagged union for regular vs group task functions - Update AnyTask and AnyBlockingTask to handle group start functions - Add standalone groupWait and groupCancel functions in group.zig - Make Group.wait/cancel compatible with std.Io.Group (idempotent, clears token) - Implement groupAsyncImpl, groupConcurrentImpl, groupWaitImpl, groupCancelImpl - Add comptime assertions for std.Io.Group binary compatibility - Add CompactWaitQueue.fromPtr/fromInt helper functions - Add tests for std.Io.Group async/wait, concurrent/wait, and cancel * Improve Group implementation with counter-based waiting - Use state field as counter + flags (bits 62-63 for canceled/failed) - Use context field as ResetEvent for waiters - Early signal when task returns error (sets canceled or failed flag) - ResetEvent now uses CompactWaitQueue to fit in single pointer - Simplify groupWait to use ResetEvent.wait() * Add fail_fast flag for early exit on task error - Add fail_fast_flag (bit 61) to Group state encoding - Add setFailFast() and isFailFast() methods - setFailed()/setCanceled() now signal event early only if fail_fast is set - Default behavior: wait for all tasks regardless of errors * Unify Closure.start calling for tasks and blocking tasks Add Closure.call() method that handles both regular and group cases, including counter decrement and event signaling for group tasks. Both AnyTask.startFn and blocking_task.workFunc now use this shared method.
* Implement std.Io.Group operations - Add Closure.Start tagged union for regular vs group task functions - Update AnyTask and AnyBlockingTask to handle group start functions - Add standalone groupWait and groupCancel functions in group.zig - Make Group.wait/cancel compatible with std.Io.Group (idempotent, clears token) - Implement groupAsyncImpl, groupConcurrentImpl, groupWaitImpl, groupCancelImpl - Add comptime assertions for std.Io.Group binary compatibility - Add CompactWaitQueue.fromPtr/fromInt helper functions - Add tests for std.Io.Group async/wait, concurrent/wait, and cancel * Improve Group implementation with counter-based waiting - Use state field as counter + flags (bits 62-63 for canceled/failed) - Use context field as ResetEvent for waiters - Early signal when task returns error (sets canceled or failed flag) - ResetEvent now uses CompactWaitQueue to fit in single pointer - Simplify groupWait to use ResetEvent.wait() * Add fail_fast flag for early exit on task error - Add fail_fast_flag (bit 61) to Group state encoding - Add setFailFast() and isFailFast() methods - setFailed()/setCanceled() now signal event early only if fail_fast is set - Default behavior: wait for all tasks regardless of errors * Unify Closure.start calling for tasks and blocking tasks Add Closure.call() method that handles both regular and group cases, including counter decrement and event signaling for group tasks. Both AnyTask.startFn and blocking_task.workFunc now use this shared method.
Summary
Closure.Starttagged union for regular vs group task functionsAnyTaskandAnyBlockingTaskto handle group start functionsgroupWaitandgroupCancelfunctions in group.zigGroup.wait/cancelcompatible withstd.Io.Group(idempotent, clears token)groupAsyncImpl,groupConcurrentImpl,groupWaitImpl,groupCancelImplstd.Io.Groupbinary compatibilityCompactWaitQueue.fromPtr/fromInthelper functionsstd.Io.Groupasync/wait, concurrent/wait, and cancelTest plan
Io: Group async/wait,Io: Group concurrent/wait,Io: Group cancel