What changed: steady.WriteFile, on a write that went through, removes the temp files a crashed or failed write of the same file left beside it - its own .<file>.<digits> shape and edit.WriteAtomic's .<file>.<digits>.tmp alike, both old enough (10 minutes) to belong to no write still going on, and only ones whose remainder is digits, so another file's temps (.models.yml.bak.1.tmp is models.yml.bak's) stand. edit.WriteAtomic reaps through the same function: one reaper, both shapes, every folder magpie writes its own files in. Nothing reaped a steady temp before - edit reaped only its own .tmp shape - so a multi-instance start-up exiting mid-write (the reaping exists beside each write, and nothing swept) left hundreds beside magpie's own files over a run, and every O_EVTONLY descriptor the directory watcher holds per file with them (#4938 Bug 1: 758 temps, 839 descriptors). A write younger than the ten minutes stays, as another write may still be on it; a stalled write that loses its temp here fails its rename and leaves the file as it was. Bug 2 beside this one is yetone'sb295d1196(v0.1.2785); Bug 3 stays open. Run: - go test ./internal/steady -run TestWriteFileReapsStaleTemps -count=1 RED before (the .123 and .789.tmp temps a write leaves behind stay), green after: this package's shape and edit's reaped, a younger one and another file's stay. - go test ./internal/edit -count=1, ./internal/steady -count=1: ok. Generated-by: GLM-5.3-Flash (ZCode) Reviewed and ported: a follow-up tob295d1196(Bug 2), not a duplicate. On mote, with steady.go and edit/file.go at the parent: TestWriteFileReapsStaleTemps FAIL (".quotas.json.123: stays true, want false", ".quotas.json.789.tmp: stays true, want false"); green with the patch. go test ./internal/steady ./internal/edit ok on mote and on Windows (win-run); GOOS=linux/windows/darwin go vet -tags nogui ok. Not reaped (siblings, not in this change): plugin-auth.json's .tmp-<pid> temps and gemini_sigstore's .tmp-* shape. Bug 3 (CPU) stays open. Not tried: a real multi-instance start-up on the reporter's Mac. Co-authored-by: ggbdpq <ggbdpq@gmail.com>
104 lines
3.3 KiB
Go
104 lines
3.3 KiB
Go
package steady
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A file is flushed to the disk before it is renamed over the old one: a
|
|
// Windows machine that went down between the two left logins.json at its
|
|
// full length, every byte zero (#1505). The flush is of the new contents,
|
|
// before the old file is replaced; one that fails leaves the old file and
|
|
// no temp file beside it.
|
|
func TestWriteFileSyncsBeforeRename(t *testing.T) {
|
|
defer func() { Sync = (*os.File).Sync }()
|
|
dir := t.TempDir()
|
|
p := filepath.Join(dir, "logins.json")
|
|
if err := os.WriteFile(p, []byte(`["old"]`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
synced := 0
|
|
Sync = func(f *os.File) error {
|
|
synced++
|
|
if b, _ := os.ReadFile(p); string(b) != `["old"]` {
|
|
t.Errorf("flushed after the rename: the file was already %q", b)
|
|
}
|
|
if b, _ := os.ReadFile(f.Name()); string(b) != `["new"]` {
|
|
t.Errorf("flushed %q, want the new contents", b)
|
|
}
|
|
return f.Sync()
|
|
}
|
|
if err := WriteFile(p, []byte(`["new"]`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if synced != 1 {
|
|
t.Fatalf("flushed %d times, want once", synced)
|
|
}
|
|
if b, _ := os.ReadFile(p); string(b) != `["new"]` {
|
|
t.Fatalf("file = %q", b)
|
|
}
|
|
|
|
Sync = func(*os.File) error { return errors.New("the disk went away") }
|
|
if err := WriteFile(p, []byte(`["newer"]`), 0o600); err == nil {
|
|
t.Fatal("a flush that failed reported as written")
|
|
}
|
|
if b, _ := os.ReadFile(p); string(b) != `["new"]` {
|
|
t.Fatalf("a failed flush changed the file to %q", b)
|
|
}
|
|
if left, _ := os.ReadDir(dir); len(left) != 1 {
|
|
t.Fatalf("a failed write left %v", left)
|
|
}
|
|
}
|
|
|
|
// A write that died between making its temp file and renaming it leaves the
|
|
// temp beside the file: a multi-instance start-up exiting mid-write made
|
|
// hundreds of them beside magpie's own files over a run, and nothing took
|
|
// them — edit.WriteAtomic reaps its temps beside the file it wrote, but
|
|
// steady.WriteFile never did (#4938). A served write reaps the ones old
|
|
// enough to belong to no write still going on, its own shape and edit's
|
|
// alike, and leaves a younger one alone and another file's temp alone.
|
|
func TestWriteFileReapsStaleTemps(t *testing.T) {
|
|
dir := t.TempDir()
|
|
p := filepath.Join(dir, "quotas.json")
|
|
if err := os.WriteFile(p, []byte(`{}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old := time.Now().Add(-time.Hour)
|
|
temps := []struct {
|
|
name string
|
|
stale, stays bool
|
|
}{
|
|
{".quotas.json.123", true, false}, // this package's shape, a crash left it
|
|
{".quotas.json.456", false, true}, // another write may still be on it
|
|
{".quotas.json.789.tmp", true, false}, // edit.WriteAtomic's shape, the same file's
|
|
{".quotas.json.x.012", true, true}, // .quotas.json.x's, not quotas.json's
|
|
}
|
|
for _, tt := range temps {
|
|
f := filepath.Join(dir, tt.name)
|
|
if err := os.WriteFile(f, []byte("half"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tt.stale {
|
|
if err := os.Chtimes(f, old, old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
if err := WriteFile(p, []byte(`{"a":1}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, err := os.ReadFile(p); err != nil || string(b) != `{"a":1}` {
|
|
t.Fatalf("file = %q, %v", b, err)
|
|
}
|
|
for _, tt := range temps {
|
|
_, err := os.Stat(filepath.Join(dir, tt.name))
|
|
if stays := err == nil; stays != tt.stays {
|
|
t.Errorf("%s: stays %v, want %v", tt.name, stays, tt.stays)
|
|
}
|
|
}
|
|
}
|