Skip to content

fix(io): flush stdout before native IO jobs - #2155

Merged
nicolas-abril merged 4 commits into
bendlang:mainfrom
toprameen:flush-stdout-before-native-io
Nov 14, 2026
Merged

nicolas-abril merged 4 commits into
bendlang:mainfrom
toprameen:flush-stdout-before-native-io

Conversation

@toprameen

Copy link
Copy Markdown
Contributor

On the native runtime, IO.print and IO.write can leave output buffered until the event loop next polls. A later effect in the same fiber can start on a helper thread before that flush. A file write to stdout can then appear ahead of earlier output, or a subprocess can read redirected output before the earlier text reaches the file.

The runtime now flushes stdout before handing a job to a helper thread. The regression keeps another fiber runnable so the helper can finish before the next poll, then checks that stdout still follows program order. It fails on upstream and passes with the patch on macOS and Linux arm64.

@toprameen
toprameen marked this pull request as ready for review November 13, 2026 19:23

@YidaWeng YidaWeng 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.

The C lane reproduces the bug on main: reverting the io_sync() line gives two / one / three; with it, one / two / three. The JS lane is unchanged. bun gates/repo.ts is 56/56 and comp.ts is 65,970 / 66,000.

The test is a real regression, and the fix is in the right place: flush the held stdout before the native job is queued. The one tradeoff is that fflush(stdout) now runs before every native job, not only jobs that can write to stdout. Since io_sync() is a no-op when nothing is held and this keeps the ordering rule simple, I think it is reasonable. Narrowing it later would mean passing the effect id through IoWork; not necessary here.

io_work flushed stdout before every helper job, Random.u32 included. The
flush is now each effect's own, before io_work: File.open, File.read,
File.size, File.write, File.write_bytes, File.write_at and Process.run,
the jobs that can reach stdout's destination (stdout redirected to the
file, or inherited by the process). EFFECTS.md says so for new effects.

The test fails when the write it races is not running (it checked
nothing then), and waits for it with no deadline.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks! I pushed dfeec5a to narrow the flush to the jobs where the order can be seen:

  • Fewer flushes. io_work no longer flushes stdout before every helper job. The flush is each effect's own, right before io_work, in File.open, File.read, File.size, File.write, File.write_bytes, File.write_at and Process.run. Those are the jobs that can reach stdout's destination: stdout redirected to that file, or inherited by the child. Random.u32 (and any job like it) keeps stdout buffered. guide/EFFECTS.md now says that an effect whose job can reach stdout's destination calls io_sync() first.
  • comp.ts shrinks. Since the flush lives in the effects, comp.ts is one line shorter than main, and gates/repo.ts passes 56/56.
  • The test can't pass vacuously. It now fails if the write it races isn't running, because before, that case passed without checking anything. It also waits for the write with no deadline, so a loaded machine can't time it out.

Checked on an M4 Max: the test prints one two three five runs out of five with the change, and two one three on main. The interpreter and the JS lane print in order. All 237 tests under tests/io or using File./Process.run print the same as on main.

The longer-term plan is in #2160: buffer files and stdout alike behind a manual File.flush. That would replace this automatic flush.

File.write_bytes, File.write_at and Process.run flushed stdout before
their EINVAL checks, so a rejected call flushed for a job it never ran;
the flush now sits next to io_work, as in File.open. The test waits a
minute for the write it races, so a stuck write fails with its message
instead of hanging the shard. EFFECTS.md says why a process job flushes:
the child can read the file stdout is redirected to (its own stdio is
piped, not inherited), and that other io_work jobs keep stdout buffered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Pushed 85420ea with the follow-ups from a second review:

  • Flush only when the job starts. File.write_bytes, File.write_at and Process.run flushed stdout before their EINVAL checks, so a rejected call flushed for a job it never ran. The flush now sits right before io_work in each of them, as it already did in File.open. Effects that go through io_work without touching stdout's destination (Random.u32, and external effects written the same way) keep stdout buffered.
  • The test has a deadline again. It waits a minute for the write it races. A stuck write now fails with "the file write did not finish" instead of hanging the shard, and a merely slow one still passes.
  • A correction. In my last comment I said Process.run needs the flush because the child inherits stdout. That's wrong: process_run pipes all three of the child's streams. The real reason is that the child can read the file stdout is redirected to (e.g. cat out.txt). guide/EFFECTS.md now says so, and Buffered file and stdout writes with a manual File.flush #2160 is corrected too.

The test still prints one two three five runs out of five, and the file and process tests print as on main.

@nicolas-abril
nicolas-abril merged commit f0cf0a7 into bendlang:main Nov 14, 2026
1 check passed
nicolas-abril added a commit that referenced this pull request Nov 16, 2026
…elds every 64

The buffer change rewrote file_read.c from a copy that predated #2155, so
file_read_start lost its io_sync(): a read that blocks while another fiber
keeps the loop busy no longer flushed the text IO.print held.
file_read_line_stdout_order checks it with an interposed read on a handle
opened before the print.

A line whole in the buffer still answers on the loop, but only 64 in a
row: the next goes through the helper, so the loop polls its timers and
parked work (guide/EFFECTS.md). It allocates the line, not max + 1 bytes,
and scans for '\n' once.

Before a write or close, the seek back runs on any fd that seeks (a block
device too, not only a regular file) and drops the buffer only when the
seek lands where it was asked; a terminal keeps it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nicolas-abril added a commit that referenced this pull request Nov 16, 2026
…ered line inline (#2178)

On the C lane, File.read_line made one read(2) per byte. A handle that read_line reads now keeps a 16 KiB buffer of its own, filled by one read(2) with what is there, and a line already whole in the buffer answers on the loop straight from it. A read that reaches the fd flushes stdout first (#2155). File.read and File.read_bytes answer buffered bytes first; a write and File.close seek back to the end of the last line on an fd that seeks. Supersedes #2157.

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.

3 participants