diff --git a/internal/vm/io_dirty_fsize_internal_test.go b/internal/vm/io_dirty_fsize_internal_test.go index 0ffd1ed1..f9c097f1 100644 --- a/internal/vm/io_dirty_fsize_internal_test.go +++ b/internal/vm/io_dirty_fsize_internal_test.go @@ -7,33 +7,78 @@ package vm import ( + "flag" + "os" + "os/exec" "path/filepath" + "strings" "syscall" "testing" ) +// fsizeChildEnv marks the copy of the test binary that runs the limited flush. +const fsizeChildEnv = "RBGO_FSIZE_CHILD" + // TestAFlushPastTheFileSizeLimitRaises: the file opened, and the write itself // was refused. With RLIMIT_FSIZE below the span's offset, write(2) answers // EFBIG (the Go runtime ignores SIGXFSZ, so the process survives it). This is // TestAFlushThatCannotWriteRaises for platforms with no /dev/full: the error // must be raised, not dropped while Close reports success. +// +// The limit is set in a CHILD copy of the test binary, never in this one. +// RLIMIT_FSIZE is per process: lowered here, it bound every file the other +// tests and the testing package wrote meanwhile, and the arm64 lane failed +// with "can't write .../testlog.txt: file too large" after every test had +// passed (go-embedded-ruby/ruby#982's first run). func TestAFlushPastTheFileSizeLimitRaises(t *testing.T) { - path := filepath.Join(t.TempDir(), "f") + if os.Getenv(fsizeChildEnv) == "1" { + fsizeChild() + return + } + args := []string{"-test.run=^TestAFlushPastTheFileSizeLimitRaises$"} + // Under -cover the child's counters must land where this process's do: + // the testing package merges every counter file in -test.gocoverdir into + // the profile, and the EFBIG arm of writeDirty is reached ONLY in the + // child on macOS (no /dev/full). Without this the coverage ratchet saw the + // arm as unreached (go-embedded-ruby/ruby#983's macos lane). + if f := flag.Lookup("test.gocoverdir"); f != nil && f.Value.String() != "" { + args = append(args, "-test.gocoverdir="+f.Value.String()) + } + cmd := exec.Command(os.Args[0], args...) + cmd.Env = append(os.Environ(), fsizeChildEnv+"=1", "TMPDIR="+t.TempDir()) + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("child: %v\n%s", err, out) + } + switch got := string(out); { + case strings.Contains(got, "fsize: skip"): + t.Skipf("child could not arrange the limit:\n%s", got) + case !strings.Contains(got, "fsize: raised Errno::EFBIG\n"): + t.Errorf("a flush past RLIMIT_FSIZE did not raise Errno::EFBIG; child said:\n%s", got) + } +} + +// fsizeChild runs in the child: it lowers its own RLIMIT_FSIZE, flushes, and +// prints the class raised. Nothing else runs in this process. +func fsizeChild() { + path := filepath.Join(os.TempDir(), "f") o := &IOObj{isStr: true, writable: true, dirty: []ioSpan{{8, 12}}, path: path, buf: []byte("0123456789ab")} var old syscall.Rlimit if err := syscall.Getrlimit(syscall.RLIMIT_FSIZE, &old); err != nil { - t.Skipf("getrlimit: %v", err) + os.Stdout.WriteString("fsize: skip getrlimit: " + err.Error() + "\n") + return } - limited := old - limited.Cur = 4 + limited := syscall.Rlimit{Cur: 4, Max: old.Max} if err := syscall.Setrlimit(syscall.RLIMIT_FSIZE, &limited); err != nil { - t.Skipf("setrlimit: %v", err) + os.Stdout.WriteString("fsize: skip setrlimit: " + err.Error() + "\n") + return } got := catchRaise(func() { ioFlush(o) }) + // Restore it before anything else writes: the child still has its own + // files to write on the way out (the coverage data, under -cover, which + // is what failed on the CI lanes while the flush had raised EFBIG). if err := syscall.Setrlimit(syscall.RLIMIT_FSIZE, &old); err != nil { - t.Fatalf("restoring RLIMIT_FSIZE: %v", err) - } - if got != "Errno::EFBIG" { - t.Errorf("a flush past RLIMIT_FSIZE raised %q, want Errno::EFBIG", got) + os.Stdout.WriteString("fsize: restoring RLIMIT_FSIZE: " + err.Error() + "\n") } + os.Stdout.WriteString("fsize: raised " + got + "\n") }