provider: a save of the bytes the file already holds changes nothing (#6289) (KdhsiiKshshhs on X)
writeLogins and writePrivate rewrote logins.json with identical bytes, which moved its mtime and called filememo.Forget, so every read after parsed the 230 KB file again (anton1615 measured 182 reads in 15 s, CPU 3.3x after each write). Now a file that holds exactly these bytes, at the mode the write would give it, is left alone: no lastgood generation, no rename, no Forget. A stat or read that fails is "not equal" and writes as before. Real changes keep steady.WriteFile's temp-and-rename. The same check is in writePrivate, writePrivateQuiet and (writeChanged) the sibling writers of codex-autoreset, codex resets-used and Kiro's token file. Nothing else bumped the mtime in the reporter's path: the other writers of logins.json (rememberLogins, Claude/Codex sign-in syncs) all go through writeLogins. Changed() still runs on a told save, so a list built before it is not reused (TestLoginsListedOnceARequest unchanged). Run on mote: TestUnchangedLoginsSaveKeepsFileAndParse and TestWritePrivateSameBytesLeavesFile, -count=20 green; both red with fileHolds forced to return false (mtime moved). gofmt clean; go vet on darwin, linux nogui, windows nogui; go test -tags nogui ./internal/provider ./internal/filememo ./internal/lastgood (result below in the push notes). Not tried: the reporter's 130-account gateway; the full suite before the tag.
This commit is contained in:
@@ -23,7 +23,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
"github.com/yetone/magpie/internal/steady"
|
||||
)
|
||||
|
||||
// The two times an account the user lets spend its resets by itself
|
||||
@@ -342,7 +341,7 @@ func (a *autoResets) save() {
|
||||
}
|
||||
b, _ := json.MarshalIndent(a.spent, "", " ")
|
||||
_ = os.MkdirAll(settings.Dir(), 0o755)
|
||||
if err := steady.WriteFile(a.path, append(b, '\n'), 0o644); err != nil {
|
||||
if err := writeChanged(a.path, append(b, '\n'), 0o644); err != nil {
|
||||
log.Printf("writing what a week's auto-use spent to %s: %v", a.path, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
|
||||
"github.com/yetone/magpie/internal/filelock"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
"github.com/yetone/magpie/internal/steady"
|
||||
)
|
||||
|
||||
// Why a reset was used: by itself as the account's week was used up and no
|
||||
@@ -105,7 +104,7 @@ func noteResetUsed(user, why string, out ResetOutcome) error {
|
||||
if err := os.MkdirAll(settings.Dir(), 0o755); err != nil {
|
||||
return fmt.Errorf("keeping that a reset was used to %s: %w", resetsUsedPath(), err)
|
||||
}
|
||||
if err := steady.WriteFile(resetsUsedPath(), append(b, '\n'), 0o600); err != nil {
|
||||
if err := writeChanged(resetsUsedPath(), append(b, '\n'), 0o600); err != nil {
|
||||
return fmt.Errorf("keeping that a reset was used to %s: %w", resetsUsedPath(), err)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -42,7 +42,6 @@ import (
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
"github.com/yetone/magpie/internal/proc"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
"github.com/yetone/magpie/internal/steady"
|
||||
)
|
||||
|
||||
// KiroExecutable finds kiro-cli, which refreshes its own sign-in; a var so
|
||||
@@ -466,7 +465,7 @@ func writeFileAtomic(path string, b []byte) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return steady.WriteFile(path, b, 0o600) // on the disk before it is renamed in (#1505)
|
||||
return writeChanged(path, b, 0o600) // on the disk before it is renamed in (#1505)
|
||||
}
|
||||
|
||||
// kiroPost posts to a sign-in endpoint.
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -319,6 +320,15 @@ func writeLoginsTold(ls []savedLogin, tell bool) error {
|
||||
defer Changed() // an account added, switched or gone: All builds anew
|
||||
}
|
||||
beforeLoginsWrite()
|
||||
b = append(b, '\n')
|
||||
if fileHolds(loginsPath(), b, 0o600) {
|
||||
// the bytes are on the disk already (#6289): no new generation, no
|
||||
// new mtime, and what filememo parsed stays
|
||||
lastLoginsMu.Lock()
|
||||
lastLogins, loginsKnown = slices.Clone(ls), true
|
||||
lastLoginsMu.Unlock()
|
||||
return nil
|
||||
}
|
||||
// the file there now is kept first: as its last good generation, or
|
||||
// copied aside when it doesn't read, never written over unkept
|
||||
if err := lastgood.Keep(loginsPath(), validLogins, 0o600); err != nil {
|
||||
@@ -328,7 +338,7 @@ func writeLoginsTold(ls []savedLogin, tell bool) error {
|
||||
if !tell {
|
||||
write = writePrivateQuiet
|
||||
}
|
||||
if err := write(loginsPath(), append(b, '\n')); err != nil {
|
||||
if err := write(loginsPath(), b); err != nil {
|
||||
return err
|
||||
}
|
||||
lastLoginsMu.Lock()
|
||||
@@ -365,6 +375,9 @@ func sortLogins(ls []savedLogin) {
|
||||
// an agent reading it at that moment sees either version, never half; the
|
||||
// new one is on the disk before it is renamed over the old (#1505).
|
||||
func writePrivate(path string, b []byte) error {
|
||||
if fileHolds(path, b, 0o600) { // the file has these bytes (#6289): nothing moves
|
||||
return nil
|
||||
}
|
||||
defer filememo.Forget() // read again, where a request holds it
|
||||
return writePrivateQuiet(path, b)
|
||||
}
|
||||
@@ -372,6 +385,9 @@ func writePrivate(path string, b []byte) error {
|
||||
// writePrivateQuiet is writePrivate with nothing read forgotten: a
|
||||
// request holding the file reads the old bytes for up to a second.
|
||||
func writePrivateQuiet(path string, b []byte) error {
|
||||
if fileHolds(path, b, 0o600) {
|
||||
return nil
|
||||
}
|
||||
path, err := edit.Target(path) // a symlink stays, its target written
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -382,6 +398,33 @@ func writePrivateQuiet(path string, b []byte) error {
|
||||
return steady.WriteFile(path, b, 0o600)
|
||||
}
|
||||
|
||||
// fileHolds reports whether path is a file of exactly b, with the mode
|
||||
// the write would give it. A read that fails is "no": it is written.
|
||||
// writeChanged is steady.WriteFile for a file that is left alone, mtime
|
||||
// and all, when it already holds b (#6289).
|
||||
func writeChanged(path string, b []byte, perm os.FileMode) error {
|
||||
if fileHolds(path, b, perm) {
|
||||
return nil
|
||||
}
|
||||
return steady.WriteFile(path, b, perm)
|
||||
}
|
||||
|
||||
func fileHolds(path string, b []byte, perm os.FileMode) bool {
|
||||
path, err := edit.Target(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil || !fi.Mode().IsRegular() || fi.Size() != int64(len(b)) {
|
||||
return false
|
||||
}
|
||||
if runtime.GOOS != "windows" && fi.Mode().Perm() != perm {
|
||||
return false
|
||||
}
|
||||
old, err := os.ReadFile(path)
|
||||
return err == nil && bytes.Equal(old, b)
|
||||
}
|
||||
|
||||
func upsertLogin(ls []savedLogin, l savedLogin) []savedLogin {
|
||||
l.User = codexName(ls, l)
|
||||
if claudeSignInOfAnother(ls, l) {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
)
|
||||
|
||||
// An unchanged save leaves logins.json alone (#6289): its mtime stays and
|
||||
// what filememo parsed of it is not parsed again.
|
||||
func TestUnchangedLoginsSaveKeepsFileAndParse(t *testing.T) {
|
||||
signIn(t)
|
||||
if err := writeLogins([]savedLogin{{Agent: "codex", User: "me@example.com", Plan: "pro"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Hour).Truncate(time.Second)
|
||||
if err := os.Chtimes(loginsPath(), old, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := filememo.ReadNow("logins", loginsPath(), parseLogins); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writeLogins(readLogins()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fi, err := os.Stat(loginsPath())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !fi.ModTime().Equal(old) {
|
||||
t.Fatalf("an unchanged save moved logins.json's time to %v", fi.ModTime())
|
||||
}
|
||||
parses := 0
|
||||
if _, err := filememo.ReadNow("logins", loginsPath(), func(b []byte) ([]savedLogin, error) {
|
||||
parses++
|
||||
return parseLogins(b)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if parses != 0 {
|
||||
t.Fatalf("an unchanged save made logins.json parse again: %d parses", parses)
|
||||
}
|
||||
// a real change is still written
|
||||
ls := readLogins()
|
||||
ls[0].Order = 7
|
||||
if err := writeLogins(ls); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fi, _ := os.Stat(loginsPath()); fi.ModTime().Equal(old) {
|
||||
t.Fatal("a changed account was not written")
|
||||
}
|
||||
}
|
||||
|
||||
// The siblings written through writePrivate hold still too.
|
||||
func TestWritePrivateSameBytesLeavesFile(t *testing.T) {
|
||||
p := t.TempDir() + "/x.json"
|
||||
if err := writePrivate(p, []byte("{}\n")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Hour).Truncate(time.Second)
|
||||
os.Chtimes(p, old, old)
|
||||
if err := writePrivate(p, []byte("{}\n")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fi, _ := os.Stat(p); !fi.ModTime().Equal(old) {
|
||||
t.Fatal("the same bytes moved the file's time")
|
||||
}
|
||||
if err := writePrivate(p, []byte("{\"a\":1}\n")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b, _ := os.ReadFile(p); string(b) != "{\"a\":1}\n" {
|
||||
t.Fatalf("changed bytes not written: %q", b)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user