Repository navigation
fix(io): flush stdout before native IO jobs - #2155
Conversation
YidaWeng
left a comment
There was a problem hiding this comment.
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]>
|
Thanks! I pushed dfeec5a to narrow the flush to the jobs where the order can be seen:
Checked on an M4 Max: the test prints The longer-term plan is in #2160: buffer files and stdout alike behind a manual |
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]>
|
Pushed 85420ea with the follow-ups from a second review:
The test still prints |
…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]>
…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]>
On the native runtime,
IO.printandIO.writecan 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.