Skip to content

Fix Windows file I/O position tracking for overlapped operations - #17

Merged
lalinsky merged 1 commit into
zig-0.15from
fix-windows-file-position-tracking
Oct 3, 2025
Merged

lalinsky merged 1 commit into
zig-0.15from
fix-windows-file-position-tracking

Conversation

@lalinsky

@lalinsky lalinsky commented Oct 3, 2025

Copy link
Copy Markdown
Owner

Problem

Windows overlapped I/O (required for IOCP) doesn't maintain file position automatically. When files are opened with FILE_FLAG_OVERLAPPED, the offset fields in the OVERLAPPED structure were uninitialized, causing read/write operations to occur at unpredictable offsets.

This manifested as test failures where reading 10 bytes would return 20 bytes (reading the same data twice from offset 0).

Solution

Added position tracking to the File abstraction and converted all sequential read/write operations to use positional I/O (pread/pwrite) with the tracked position.

Changes

1. Position tracking in File struct (src/file.zig)

  • Added position: u64 field to track file position
  • Converted read(), write(), readBuf(), writeBuf() to use pread/pwrite
  • Position automatically increments after each successful operation
  • Added seek() method for manual position control (from start/current)
  • Updated function signatures from *const File to *File (required for mutation)

2. Fixed libxev IOCP backend bugs (vendor/libxev/src/backend/iocp.zig)

  • Fixed pread error handling: was returning .read instead of .pread
  • Fixed pwrite error handling: was returning .write instead of .pwrite
  • Added HANDLE_EOF error handling for pread operations

3. Build infrastructure (build.zig, check.sh)

  • Added build-tests step for cross-compilation testing
  • Updated test runner to use built test executable

Design Rationale

Using pread/pwrite everywhere with manual position tracking:

  • Cross-platform consistency: Works identically on all platforms
  • Simpler reasoning: Position is always explicit, not OS-managed
  • Eliminates the bug: No reliance on Windows file pointer (which doesn't exist for overlapped I/O)
  • POSIX-compatible: Sequential operations behave like standard POSIX file I/O

Testing

All 37 tests pass on both platforms:

  • ✅ Linux (native): ./check.sh - 37/37 tests pass
  • ✅ Windows (wine): wine ./zig-out/bin/test.exe - 37/37 tests pass

Specific test coverage:

  • File position tracking with sequential reads/writes
  • Positional I/O with pread/pwrite (unchanged behavior)
  • Reader/Writer interface integration
  • EOF handling on Windows

Thread Safety Note

File instances are single-threaded (all async code runs on one thread). Position tracking does not require synchronization.

Future Considerations

Per code review, we may want to consider using threadpool for file I/O on Windows (like libuv does) instead of IOCP, since IOCP is designed for sockets/pipes rather than regular files. This would eliminate the need for position tracking entirely.

Windows overlapped I/O (required for IOCP) doesn't maintain file position
automatically. All read/write operations were using uninitialized offset
values in the OVERLAPPED structure, causing unpredictable behavior.

Changes:

1. Add position tracking to File struct (src/file.zig)
   - Track position starting at 0 (POSIX-compatible)
   - Convert read/write to use pread/pwrite with tracked position
   - Add seek() method for manual position control
   - Update position after each successful operation

2. Fix libxev IOCP backend bugs (vendor/libxev/src/backend/iocp.zig)
   - Fix pread error handler returning .read instead of .pread
   - Fix pwrite error handler returning .write instead of .pwrite
   - Add HANDLE_EOF handling for pread operations

3. Add build-tests step for cross-compilation testing (build.zig)

The solution uses pread/pwrite everywhere with manual position tracking,
which works consistently across all platforms and eliminates Windows's
uninitialized offset issue entirely.

All tests pass on both Linux (native) and Windows (wine): 37/37
@coderabbitai

coderabbitai Bot commented Oct 3, 2025

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-windows-file-position-tracking

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 9f23c00 into zig-0.15 Oct 3, 2025
lalinsky added a commit that referenced this pull request Nov 18, 2025
4bb1a60 Fix stack unwinding on aarch64 and riscv64 (#18)
ae64534 Update CI to use Zig master
cf32ccd Update for Zig 0.16 compatibility
c2f6ee8 Add a test for capturing stack trace (#17)
3d4c6de Test using DebugAllocator in coroutine (#14)
6536658 Fix the test
b6eec4f Build tests in release mode
REVERT: eab0a7b Merge remote-tracking branch 'origin/main' into zig-0.16
REVERT: 7579996 Expose StackPoolConfig
REVERT: 7fa7b4f Update CI to use Zig master
REVERT: cbabd36 Update for Zig 0.16 compatibility

git-subtree-dir: src/lib/coro
git-subtree-split: 4bb1a60186952636e6d3e409d62d9e8cf50b1560
lalinsky added a commit that referenced this pull request Jan 6, 2026
* Remove userdata from callback signature

Users can now access userdata via c.userdata instead of receiving it
as a parameter. This simplifies callback signatures from:
  fn(userdata, loop, completion)
to:
  fn(loop, completion)

Changes:
- Updated Completion.CallbackFn and Work.WorkFn signatures
- Modified Completion.call and thread pool to not pass userdata
- Updated Work.init to set c.userdata for easy access
- Updated tests to use c.userdata

Fixes #15

Co-authored-by: Lukáš Lalinský <[email protected]>

* Fix Work callback signature to receive Work pointer

WorkFn now receives (*Loop, *Work) instead of (*Loop, *Completion).
This allows callbacks to access the Work struct directly, including
its userdata field. Work.init no longer incorrectly sets c.userdata.

Co-authored-by: Lukáš Lalinský <[email protected]>

* Use implicit struct literal syntax in Work.init

This makes Work.init consistent with all other init functions
in the file, which use 'return .{' instead of 'return Type{'.

Co-authored-by: Lukáš Lalinský <[email protected]>

---------

Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Lukáš Lalinský <[email protected]>
lalinsky added a commit that referenced this pull request Jan 6, 2026
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