Repository navigation
fix(io): File.read fills its buffer instead of answering one read(2) - #1845
Merged
Merged
Conversation
File.read now fills max bytes, so a terminal or a pipe needs a read that answers each line as it comes: File.read_line reads a byte at a time up to its "\n" (kept), max or EOF, and answers "" only at the end. The C lane shares one loop for read, read_at and read_line. Both lanes retry EINTR and answer the bytes already read when an error follows. The FIFO test checks its setup and reaps its writer. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Lorenzobattistela
approved these changes
Oct 26, 2026
This was referenced Oct 31, 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.
File.read/File.read_bytes/File.read_atmade exactly oneread(2)/pread(2)call, so a pipe, a terminal or any short read answered fewer bytes than the caller asked for even when the file had more. The cache loader in MLBend hit this:File.read(f, 6)over{ printf abc; sleep 1; printf def; } | proganswered 3 bytes.This fills the buffer: the effect loops until
maxbytes or EOF, retriesEINTR, and returns the bytes already read if an error comes after a partial read. Both the C and JS lanes are changed, andFile.read_atfollows the same rule. This is the fread / Pythonf.read(n)behaviour for a file loader.Since a stream read now waits for
max/EOF, interactive and line-based input gets its own effect:It reads a byte at a time up to its
"\n"(kept),maxor EOF, so nothing past the line leaves the file, and answers""only at the end of the file (an empty line is"\n"; a line longer thanmaxcontinues on the next call). On(printf 'line1\n'; sleep 2; printf 'line2\n') | progit answersline1at once andline2two seconds later, on C and JS.Changes:
bend2/effs/file_read.c: one fill loop (file_read_fill) for read, read_bytes, read_at and read_line; a zeromaxstill makes its syscall, so a bad handle still fails.bend2/effs/file_read.js: the same loop; EINTR retried and partial bytes kept on a later error, as in C.bend2/base.bend:File.read_line, and the fill rule documented onFile.read.tests/io/file_read_short.{bend,c,js}: a FIFO writer sends sixteen bytes one at a time;File.read(f, 16)answers all sixteen (one before the change). The writer is a reaped grandchild and the FIFO setup is checked.tests/io/file_read_line.bend: lines, an empty line, a line cut atmax, a last line without"\n", and""at EOF.Tested, merged onto main 79642f1: repo 54/54, ping 81/81, test 1710/1710, perf 123/124 (the
bfsGPU cell fails the same on main), safe 1112 agree / 0 false reject.🤖 Generated with Claude Code