Repository navigation
vm: flush only pending bytes, so closing an untouched File writes nothing - #971
Merged
Merged
Conversation
…hing
ioFlush wrote the whole buffer back by path whenever the stream was
writable. The buffer also holds what the file contained at open, plus any
bytes #ungetc pushed back. So an r+/a/a+/w+ stream closed without a write
put its open-time snapshot back over another writer's bytes, re-created a
file unlinked while it was open, and wrote #ungetc's pushed-back bytes
into the file. MRI does none of this: io_fflush with an empty write buffer
writes nothing.
ioFlush now returns unless wbufDirty is set. That flag stands in for
fptr->wbuf.len and already gated the teardown flush. writeBytes now sets
it before a sync=true flush; that flush used to bypass the flag and would
otherwise have been gated away. Opens that truncate ("w", O_TRUNC) and
#truncate act on disk at once and do not depend on the flush.
Measured with rubydiff against MRI 4.0.7: 6 of 11 probe lines diverged
before this change, none after. The five writes that must still land (a
write, a sync write, an append + flush, "w" with no write, #truncate)
agree before and after. TestClosingAStreamWritesOnlyWhatWasWritten pins
all 11 rows.
Closes #969
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mutsweep caught the guard's mutant only through a panic: the no-op flushes in TestIOFlushError were called bare, so a write attempted on an unwritable path panicked rather than failed an assertion. Each no-op is now a row checked with catchRaise, and each row is refused by one clause alone: not writable, no path, nothing pending. Refs #969 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 21, 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.
Summary
Closes #969.
ioFlushwrote the stream's whole buffer back by path whenever the stream was writable. That buffer also holds what the file contained at open, plus any bytes#ungetcpushed back. A stream openedr+,a,a+orw+and closed without a write therefore:#ungetc's pushed-back bytes into the file.MRI does none of these.
io_fflushwith an empty write buffer writes nothing.Change
ioFlushreturns unlesswbufDirtyis set. The flag stands in forfptr->wbuf.lenand already gated the teardown flush (flushOpenFiles).writeBytesnow sets the flag before async = trueflush. That flush used to skip the flag, so without this change the new gate would have dropped sync writes.bufand every caller ofioFlush:"w",O_TRUNC,#reopenin a write mode) and#truncateact on disk immediately and don't depend on the flush.#reopenflushes the old stream before it moves.flush,close,fsync,chmod,size,flock,syswrite,write_nonblock) all mean "put what is pending on disk".Measured against MRI 4.0.7
rubydiff(rbgo vs/opt/homebrew/opt/ruby/bin/ruby, 4.0.7+PRISM, arm64-darwin25). The file holds0123456789:r+, another writer wroteNEW"NEW""0123456789""NEW"a, same"NEW""0123456789""NEW"a+, same"NEW""0123456789""NEW"w+, same"NEW""""NEW"r+, path unlinked,File.exist?after closefalsetruefalseungetc("X"), then close"0123456789""X0123456789""0123456789"write("AB"), then close"AB23456789"sync = true; write("CD"), before close"CD23456789""a",write("Z"),flush"0123456789Z"File.open(p, "w").close""truncate(4), then close"0123"Before this change, 6 of 11 rows diverged; now none do. The last five are writes that must still land, so a gate that blocks too much also fails these tests.
Tests
TestClosingAStreamWritesOnlyWhatWasWrittencovers all 11 rows, each pinned to MRI's output.TestIOFlushErrornow checks each clause of the guard separately withcatchRaise. The first sweep caught the mutant only through a panic.io.go:1284: caught by an assertion.internal/vmsuite is green locally (231 s), andgofmt -lis clean.Relation to #970
Refs #970 (
IO#pwritereaches the file). That PR doesn't mark the buffer dirty after a successful direct write. With this gate, a later close therefore doesn't rewrite the file either. The two PRs touch different functions ininternal/vm.Security
This removes a data-integrity hazard. Merely opening a file read-write and closing it, which code that only inspects a file may well do, could overwrite a concurrent writer's content with the stale snapshot. A program that
ungetcs while parsing anr+stream could also corrupt the file.🤖 Generated with Claude Code