Skip to content

Add blocking mode support for file operations - #286

Merged
lalinsky merged 3 commits into
mainfrom
blocking-file-ops
Feb 5, 2026
Merged

lalinsky merged 3 commits into
mainfrom
blocking-file-ops

Conversation

@lalinsky

@lalinsky lalinsky commented Feb 5, 2026

Copy link
Copy Markdown
Owner

This PR adds support for using file operations in synchronous code without requiring an async runtime.

Summary

File operations can now be used in regular blocking code. When called without a Runtime, operations execute synchronously using blocking syscalls. When called within a Runtime context, they continue to use the async event loop as before.

Changes

  • New file: src/ev/blocking.zig - Provides synchronous I/O execution
  • Updated: src/common.zig - waitForIo() auto-detects blocking context, added waitForIoUncancelable()
  • Updated: src/fs.zig - Close methods now use waitForIoUncancelable()
  • Updated: src/ev/root.zig - Export executeBlocking function

What Works in Blocking Mode

✅ All file operations (read, write, open, close, stat, sync, setSize, etc.)
✅ All directory operations (openDir, createDir, deleteDir, rename, etc.)
✅ Pipe create/close

What Doesn't Work in Blocking Mode

❌ Network operations (require event loop)
❌ Pipe read/write (require non-blocking I/O)
❌ Timers and async primitives (require event loop)

Example Usage

const zio = @import("zio");

pub fn main() !void {
    // No Runtime initialization needed!
    var file = try zio.fs.createFile("test.txt", .{ .read = true });
    defer file.close();

    _ = try file.write("Hello blocking world!", 0);
    
    var buffer: [100]u8 = undefined;
    const n = try file.read(&buffer, 0);
    // ...
}

Testing

  • All existing tests pass (341/341)
  • New test added: "File: blocking mode without runtime"

Notes

  • TODO comments added for future improvement: allocator should be passed as parameter instead of using std.heap.smp_allocator

This allows file operations to be used in synchronous code without
requiring an async runtime initialization.

Changes:
- Add src/ev/blocking.zig for synchronous I/O execution
- Update waitForIo() to auto-detect blocking context
- Add waitForIoUncancelable() for cleanup operations
- Update close() methods to use waitForIoUncancelable()

When called without a Runtime, file operations now execute
synchronously using blocking syscalls. When called within a
Runtime context, they continue to use the event loop as before.

Supported in blocking mode:
- All file operations (read, write, open, close, stat, sync, etc.)
- All directory operations
- Pipe create/close

Not supported in blocking mode (require event loop):
- Network operations
- Pipe read/write
- Timers and async primitives
@coderabbitai

coderabbitai Bot commented Feb 5, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@lalinsky has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 23 minutes and 34 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📝 Walkthrough

Walkthrough

Adds uncancelable blocking I/O support: new waitForIoUncancelable, extends waitForIo with a blocking (no-task) path, and introduces ev.executeBlocking to synchronously dispatch I/O when no event loop exists. Files and sockets use the uncancelable wait instead of shielding.

Changes

Cohort / File(s) Summary
Core I/O primitives
src/common.zig
Added waitForIoUncancelable(c: *ev.Completion) void. Changed waitForIo to handle a blocking path via ev.executeBlocking when no task/runtime is present; updated docs and control flow.
Blocking dispatcher
src/ev/blocking.zig, src/ev/root.zig
New ev.executeBlocking(c, allocator) implemented in blocking.zig to perform synchronous I/O dispatch for filesystem/pipe ops; re-exported in root.zig.
Filesystem updates & tests
src/fs.zig
Replaced beginShield/endShield usage with waitForIoUncancelable in Dir.close, File.close, Pipe.close. Added test for blocking file ops without an async runtime.
Networking adjustments
src/net.zig
Replaced shielded waitForIo in Socket.close with waitForIoUncancelable, removing shield/cancel wrappers.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

When tasks are gone and loops are still,
A blocking hand completes the will.
No shield, no cancel, just steady time—
Files close safe, sockets fall in line. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: adding blocking mode support for file operations, which is the core purpose of this PR.
Description check ✅ Passed The description clearly relates to the changeset, explaining the blocking mode feature, listing specific files changed, providing examples, and noting test coverage.
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 blocking-file-ops

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.

@lalinsky
lalinsky merged commit 3b21f98 into main Feb 5, 2026
14 checks passed
@lalinsky
lalinsky deleted the blocking-file-ops branch February 5, 2026 20:53
@lalinsky
lalinsky restored the blocking-file-ops branch February 9, 2026 09:25
@lalinsky
lalinsky deleted the blocking-file-ops branch February 9, 2026 13:14
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