Skip to content

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 #969

Closed

Description

@tannevaled

Closing a writable File stream writes its whole buffer back by path, even when nothing was written through it. The buffer still holds whatever the file contained at open. Two consequences follow, and MRI 4.0.7 has neither.

  1. Another writer's bytes are overwritten. A stream opened "r+" or "a" and closed untouched puts back the content it read at open.
  2. A file unlinked while open comes back. The flush re-creates it at its old path.

Measured

rbgo built from main at c5b3574, compared with MRI 4.0.7 (+PRISM, arm64-darwin25) using rubydiff:

File.write(path, "0123456789")
f = File.open(path, "r+")      # or "a"
File.write(path, "NEW")         # another writer
f.close                         # nothing was written through f
p File.read(path)
"r+" "a"
MRI 4.0.7 "NEW" "NEW"
rbgo "0123456789" "0123456789"
File.write(path, "0123456789")
f = File.open(path, "r+")
File.unlink(path)
f.close
p File.exist?(path)
untouched "r+" "r+" after f.write("Q") "r"
MRI 4.0.7 false false false
rbgo true true false

Where

ioFlush (internal/vm/io.go) checks only o.writable && o.path != "", then runs os.WriteFile(o.path, o.buf, …). The stream already tracks pending bytes in wbufDirty, which stands in for MRI's fptr->wbuf.len, but the flush does not consult it. In MRI, io_fflush with an empty write buffer is a no-op, so a close with nothing pending touches no byte of the file.

Shape of a fix (not measured yet)

Gating the flush on wbufDirty is the obvious step. Before it can be trusted, every mutation of buf that must reach the disk has to set the flag: #write and its siblings, #truncate, #pwrite's buffer fallback, the truncation done by "w" at open, and whatever #reopen does. A gate on a flag that one door forgets to set turns this bug into data loss. TestEveryWayToGetAWritableFileFlushesAtShutdown lists the doors for the teardown flush and is the model to follow.

The unlinked case is the same root as #635 (streams are buffered by path and hold no descriptor). A gated flush fixes the untouched row, but not the row where the stream was written to.

Found while measuring #949.

Refs #949, #635, #643

🤖 Generated with Claude Code

Prior art: priorart "close rewrites an untouched file from its buffer" at 2026-11-21 17:50 CET reported prior art; checked: priorart 'close rewrites an untouched file from its buffer' at 17:50 found 749 entries; read the top ones: #949 is pwrite immediacy (this was found measuring it), #635 is File#stat on an unlinked file (same root, different symptom), #643 is the teardown flush that makes bytes survive — none reports a close with NOTHING written overwriting another writer. The five branches it named are older feature branches about File.open/read, not flush.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions