Repository navigation
Description
Bug description
On macOS, FileHandle can't read or write more than 2 GiB in one call. read_bytes(n) with n over 2 GiB and read_bytes() on a file over 2 GiB raise Failed to read from file: Invalid argument, write_once() and write_all() raise Failed to write to file: Invalid argument, and write_bytes() aborts with write() syscall failed.
Expected: the same as on Linux. read_bytes(2500 MiB) returns 2,621,440,000 bytes, read_bytes() returns the whole file, and the writes succeed (write_once() with a short count).
macOS rejects a read or write of more than INT_MAX (2^31 - 1) bytes with EINVAL. Linux instead transfers at most 0x7FFFF000 bytes and returns a short count, which the existing loops handle. That's why the Linux symptom in #1622 is already fixed on main. FileHandle passes the whole remaining count to each call, in read() into a Span, read_bytes(), write_once() (which write_all() calls), write_bytes() and _write(). Capping each call at INT_MAX fixes it, the same fix #7476 proposes for sockets.
Related: FileHandle.write() goes through RawFileDescriptor.write_bytes() in file_descriptor.mojo, which makes one write call and checks it only with assert. On macOS, writing a String over 2 GiB to a file writes nothing and raises nothing; the file stays at 0 bytes. #5962 rewrites that function.
Steps to reproduce
mkfile -n 2600m big.bin # sparse, uses no disk
mojo run repro.mojodef main() raises:
var f = open("big.bin", "r")
try:
print("read_bytes(2500 MiB):", len(f.read_bytes(2500 * 1024 * 1024)))
except e:
print("read_bytes(2500 MiB) error:", e)
_ = f.seek(0)
try:
print("read_bytes():", len(f.read_bytes()))
except e:
print("read_bytes() error:", e)
f^.close()
var buf = List[UInt8](length=2200 * 1024 * 1024, fill=0)
var dst = open("/dev/null", "w")
try:
print("write_once:", dst.write_once(buf))
except e:
print("write_once error:", e)
try:
dst.write_all(buf)
print("write_all: ok")
except e:
print("write_all error:", e)
dst.write_bytes(buf)
print("write_bytes: ok")
dst^.close()Output:
read_bytes(2500 MiB) error: Failed to read from file: Invalid argument
read_bytes() error: Failed to read from file: Invalid argument
write_once error: Failed to write to file: Invalid argument
write_all error: Failed to write to file: Invalid argument
ABORT: Mojo/stdlib/std/io/file.mojo:586:22: write() syscall failed
System information
- macOS 27.0 (26A428), Apple M5 Pro (arm64)
- Mojo 1.3.0.dev2026111316 (82498b3d). The stdlib on main at e6e596c has the same code.
Assisted-by: AI
Activity
NathanSWard commentedon Nov 15, 2026
Duplicate of: #7476
NathanSWard commentedon Nov 15, 2026
Ahh apologies, #7476 is for sockets specifically, this is for
FileHandle[External] [stdlib] Read and write files over 2 GiB on macOS (#112283)