Repository navigation
Implement std.Io.select - #158
Conversation
WalkthroughA new public selectAwaitables function enables selecting among up to 64 type-erased Awaitable pointers, returning the index of the first to complete. The stdio.zig selectImpl now delegates to this function. A test validates async selection behavior. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
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 |
1f63d21 to
c12c3be
Compare
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/stdio.zig (1)
927-958:Io: selecttest exercises the new path well; optional: add a case where the slow branch winsThis test does a good job driving the new std.Io.select implementation through:
- async futures backed by zio’s runtime
- mixing a plain
i32result (fastTask) and an!i32(slowTask)- asserting that
.fastwins and that the union tag matches.Right now the
.slowarm only runs if something is broken. If you ever care about coverage on the slow path, consider a second test that intentionally makes the slow task win (e.g. flip which future is “fast”) so that branch is exercised in a non-failure scenario. Not mandatory, but it would make the test suite more symmetric.src/select.zig (1)
10-10: selectAwaitables mirrors select() semantics; consider guarding the empty-slice caseThe new
selectAwaitablesimplementation lines up with the existingselectlogic:
- Uses
SelectWaiterand the shared winner atomic the same way.- Enforces a 64‑awaitable cap with a panic, which matches the “stack-allocated waiters” design you outlined.
- Registers each awaitable via
asyncWait, supports the fast path whenasyncWaitreturnsfalse, and cleans up pending waits viaasyncCancelWaitin the defer.- Requires an active task and uses the same task state transitions and
executor.yieldpattern asselect/waitInternal.One edge case worth calling out: if
awaitables.len == 0, the registration loop is skipped, you still callexecutor.yield, andwinnerremainsNO_WINNER, which will trip the assert at the end. That’s probably a programmer error, but you might want an explicit guard for a clearer failure:pub fn selectAwaitables(rt: *Runtime, awaitables: []const *Awaitable) Cancelable!usize { const max_awaitables = 64; - if (awaitables.len > max_awaitables) { - @panic("selectAwaitables: too many awaitables (max 64)"); - } + if (awaitables.len == 0) { + @panic("selectAwaitables: at least one awaitable is required"); + } + if (awaitables.len > max_awaitables) { + @panic("selectAwaitables: too many awaitables (max 64)"); + }Not a blocker, but it would fail faster and more explicitly if someone ever misuses the API.
Also applies to: 382-433
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/select.zig(2 hunks)src/stdio.zig(2 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/stdio.zigsrc/select.zig
🧠 Learnings (2)
📓 Common learnings
Learnt from: lalinsky
Repo: lalinsky/zio PR: 70
File: src/select.zig:145-153
Timestamp: 2025-10-23T16:17:53.575Z
Learning: In src/select.zig, the select() function is designed to accept futures either as copyable wrappers (e.g., JoinHandle(T)) by value, or as pointers (e.g., &reset_event) for value-owned types. This is intentional, and users are expected to pass the appropriate form based on the future type.
📚 Learning: 2025-10-23T16:17:53.575Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 70
File: src/select.zig:145-153
Timestamp: 2025-10-23T16:17:53.575Z
Learning: In src/select.zig, the select() function is designed to accept futures either as copyable wrappers (e.g., JoinHandle(T)) by value, or as pointers (e.g., &reset_event) for value-owned types. This is intentional, and users are expected to pass the appropriate form based on the future type.
Applied to files:
src/stdio.zigsrc/select.zig
🔇 Additional comments (1)
src/stdio.zig (1)
226-230: selectImpl wiring looks correct and matches existing casting patternsCasting
[]const *std.Io.AnyFutureto[]const *Awaitableand delegating toselect.selectAwaitablesis consistent with howawaitOrCancelalready treatsAnyFuture(via@ptrCast(@alignCast(any_future))). The implementation is minimal and does exactly what std.Io’s vtable.select needs here. I don’t see any correctness issues.
Summary
selectAwaitablesfunction inselect.zigfor runtime select on type-erased*AwaitablesliceselectImplinstdio.zigusing the new functionTest plan
./check.sh- all tests passio.selectworks with async futures