You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
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.
Another writer's bytes are overwritten. A stream opened "r+" or "a" and closed untouched puts back the content it read at open.
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 writerf.close# nothing was written through fpFile.read(path)
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.
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.
Activity
vm: IO#pwrite and IO#pread reach the file, not the stream's buffer (#970
vm: keep the special mode bits, and bound positional offsets (#975)