Repository navigation
Fix Windows file I/O position tracking for overlapped operations - #17
Merged
Merged
Conversation
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
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ 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 |
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 theOVERLAPPEDstructure 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)position: u64field to track file positionread(),write(),readBuf(),writeBuf()to usepread/pwriteseek()method for manual position control (from start/current)*const Fileto*File(required for mutation)2. Fixed libxev IOCP backend bugs (
vendor/libxev/src/backend/iocp.zig)preaderror handling: was returning.readinstead of.preadpwriteerror handling: was returning.writeinstead of.pwriteHANDLE_EOFerror handling forpreadoperations3. Build infrastructure (
build.zig,check.sh)build-testsstep for cross-compilation testingDesign Rationale
Using
pread/pwriteeverywhere with manual position tracking:Testing
All 37 tests pass on both platforms:
./check.sh- 37/37 tests passwine ./zig-out/bin/test.exe- 37/37 tests passSpecific test coverage:
pread/pwrite(unchanged behavior)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.