Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions internal/vm/filestat_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,27 @@ func TestStatGroupsDefault(t *testing.T) {
// TestIOFlushError covers ioFlush's write-failure branch: flushing a writable
// file stream whose path is an unwritable directory raises Errno::ENOENT.
func TestIOFlushError(t *testing.T) {
o := &IOObj{isStr: true, writable: true, path: "/no/such/dir/x.txt", buf: []byte("data")}
o := &IOObj{isStr: true, writable: true, wbufDirty: true, path: "/no/such/dir/x.txt", buf: []byte("data")}
if got := catchRaise(func() { ioFlush(o) }); got != "Errno::ENOENT" {
t.Errorf("ioFlush error: got %q want Errno::ENOENT", got)
}
// A non-writable or path-less stream flushes to a no-op (no raise).
ioFlush(&IOObj{isStr: true, writable: false, path: "/no/such"})
ioFlush(&IOObj{isStr: true, writable: true, path: ""})
// Each of these is a no-op: no write is attempted, so none raises. Every row
// is pending (wbufDirty) except the last, so each one is refused by its own
// clause of the guard alone. The unwritable path makes an attempted write
// visible as Errno::ENOENT rather than as a file appearing somewhere.
noops := []struct {
name string
o *IOObj
}{
{"not writable", &IOObj{isStr: true, writable: false, wbufDirty: true, path: "/no/such/dir/x.txt"}},
{"no path", &IOObj{isStr: true, writable: true, wbufDirty: true, path: ""}},
// #969: nothing pending, so even a writable stream with a path does not
// write. The same stream with wbufDirty set raised above.
{"nothing pending", &IOObj{isStr: true, writable: true, path: "/no/such/dir/x.txt", buf: []byte("data")}},
}
for _, c := range noops {
if got := catchRaise(func() { ioFlush(c.o) }); got != "" {
t.Errorf("%s: ioFlush attempted a write and raised %q", c.name, got)
}
}
}
17 changes: 14 additions & 3 deletions internal/vm/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,11 @@ func (o *IOObj) writeBytes(p []byte) int {
copy(o.buf[o.pos:], p)
o.pos += len(p)
o.syncStr()
if o.path != "" {
o.wbufDirty = true // bytes accepted but not on disk: MRI's fptr->wbuf.len
}
if o.sync { // sync=true: a File's writes reach disk immediately (no buffering)
ioFlush(o)
} else if o.path != "" {
o.wbufDirty = true // bytes accepted but not on disk: MRI's fptr->wbuf.len
}
return len(p)
}
Expand Down Expand Up @@ -1269,8 +1270,18 @@ func notRegular(p string) bool {
// permission puts the owner-write bit back for the length of the write and
// restores the file's mode afterwards. That is the descriptor's grant, replayed;
// it never widens a file this stream was not already entitled to write.
//
// Only PENDING bytes are written (#969). The buffer also holds what the file
// contained at open, plus any bytes #ungetc pushed back, and neither belongs on
// disk: MRI's io_fflush with an empty write buffer writes nothing. Before this
// gate, a stream opened "r+" or "a" and closed untouched put its open-time
// snapshot back over another writer's bytes, and re-created a file unlinked
// while it was open. wbufDirty stands in for fptr->wbuf.len, and every write
// into the buffer that must reach the disk sets it (writeBytes, and #pwrite's
// buffer fallback); opens that truncate do so on disk at once and need no
// flush.
func ioFlush(o *IOObj) {
if !o.writable || o.path == "" {
if !o.writable || o.path == "" || !o.wbufDirty {
return
}
o.wbufDirty = false
Expand Down
54 changes: 54 additions & 0 deletions internal/vm/io_close_untouched_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm_test

import (
"fmt"
"path/filepath"
"testing"
)

// TestClosingAStreamWritesOnlyWhatWasWritten pins #969: a flush or close puts
// PENDING bytes on disk and nothing else. Every want is MRI 4.0.7's output for
// the same program (arm64-darwin25, rubydiff, 2026-11-21). Before the fix rbgo
// diverged on the first six rows, which wrote the stream's open-time snapshot
// (or a byte #ungetc pushed back) over the file. The rows after them are the
// writes that MUST still land, so a gate that stops too much goes red too.
func TestClosingAStreamWritesOnlyWhatWasWritten(t *testing.T) {
p := filepath.ToSlash(filepath.Join(t.TempDir(), "f"))
reset := fmt.Sprintf("File.write(%q, \"0123456789\"); ", p)
read := fmt.Sprintf("p File.read(%q)", p)
cases := []struct{ name, src, want string }{
{"untouched r+ keeps another writer's bytes",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); File.write(%q, "NEW"); f.close; `, p, p) + read, "\"NEW\"\n"},
{"untouched a keeps another writer's bytes",
reset + fmt.Sprintf(`f = File.open(%q, "a"); File.write(%q, "NEW"); f.close; `, p, p) + read, "\"NEW\"\n"},
{"untouched a+ keeps another writer's bytes",
reset + fmt.Sprintf(`f = File.open(%q, "a+"); File.write(%q, "NEW"); f.close; `, p, p) + read, "\"NEW\"\n"},
{"untouched w+ keeps another writer's bytes",
reset + fmt.Sprintf(`f = File.open(%q, "w+"); File.write(%q, "NEW"); f.close; `, p, p) + read, "\"NEW\"\n"},
{"untouched r+ does not bring back an unlinked file",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); File.unlink(%q); f.close; p File.exist?(%q)`, p, p, p), "false\n"},
{"ungetc is never written to the file",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.ungetc("X"); f.close; `, p) + read, "\"0123456789\"\n"},
{"a write still lands at close",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.write("AB"); f.close; `, p) + read, "\"AB23456789\"\n"},
{"a sync write lands before close",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.sync = true; f.write("CD"); `, p) + read, "\"CD23456789\"\n"},
{"an append lands at flush",
reset + fmt.Sprintf(`f = File.open(%q, "a"); f.write("Z"); f.flush; `, p) + read, "\"0123456789Z\"\n"},
{"w truncates with no write",
reset + fmt.Sprintf(`File.open(%q, "w").close; `, p) + read, "\"\"\n"},
{"truncate lands with no write",
reset + fmt.Sprintf(`f = File.open(%q, "r+"); f.truncate(4); f.close; `, p) + read, "\"0123\"\n"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q\n got=%q\nwant=%q", c.src, got, c.want)
}
})
}
}
Loading