Skip to content

Add primitives for waiting on I/O with timeout - #227

Merged
lalinsky merged 4 commits into
mainfrom
io-timeout
Jan 22, 2026
Merged

lalinsky merged 4 commits into
mainfrom
io-timeout

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

No description provided.

- Timer.init() now accepts Timeout (none/duration/deadline) instead of Duration
- .none sets delay to Duration.max (effectively infinite)
- .deadline pre-sets the deadline timestamp, skipping delay calculation
- timedWaitForIo() now accepts Timeout, handles .none by calling waitForIo directly
- LoopState.setTimer() only computes deadline from delay if delay > 0
@coderabbitai

coderabbitai Bot commented Jan 22, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR changes Timer construction to accept Timeout variants and updates call sites to use a named-field duration initializer, renames runIo to waitForIo, adds timedWaitForIo for timeout-aware I/O, and makes loop deadline computation conditional when delay is zero.

Changes

Cohort / File(s) Summary
Timer init call-sites
examples/ev_demo.zig, src/ev/test/cancel.zig, src/ev/test/group.zig, src/ev/test/timer.zig, src/ev/tests.zig, src/runtime/autocancel.zig
Updated Timer construction syntax from positional to named-field: .init(.fromMilliseconds(X)) → .init(.{ .duration = .fromMilliseconds(X) }) (including .zero).
Timer API & completion
src/ev/completion.zig
Timer.init signature changed from Duration → Timeout; Timer.delay now has default .zero; init maps Timeout variants (none/duration/deadline) to appropriate Timer fields.
Event loop timer handling
src/ev/loop.zig
LoopState.setTimer now recomputes timer.deadline only when timer.delay.ns > 0, preserving existing deadline for zero delay.
Docs
src/ev/README.md
Example updated to use .init(.{ .duration = .fromSeconds(1) }).
I/O helper APIs
src/common.zig
runIo renamed to waitForIo; new timedWaitForIo(rt, c, timeout) added to run I/O with timeout semantics (race with timer); tests added for wait/timeout behaviors.
I/O call sites
src/fs.zig, src/net.zig, src/runtime.zig
Replaced runIo with waitForIo across filesystem, network, and runtime codepaths; runtime sleep timer init updated to named-field form.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

⏳ Timers learned to pick a named field, not a guess,
waitForIo waits while races test the press,
Deadlines stand firm unless delay is nil,
Small changes, sharp clocks — the loop turns still. 🛠️

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Description check ❓ Inconclusive No description was provided by the author, making it impossible to assess relevance to the changeset. Add a brief description explaining the motivation and key changes, such as the new timedWaitForIo API and Timer initialization refactor.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add primitives for waiting on I/O with timeout' directly matches the main change: introducing timedWaitForIo and related timeout handling across the codebase.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch io-timeout

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: 1

🤖 Fix all issues with AI agents
In `@src/common.zig`:
- Around line 93-106: The current logic infers a timeout by checking c.err for
error.Canceled, which conflates real IO cancellations with timeout
cancellations; instead after waitForIo(rt, &group.c) check the timer completion
status (e.g., inspect timer.c.err / timer result) to see if the timer finished
successfully and only then return error.Timeout, otherwise propagate c.err from
the IO callback (c) as the true IO error; update the logic around
ev.Timer.init(timeout), group.add(&timer.c), and the post-wait check in the
waitForIo handling to use timer.c (not c.err) to detect timeout.

Comment thread src/common.zig Resolved
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