Repository navigation
Simplify runIo and consolidate IO utilities - #220
Conversation
- Refactor runIo to use a single loop with canceling flag instead of separate cancelIo/waitForIo functions - Add Completion.isCompleted() method that checks both cancel_state.completed and state == .dead - Fix race condition by setting preparing_to_wait before loop.add() - Handle edge case where IO completes during cancellation by calling task.recancel() to preserve the cancel for the next checkpoint - Use beginShield/endShield while waiting for cancel to complete - Remove unused functions: cancelIo, waitForIo, runIoMulti, timedWaitForIo, MultiIoContext, multiIoCallback - Remove unused imports: Executor, meta, Timeoutable, Duration, AutoCancel
- Inline genericCallback as anonymous struct function - Update imports in fs.zig, net.zig, runtime.zig - Delete now-empty io.zig
📝 WalkthroughWalkthroughThis PR reorganizes I/O orchestration by moving Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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: 1
🤖 Fix all issues with AI agents
In `@src/ev/completion.zig`:
- Around line 298-300: isCompleted currently does an acquire load of
Completion.cancel_state and then reads Completion.state non-atomically, which
can see cancel_state.completed=true before state is updated to .dead (race
between finishCompletion's .acq_rel cmpxchg and the later write to state). Fix
by making state an atomic (e.g., atomic enum/int) and use matching ordering
(acquire in isCompleted, release in finishCompletion when writing .dead), or
reorder finishCompletion so state = .dead is written before performing the
.acq_rel cmpxchg that sets completed=true; update references to
Completion.state, finishCompletion, and cancel_state/cmpxchg accordingly.
🧹 Nitpick comments (1)
src/utils/writer.zig (1)
17-83: Optional: guard the single‑byte splat path whensplat_bufferis empty.If a caller ever passes a zero‑length buffer, the single‑byte path can emit zero‑length slices and stall progress. A small fallback keeps it robust.
Suggested guard for empty splat_buffer
- 1 => { - const memset_len = `@min`(splat_buffer.len, splat); - const buf = splat_buffer[0..memset_len]; - `@memset`(buf, pattern[0]); - if (len < max_len) { - out[len] = buf; - len += 1; - } - var remaining_splat = splat - buf.len; - while (remaining_splat > splat_buffer.len and len < max_len) { - out[len] = splat_buffer; - len += 1; - remaining_splat -= splat_buffer.len; - } - if (remaining_splat > 0 and len < max_len) { - out[len] = splat_buffer[0..remaining_splat]; - len += 1; - } - }, + 1 => { + if (splat_buffer.len == 0) { + var i: usize = 0; + while (i < splat and len < max_len) : (i += 1) { + out[len] = pattern; + len += 1; + } + } else { + const memset_len = `@min`(splat_buffer.len, splat); + const buf = splat_buffer[0..memset_len]; + `@memset`(buf, pattern[0]); + if (len < max_len) { + out[len] = buf; + len += 1; + } + var remaining_splat = splat - buf.len; + while (remaining_splat > splat_buffer.len and len < max_len) { + out[len] = splat_buffer; + len += 1; + remaining_splat -= splat_buffer.len; + } + if (remaining_splat > 0 and len < max_len) { + out[len] = splat_buffer[0..remaining_splat]; + len += 1; + } + } + },
Replace racy Completion.isCompleted() with local atomic bool in runIo. The callback sets the bool before waking the task, giving us a race-free way to check if our callback ran.
Summary
runIoto use a single loop with canceling flag instead of separatecancelIo/waitForIofunctionsCompletion.isCompleted()method that checks bothcancel_state.completedandstate == .deadpreparing_to_waitbeforeloop.add()task.recancel()to preserve the cancel for the next checkpointc.errfillBufhelper tosrc/utils/writer.zigrunIotocommon.zigand deleteio.zigcancelIo,waitForIo,runIoMulti,timedWaitForIoTest plan