Skip to content

fix(io): File.read and its twins read through fs on Node, where bun:ffi is missing - #1881

Merged
Lorenzobattistela merged 3 commits into
bendlang:mainfrom
sebastianbreguel:fix/file-read-no-ffi
Oct 27, 2026
Merged

Lorenzobattistela merged 3 commits into
bendlang:mainfrom
sebastianbreguel:fix/file-read-no-ffi

Conversation

@sebastianbreguel

Copy link
Copy Markdown
Contributor

File.read, read_line, read_bytes and read_at end the run on Node, because file_read.js asks io_sys() for libc's read/pread, and that needs bun:ffi:

$ node r.js     # File.open + File.read(f, 100) on a 5-byte file
Error: Cannot find module 'bun:ffi'

This is the same kind of bug as #1468, but for reads. When io_sys() throws, the reads now go through fs.readvSync, and the errno comes from the thrown error. On Bun nothing changes: the FFI path is the same code. I used readvSync and not readSync because readSync skips the syscall for a 0-byte read, so File.read(h, 0) on a write-only handle would answer "" instead of EBADF. A failed require("bun:ffi") costs about 10 µs on Node (readSync costs about 0.4 µs), so the first failure is cached. Without the cache, 200k small reads took 3.2 s on Node. With it they take about 0.3 s.

tests/io/file_read_no_ffi.{bend,js} follows file_size_no_ffi: the twin makes io_sys() throw, the test reads "hello\nbye" through all four effects, then reads 64 bytes and 0 bytes from an append-only handle (both fail 9). On main it prints Error: no ffi table on this host (exit 1). With the fix it passes on interp and JS. The same .bend without the twin also prints the expected lines under real Node 25.9.

  • The 20 file IO tests that use the read effects, plus file_size_no_ffi and tcp_connect_no_ffi, pass on interp, JS and C: 57/57. I ran them locally, not on the minis.
  • gates/repo.ts: 54/54.
  • Perf on Bun, 20 interleaved fresh-process runs each, main vs this branch, all within noise:
    • one 50 MB File.read: p50 going from 22.2 to 21.8 ms, p95 from 25.4 to 22.9 ms, peak RSS 122.8 MB on both
    • 200k File.read(f, 16): p50 going from 201.9 to 201.0 ms, p95 from 203.7 to 205.1 ms, CPU 0.29 s and RSS 51.2 MB on both
    • Node, new: 43.9 ms and 316.8 ms p50 for the same two

The TCP/UDP effects still need bun:ffi on Node, as on main. I left them alone.

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 27, 2026
Lorenzobattistela and others added 2 commits October 27, 2026 11:00
… FFI path

readvSync makes the syscall for a 0-byte read, the case the FFI read was
added for (Bun's readSync skips it), so the reads need no bun:ffi on Bun
either: one fs path, no fallback flag, no second errno source. libuv
retries EINTR, so the loop drops its own retry.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
libuv's sync read does not retry an interrupted read, so readvSync can
throw EINTR; the loop retries it, as file_read.c does.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Lorenzobattistela
Lorenzobattistela merged commit 5396ca4 into bendlang:main Oct 27, 2026
1 check passed
nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Nov 5, 2026
… park case

The JS io_sys bound pread for the old file_read.js, which moved to
readvSync in bendlang#1881. ALC's extra row was for the old ALC[1 + arg] indexing;
the main thread uses ALC[0] and workers ALC[1..pool_size-1] since bendlang#1269.
io_park_add's w->next == w case writes what the next line writes anyway.

Co-Authored-By: Claude Opus 5.5 <[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