Repository navigation
vm: IO#pwrite and IO#pread reach the file, not the stream's buffer - #970
Merged
Merged
Conversation
MRI's rb_io_pwrite and rb_io_pread are pwrite(2) and pread(2) on the descriptor. rbgo's File streams are buffer-backed and wrote and read only the buffer, so a pwrite was not on disk until a flush and a pread could not see another writer. Both now reach the regular file by its path: - pwrite writes its own range with WriteAt and nothing else. The stream's other pending writes stay pending; MRI does the same (disk "01234AB789", not "QQ234AB789"). On success it no longer marks the buffer dirty. A permission refusal is retried with the owner-write bit restored, as ioFlush does, so a stream created 0444 writes through its grant. Before, those bytes never reached the disk. - pread flushes this stream's pending writes first, as rb_io_pread does, then reads with ReadAt. The allocation is bounded by the file's size, not by the length Ruby asked for, so f.pread(2**40, 0) returns at once (MRI 4.0.7 did not answer within 30 s). A stream with no reachable regular file (a StringIO, a pipe, a popen stream, a device, an unlinked path) falls back to the buffer, as before. Measured with rubydiff against MRI 4.0.7: the probe diverged on 6 of 11 lines before this change and on none after. TestPositionalIOReachesTheFile pins those rows. Found while measuring: closing a writable stream that was never written to rewrites the file from its buffer (#969). That is a separate defect and is not touched here. Closes #949 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s it
mutsweep on the first commit caught 5 of 12 mutants; 7 survived. This
commit fixes them in two ways.
The code is restructured so that no line is a mutant nobody can tell
apart:
- The permission retry is now `if errors.Is(err, ErrPermission)`, and the
0444 row holds it. The old `if !errors.Is(...) return` was a fast path
whose mutant only repeated a successful write.
- preadFile reads with io.ReadAll over an io.SectionReader instead of
Stat-then-allocate. That removes an `f.Stat` failure on an open file
that nothing can provoke, and the allocation stays bounded by what the
file holds.
- ioPositionalPath lets os.Stat("") refuse an empty path, instead of a
second guard for the same case.
Package-internal tests cover the remaining refusals: a pipe or popen
stream over a regular path, a directory, a missing path, a retry whose
mode cannot be read, a write that fails after a successful open, and the
EOF / error / data answers of preadFile.
Refs #949
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When os.Stat cannot read the mode, writeAtWithOwnerWrite must return the original refusal. Its mutant was caught only by a nil-pointer panic. The test now recovers that panic and fails with an assertion, and mutsweep reports the line "caught", not "caught by PANIC". Refs #949 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CI's per-function coverage ratchet failed on both POSIX lanes: - defIOSeekable 99.1%: the buffer's own end-of-file check in #pread is reached only when the file cannot be read by path, and no test did that. Two rows now do, each matching MRI 4.0.7 under rubydiff: a pread on a path made unreadable after open (falls back to the buffer, "234"), and a pread past the end of an unlinked file (EOFError). - writeAtWithOwnerWrite 88.9%: the "chmod failed" branch could not be provoked by a test, and it was redundant. When the mode cannot be changed, the retried write is refused for permission again and that refusal is returned. The branch is gone; the chmod's error is ignored with that stated. Refs #949 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 #949.
In MRI,
IO#pwriteandIO#preadarepwrite(2)andpread(2)on the descriptor. rbgo's File streams are buffer-backed and hold no descriptor, so both methods wrote and read only the in-memory buffer.Both now reach the regular file by its path:
#pwritewrites its own range withWriteAtand nothing else. The stream's other pending writes stay pending, as in MRI. A permission refusal is retried with the owner-write bit restored for the length of the write (the same grantioFlushreplays), so a stream created0444writes through.#preadfirst flushes this stream's own pending writes, asrb_io_preaddoes, then reads withio.ReadAll(io.NewSectionReader(…)). The allocation is bounded by what the file holds, not by the length Ruby asks for.Measured against MRI 4.0.7
rubydiff(rbgo vs/opt/homebrew/opt/ruby/bin/ruby, 4.0.7+PRISM, arm64-darwin25). The file holds0123456789:pwrite("AB", 0), thenFile.read"AB23456789""0123456789""AB23456789"pread(3, 0)"xyz""012""xyz"write("QQ"),pwrite("AB", 5), thenFile.read"01234AB789""0123456789""01234AB789"pwrite("Z", 12)"0123456789\u0000\u0000Z""0123456789"File.new(p, "w", 0444), thenpwrite("AB", 0)"AB", mode444"", mode444"AB", mode444write("QQ"), thenpread(2, 0)"QQ""QQ""QQ"read(3), thenpwrite3,"34"pwrite/preadon a read-only or write-only streamIOErrorBefore this change the probe diverged on 6 of 11 output lines. After it, it diverges on none.
One deliberate divergence: on a 10-byte file,
f.pread(2**40, 7)returns"789"at once in rbgo. MRI 4.0.7 did not answer within 30 s, because it allocates the full length first.Tests
TestPositionalIOReachesTheFilehas 8 rows, each pinned to MRI's measured output.TestPreadLengthIsBoundedByTheFilecovers the bound.internal/vmsuite is green locally (367 s), as aregofmt -landgo vet.Docs
The README's divergence row is rewritten. What remains is a path that no longer names the opened file: an unlinked path falls back to the buffer, where MRI still reaches the old inode, and a replaced path reaches the new file.
Found while measuring, not fixed here
Refs #969: closing a writable stream that was never written to rewrites the file from its buffer. Another writer's bytes are lost, and an unlinked file comes back. It predates this change; the measurement and the shape of a fix are in the issue.
Security
preadcomes from Ruby and no longer sizes an allocation.ioFlushalready applies.ioFlushalready has: a path swapped for another file between open andpwriteis written. A per-stream descriptor would remove it, which is the root of Refs File::Stat: no atime/ctime on Windows, and File#stat cannot answer for an unlinked open file #635.🤖 Generated with Claude Code