Skip to content

[Stdlib] Fix FileDescriptor.write_bytes() silently dropping data on partial writes - #5962

Open
msaelices wants to merge 5 commits into
modular:mainfrom
msaelices:filedescriptor-partial-write-fix
Open

msaelices wants to merge 5 commits into
modular:mainfrom
msaelices:filedescriptor-partial-write-fix

Conversation

@msaelices

Copy link
Copy Markdown
Contributor

Summary

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.

Copilot AI review requested due to automatic review settings February 18, 2026 00:02
@msaelices
msaelices requested a review from a team as a code owner February 18, 2026 00:02

Copilot AI 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.

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.

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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).

Copilot uses AI. Check for mistakes.
Comment on lines +77 to +86
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")

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread mojo/stdlib/std/io/file_descriptor.mojo Outdated
Comment on lines +77 to +83
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,
)

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@lsh

lsh commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Thanks, this seems like an improvement! Can you please add a test case where you maybe write more bytes than PIPE_BUF to a pipe with O_NONBLOCK fd?

@github-actions github-actions Bot added mojo-stdlib Tag for issues related to standard library waiting-on-review mojo-docs labels Feb 26, 2026
@msaelices

Copy link
Copy Markdown
Contributor Author

Thanks, this seems like an improvement! Can you please add a test case where you maybe write more bytes than PIPE_BUF to a pipe with O_NONBLOCK fd?

f9a9d94

@msaelices
msaelices force-pushed the filedescriptor-partial-write-fix branch from c8f944c to ef0277a Compare April 11, 2026 10:21
@msaelices
msaelices force-pushed the filedescriptor-partial-write-fix branch from a242386 to 3aec8d1 Compare May 3, 2026 19:20
@msaelices
msaelices force-pushed the filedescriptor-partial-write-fix branch from d8e7759 to 508e224 Compare August 16, 2026 09:03
@alisterburt alisterburt self-assigned this Oct 15, 2026
…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]>
…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]>
@alisterburt
alisterburt force-pushed the filedescriptor-partial-write-fix branch from c33b80e to 5850223 Compare October 15, 2026 04:22
@alisterburt
alisterburt requested a review from a team as a code owner October 15, 2026 04:22
modularbot pushed a commit that referenced this pull request Nov 18, 2026
[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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mojo-docs mojo-stdlib Tag for issues related to standard library waiting-on-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants