Skip to content

fix(io): File.read fills its buffer instead of answering one read(2) - #1845

Merged
Lorenzobattistela merged 4 commits into
bendlang:mainfrom
adeerkhan:fix/file-read-all
Oct 26, 2026
Merged

Lorenzobattistela merged 4 commits into
bendlang:mainfrom
adeerkhan:fix/file-read-all

Conversation

@adeerkhan

@adeerkhan adeerkhan commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

File.read/File.read_bytes/File.read_at made exactly one read(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; } | prog answered 3 bytes.

This fills the buffer: the effect loops until max bytes or EOF, retries EINTR, and returns the bytes already read if an error comes after a partial read. Both the C and JS lanes are changed, and File.read_at follows the same rule. This is the fread / Python f.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:

def File.read_line(file: File, max: U32) ->
  IO(File & Result<&1, &1, U32 & String, String>)

It reads a byte at a time up to its "\n" (kept), max or 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 than max continues on the next call). On (printf 'line1\n'; sleep 2; printf 'line2\n') | prog it answers line1 at once and line2 two 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 zero max still 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 on File.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 at max, 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 bfs GPU cell fails the same on main), safe 1112 agree / 0 false reject.

🤖 Generated with Claude Code

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 26, 2026
Lorenzobattistela and others added 2 commits October 26, 2026 07:13
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants