Skip to content

std.Io: implement operateTimeout for single-operation batches - #387

Merged
lalinsky merged 2 commits into
mainfrom
io-operate-timeout
May 12, 2026
Merged

lalinsky merged 2 commits into
mainfrom
io-operate-timeout

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Summary

  • Refactors operateImpl to delegate to a new operateInner that accepts an explicit time.Timeout, replacing waitForIo with timedWaitForIo in fileReadStreamingImpl, fileWriteStreamingImpl, and netReceiveImpl
  • Implements batchAwaitConcurrentImpl for the specialized case of exactly one submitted operation with no pending ops, which covers the io.operateTimeout call pattern
  • Adds tests for the success and timeout paths via io.operateTimeout on a UDP socket

@coderabbitai

coderabbitai Bot commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c212a392-cb48-4cff-a9da-1404c589277d

📥 Commits

Reviewing files that changed from the base of the PR and between 85d0698 and af529b9.

📒 Files selected for processing (1)
  • src/io.zig
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/io.zig

📝 Walkthrough

Walkthrough

Operation dispatch was refactored to pass an optional timeout through a new operateInner helper. File read/write streaming and datagram receive now accept timeouts and use timedWaitForIo. batchAwaitConcurrentImpl gains a fast path for single submitted operations. Tests cover net_receive timeout behavior.

Changes

Timeout-aware I/O operations and batch optimization

Layer / File(s) Summary
Core operation routing refactor
src/io.zig
operateImpl delegates to operateInner(operation, timeout) enabling timeout-aware routing for operations including net_receive.
File streaming and datagram receive timeout support
src/io.zig
fileReadStreamingImpl, fileWriteStreamingImpl, and netReceiveImpl accept time.Timeout, add common.Timeoutable to their errors, and use timedWaitForIo instead of waitForIo.
Batch concurrent execution single-operation fast path
src/io.zig
batchAwaitConcurrentImpl fast-path executes a single submitted operation inline via operateInner(timeout) and links its result into the batch completion list.
Timeout test coverage for net_receive
src/io.zig
Two io.operateTimeout tests: one verifies successful datagram receipt before the deadline, the other verifies error.Timeout when none arrives.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

  • lalinsky/zio#380: Modifies batch execution and core operation dispatch paths; overlaps the refactoring surface touched here.

Poem

⏰ A helper takes the ticking hand in stride,
Timeouts travel down the IO tide.
One-op batches skip the multi-op stall,
Datagram waits answer duty's call.
Code and clock now dance—no more stall. 🎯

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: implementing operateTimeout for single-operation batches, which is the core addition shown in the changeset.
Description check ✅ Passed The description maps precisely to the changeset, detailing the operateInner refactor, timeout handling, and the specialized fast path for single operations with tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch io-operate-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.

🧹 Nitpick comments (2)
src/io.zig (2)

339-367: 💤 Low value

Three of four arms repeat the same Canceled/Timeout passthrough.

Not a bug, just noise. If a fifth operation lands, you'll be copy-pasting the same error.Canceled => |e| return e, error.Timeout => |e| return e, again. A tiny helper that maps (Cancelable||Timeoutable||OpErr) into either an early return or an else => |e| ... payload would shrink each arm to one line. Happy to leave it as-is too — the pattern is obvious enough at four cases.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/io.zig` around lines 339 - 367, The three arms in operateInner repeat
identical catch/err->return logic; create a small helper (e.g.,
mapCancelableOrTimeout) that accepts the error union from
fileReadStreamingImpl/fileWriteStreamingImpl/netReceiveImpl and either performs
the early return for error.Canceled/error.Timeout or converts the error into the
payload form expected by the arm, then call that helper from each arm
(operateInner, replace the inline catch blocks used around
fileReadStreamingImpl, fileWriteStreamingImpl, and netReceiveImpl); keep
device_io_control as-is using deviceIoControlBlocking. Ensure the helper returns
the correct variant/type so each arm can simply call the impl and pass its
result through the helper instead of duplicating the two return cases.

427-448: 💤 Low value

Fast path looks right; worth a note about the error path.

On success the linking into batch.completed is correct in both the empty-list and append cases, and batch.submitted = .empty happens only after operateInner returns ok — good ordering.

On error.Canceled / error.Timeout from operateInner, batch state is intentionally untouched: submitted.head still points at only, storage.submission.operation is still populated, and the in-flight ev.* op has gone out of scope inside the per-op impl. This is fine as long as timedWaitForIo actually cancels/awaits the kernel op before returning Timeout (otherwise you've got a stack-allocated completion target the kernel may still poke). Worth confirming that contract holds — it's the load-bearing assumption for the whole fast path.

Also: if a caller re-calls batchAwaitConcurrent after a timeout, you'll re-enter the fast path and re-issue the operation. For net_receive that's harmless; for future ops it might not be. A short doc comment on the fast path spelling out "errors leave the batch as if no progress was made; re-await will reissue" would save a future reader some head-scratching.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/io.zig` around lines 427 - 448, Add a short doc comment above
batchAwaitConcurrentImpl that explains the fast-path behavior: when the
single-operation fast path (detected via batch.submitted.head/tail and
batch.pending.head) calls operateInner(time.Timeout.fromStd(timeout)) and it
returns an error (error.Canceled / error.Timeout), the function intentionally
leaves batch.submitted and storage.submission.operation untouched so a
subsequent call to batchAwaitConcurrentImpl will re-issue the operation; also
state the required contract that the underlying timedWaitForIo/operateInner
implementation must cancel/await the kernel operation before returning
Timeout/Canceled (so the kernel will not later write to the stack-allocated
completion), and optionally add a comment suggesting callers be aware that
re-await will reissue the op for operations where that matters (e.g.,
non-net_receive). Ensure you reference symbols operateInner, timedWaitForIo,
batch.submitted, storage.submission.operation, and batch.completed in the
comment so readers can find the relevant code.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/io.zig`:
- Around line 339-367: The three arms in operateInner repeat identical
catch/err->return logic; create a small helper (e.g., mapCancelableOrTimeout)
that accepts the error union from
fileReadStreamingImpl/fileWriteStreamingImpl/netReceiveImpl and either performs
the early return for error.Canceled/error.Timeout or converts the error into the
payload form expected by the arm, then call that helper from each arm
(operateInner, replace the inline catch blocks used around
fileReadStreamingImpl, fileWriteStreamingImpl, and netReceiveImpl); keep
device_io_control as-is using deviceIoControlBlocking. Ensure the helper returns
the correct variant/type so each arm can simply call the impl and pass its
result through the helper instead of duplicating the two return cases.
- Around line 427-448: Add a short doc comment above batchAwaitConcurrentImpl
that explains the fast-path behavior: when the single-operation fast path
(detected via batch.submitted.head/tail and batch.pending.head) calls
operateInner(time.Timeout.fromStd(timeout)) and it returns an error
(error.Canceled / error.Timeout), the function intentionally leaves
batch.submitted and storage.submission.operation untouched so a subsequent call
to batchAwaitConcurrentImpl will re-issue the operation; also state the required
contract that the underlying timedWaitForIo/operateInner implementation must
cancel/await the kernel operation before returning Timeout/Canceled (so the
kernel will not later write to the stack-allocated completion), and optionally
add a comment suggesting callers be aware that re-await will reissue the op for
operations where that matters (e.g., non-net_receive). Ensure you reference
symbols operateInner, timedWaitForIo, batch.submitted,
storage.submission.operation, and batch.completed in the comment so readers can
find the relevant code.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4f43ce3c-2b3b-4687-810f-17c89f15f7a7

📥 Commits

Reviewing files that changed from the base of the PR and between ea5f6dd and 85d0698.

📒 Files selected for processing (1)
  • src/io.zig

@lalinsky
lalinsky merged commit 9f079f7 into main May 12, 2026
26 checks passed
@lalinsky
lalinsky deleted the io-operate-timeout branch May 12, 2026 09:26
@coderabbitai coderabbitai Bot mentioned this pull request May 13, 2026
3 tasks done
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