Skip to content

vm: flush only pending bytes, so closing an untouched File writes nothing - #971

Merged
tannevaled merged 2 commits into
mainfrom
close-untouched-file
Nov 21, 2026
Merged

tannevaled merged 2 commits into
mainfrom
close-untouched-file

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Summary

Closes #969.

ioFlush wrote 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 #ungetc pushed back. A stream opened r+, a, a+ or w+ and closed without a write therefore:

  • put its open-time snapshot back over another writer's bytes;
  • re-created a file unlinked while it was open;
  • wrote #ungetc's pushed-back bytes into the file.

MRI does none of these. io_fflush with an empty write buffer writes nothing.

Change

  • ioFlush returns unless wbufDirty is set. The flag stands in for fptr->wbuf.len and already gated the teardown flush (flushOpenFiles).
  • writeBytes now sets the flag before a sync = true flush. That flush used to skip the flag, so without this change the new gate would have dropped sync writes.
  • I checked every other mutation of buf and every caller of ioFlush:
    • Opens that truncate ("w", O_TRUNC, #reopen in a write mode) and #truncate act on disk immediately and don't depend on the flush.
    • #reopen flushes the old stream before it moves.
    • The other callers (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 holds 0123456789:

case MRI 4.0.7 rbgo before rbgo after
untouched r+, another writer wrote NEW "NEW" "0123456789" "NEW"
untouched a, same "NEW" "0123456789" "NEW"
untouched a+, same "NEW" "0123456789" "NEW"
untouched w+, same "NEW" "" "NEW"
untouched r+, path unlinked, File.exist? after close false true false
ungetc("X"), then close "0123456789" "X0123456789" "0123456789"
write("AB"), then close "AB23456789" same same
sync = true; write("CD"), before close "CD23456789" same same
"a", write("Z"), flush "0123456789Z" same same
File.open(p, "w").close "" same same
truncate(4), then close "0123" same same

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

  • TestClosingAStreamWritesOnlyWhatWasWritten covers all 11 rows, each pinned to MRI's output.
  • TestIOFlushError now checks each clause of the guard separately with catchRaise. The first sweep caught the mutant only through a panic.
  • mutsweep v0.12.0 on io.go:1284: caught by an assertion.
  • The full internal/vm suite is green locally (231 s), and gofmt -l is clean.

Relation to #970

Refs #970 (IO#pwrite reaches 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 in internal/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 an r+ stream could also corrupt the file.

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 21, 2026 18:26
…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>
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.

Closing a writable File that was never written to rewrites it from its buffer: another writer's bytes are lost, an unlinked file comes back

1 participant