Skip to content

IO#pwrite writes the buffer, not the descriptor: the bytes are not on disk until a flush #949

Open

Description

@tannevaled

IO#pwrite and IO#pread are positional operations on a descriptor in MRI:
rb_io_pwrite calls pwrite(2), which by definition does not touch the stream's
buffer or its file position, and the bytes are on disk the moment the call
returns. rbgo's #pwrite writes into IOObj.buf instead, because a
buffer-backed stream holds no descriptor (see IOObj's own comment and
File#stat on an unlinked open file, #635 — same root).

Measured

MRI 4.0.7 (/opt/homebrew/opt/ruby/bin/ruby, +PRISM, arm64-darwin25) against
rbgo at close-ios-at-shutdown. The file holds 0123456789:

f = File.open(path, "r+")
f.pwrite("AB", 0)
puts File.read(path).inspect     # read back from a SECOND stream, same process
what the second read sees
MRI 4.0.7 "AB23456789" — pwrite(2) has already landed
rbgo "0123456789" — the bytes are in the buffer

What #948 does and does not fix

#948 (teardown flush) makes the bytes survive: a program that pwrites and
exits now finds AB23456789 on disk, and the two engines agree on that — it is
one of the 16 differential cases there. So this is no longer data loss.

What remains is the immediacy, and it is observable inside one program by any
second reader of the same path: another File.open, a subprocess, a
File.read, an IO.sysopen, or a peer process waiting on the file. That is the
ordinary reason to reach for #pwrite rather than #seek + #write in the
first place.

#pread has the mirror of this: it reads from buf, so it cannot see bytes a
different writer has put on disk since this stream was opened.

Why it is not a one-line fix

#pwrite could flush after writing, which would make the bytes land
immediately. Two things would have to be decided rather than assumed, and
neither is measured here:

  1. ioFlush writes the whole buffer with os.WriteFile, so a #pwrite
    flush would also push every other unflushed byte of that stream to disk —
    the opposite of pwrite(2), which touches only its own range. For a stream
    with pending buffered writes the result differs from MRI in the other
    direction.
  2. #pread cannot be fixed the same way: seeing another writer's bytes means
    re-reading the file, which is a different stream model, not a flush.

So the honest shape is either a per-range write that bypasses the buffer for
these two methods only, or an acknowledged limit. #948 records the latter in
README.md and leaves this issue as the place the decision belongs.

Refs #948, #643, #635, #593

🤖 Generated with Claude Code

Prior art: priorart "IO#pwrite writes the buffer instead of the descriptor so the bytes are not on disk until a flush" at 2026-11-15 22:31 CET reported prior art; checked: priorart --repo go-embedded-ruby/ruby on this subject at 22:31 returned 567 tracker entries, none about pwrite's immediacy: #593 (merged) IMPLEMENTED pread/pwrite and is the code this reports on; #643 and #948 are the teardown flush, which makes the bytes survive but not land; #635 is the same ROOT (no descriptor) for File#stat, cited here as such. No branch covers it either: the three it names are my own close-ios-at-shutdown.

Activity

  1. added 2 commits that reference this issue on Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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