Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes FileDescriptor.write_bytes() to correctly handle partial writes by looping until the full buffer is written, aligning its behavior with FileHandle.write_bytes() and preventing silent data loss in release builds.
Changes:
- Update
FileDescriptor.write_bytes()to loop until all bytes are written and abort on write failure/0-byte writes. - Add a changelog entry documenting the fix.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
mojo/stdlib/std/io/file_descriptor.mojo |
Implements write-all semantics for write_bytes() to prevent silent truncation on partial writes. |
mojo/docs/changelog.md |
Documents the behavioral fix in the changelog. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Write a span of bytes to the file. | ||
|
|
||
| Handles partial writes by looping until all bytes are written. | ||
|
|
There was a problem hiding this comment.
The docstring for write_bytes doesn’t mention that this method aborts on syscall failure (and on a 0-byte write). Since this is part of the Writer-trait surface and now has explicit failure paths, document the abort-on-error behavior (and optionally point users to a raising alternative if one exists) to avoid surprising callers.
| This method aborts the process if the underlying `write(2)` syscall | |
| fails (returns a negative value) or if it returns 0 bytes (for | |
| example, when the file descriptor is full or has been closed). |
| var total_written = 0 | ||
| while total_written < len(bytes): | ||
| var bytes_written = external_call["write", c_ssize_t]( | ||
| self.value, | ||
| bytes.unsafe_ptr() + total_written, | ||
| len(bytes) - total_written, | ||
| ) | ||
|
|
||
| if bytes_written < 0: | ||
| abort("write() syscall failed") |
There was a problem hiding this comment.
FileDescriptor can be constructed with invalid fds (e.g. -1), but write_bytes() currently only reports a generic "write() syscall failed". Consider validating self.value (e.g., < 0) up front and aborting with a more specific message, similar to FileHandle.write_bytes(), to make failures easier to diagnose.
| var total_written = 0 | ||
| while total_written < len(bytes): | ||
| var bytes_written = external_call["write", c_ssize_t]( | ||
| self.value, | ||
| bytes.unsafe_ptr() + total_written, | ||
| len(bytes) - total_written, | ||
| ) |
There was a problem hiding this comment.
This change fixes a correctness issue (silent data loss on partial writes), but there’s no test covering partial-write behavior for FileDescriptor.write_bytes(). Please add an IO test that forces partial writes (e.g., write to a pipe/socket with constrained buffer) and asserts the full payload is observed on the read side.
|
Thanks, this seems like an improvement! Can you please add a test case where you maybe write more bytes than |
|
c8f944c to
ef0277a
Compare
a242386 to
3aec8d1
Compare
d8e7759 to
508e224
Compare
…artial writes Loop until all bytes are written, matching FileHandle.write_bytes() behavior. Previously a single write() call with only a debug_assert meant partial writes (pipes, sockets, large writes) silently lost data in release builds. Signed-off-by: Manuel Saelices <[email protected]>
Signed-off-by: Manuel Saelices <[email protected]>
…pipe Add a test that writes more than PIPE_BUF bytes to a pipe with O_NONBLOCK set, exercising the partial-write retry loop. Signed-off-by: Manuel Saelices <[email protected]>
…tring Mention that the method aborts on write(2) failure or 0-byte write. Signed-off-by: Manuel Saelices <[email protected]>
The zero-byte-write abort message fits within the line limit, so keep it on one line to match mojo format output. Assisted-by: AI Signed-off-by: Manuel Saelices <[email protected]>
c33b80e to
5850223
Compare
[External] [stdlib] Read and write files over 2 GiB on macOS ## Linked issue Associated issue: #7616 ## Type of change - [x] Bug fix (non-breaking change that fixes an issue) ## Motivation On macOS, a `FileHandle` read or write of more than 2 GiB fails: `read_bytes()`, `read()`, `write_once()` and `write_all()` raise "Invalid argument", and `write_bytes()` aborts. ## What changed macOS rejects a `read` or `write` of more than `INT_MAX` bytes with `EINVAL` instead of transferring fewer. `FileHandle` now sends every `read` and `write` through `_platform_read()`/`_platform_write()`, which cap each call at `c_int.MAX` on macOS only; the existing loops do the rest, and other platforms pass the full length as before. `FileHandle.write()` has the same problem in `RawFileDescriptor.write_bytes()`, which I left alone because #5962 rewrites it. ## Testing - New `test_file_read_over_int_max` and `test_file_write_over_int_max` allocate `c_int.MAX + 1`-byte buffers (over 4 GiB in the read test, over 2 GiB in the write test). They fail on main and pass with this change; the rest of `test_file.mojo` and the other IO tests still pass. - Tested on macOS 27 arm64 with Mojo 1.3.0.dev2026111316 and a precompiled stdlib from this branch; not with Bazel or on Linux. ## Checklist - [ ] The linked issue above has been reviewed by a maintainer and is agreed-upon, or this is a trivial fix that does not need prior approval - [x] PR is small and focused - [ ] I ran `./bazelw run format` to format my changes (ran `mojo format` from the nightly; no changes) - [x] I added or updated tests to cover my changes - [x] If AI tools assisted with this contribution, I have included an `Assisted-by:` trailer in my commit message or this PR description Assisted-by: AI ORIGINAL_AUTHOR=A&W <[email protected]> ORIGINAL_USER=@Alex-Wengg PUBLIC_PR_HEAD=4e0d1fda2de5227962aee52c1c6ea7e53419effa --------- Co-authored-by: A&W <[email protected]> Co-authored-by: Nathan Ward <[email protected]> Pull-Request: #7617 MODULAR_ORIG_COMMIT_REV_ID: eb2ec8c37cbe91293d66caf2023e360278eeb7c0
Summary
Loop until all bytes are written, matching
FileHandle.write_bytes()behavior. Previously a singlewrite()call with only adebug_assertmeant partial writes (pipes, sockets, large writes) silently lost data in release builds.