From 0d58ec8dcbe14be0eaab3af72ea901de5c12af3d Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 12 Oct 2026 18:51:16 +0800 Subject: [PATCH] davsync: a write a server without conditional PUTs let another go over is merged again at the next sync (#1776) On an S3 server that refuses If-Match (Aliyun OSS answers 400 NotImplemented), put falls back to HEAD then an unconditional PUT. Two computers writing in the moment between both pass the HEAD, both are told they wrote, and the later file is kept. Worse, the loser's next sync then took the winner's file as having seen its own: its st.Remote was the file it wrote, so its changed parts looked unchanged here and changed there, and the old parts were brought in over its own changes. The edit was lost on the server and on the computer that made it. - version.Blind marks a write the server didn't check (s3.go's fallback). - state.Blind keeps, for such a write, the file's sum and the remote and local part hashes from before it. The next sync that reads another file than that one merges from before the write. What this computer changed counts as changed again and goes back up beside the other's. A 304 or the same file clears it. Restore and Upload clear it. - docs/reference.md says what happens on such a server. No compare-and-swap exists on OSS, so the race itself isn't closed: the other computer's file is kept for up to one sync interval (3 minutes) before this computer's change is merged back. The reporter's AppendObject journal would close it but needs every computer upgraded and OSS-only signing. If the other computer saw this one's write and then changed the same part again, the next sync can't tell that from a write that went over it. It counts the part as changed on both sides: the newer stays, the other is saved, and a notice says so. Nothing is lost. Run: - TestS3BlindWriteGoneOverIsMergedAgain (fake S3 with no conditional writes, two computers): A renames a provider and B sets the theme, B's PUT goes over A's, then A's next sync puts both on the server with no conflict notice, B brings in the rename, and nothing more is written. With the reset taken out (broken by hand), it fails: "A's rename here after its next sync: \"DeepSeek\"". - go test ./internal/davsync whole. Not tried against real Aliyun OSS: the fake answers conditional PUTs the way the reporter quotes OSS (NotImplemented). --- docs/reference.md | 8 +- internal/davsync/dav.go | 5 ++ internal/davsync/davsync.go | 39 ++++++++- internal/davsync/restore.go | 2 + internal/davsync/s3.go | 7 +- internal/davsync/s3_blind_test.go | 141 ++++++++++++++++++++++++++++++ 6 files changed, 196 insertions(+), 6 deletions(-) create mode 100644 internal/davsync/s3_blind_test.go diff --git a/docs/reference.md b/docs/reference.md index 1ab3c361b..2edcb517f 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -1990,7 +1990,13 @@ For S3: - The access key needs to read and write `/magpie/`. On AWS it also needs to list the bucket. - A server without conditional writes is supported. There magpie checks the - object's ETag just before each write. + object's ETag just before each write. Two computers that write in the + same moment can both pass that check (Aliyun OSS, #1776), and the later + write is the one kept. The other computer notices at its next sync, a + few minutes later. It finds a file other than the one it wrote, so it + merges again from before its own write: what it changed goes back up + beside the other computer's change. It never takes the other's file as + having seen its own. - Shared usage is reconciled only after a complete S3 listing. A missing or repeated continuation token, or a listing still truncated after 100 pages, reports a sync error and keeps previously downloaded usage days in place. diff --git a/internal/davsync/dav.go b/internal/davsync/dav.go index ba142c590..b3a33234f 100644 --- a/internal/davsync/dav.go +++ b/internal/davsync/dav.go @@ -38,6 +38,11 @@ var errNotModified = errors.New("the file on the server is unchanged") type version struct { ETag string `json:"etag,omitempty"` Modified string `json:"modified,omitempty"` + // Blind: a put told it wrote without the server checking the version + // it went over (s3.go's HEAD then PUT, where conditional writes are + // refused): another computer's write in the moment between may have + // gone over it (#1776) + Blind bool `json:"-"` } // conditions are the headers that ask for the file only when it isn't v. diff --git a/internal/davsync/davsync.go b/internal/davsync/davsync.go index 5c0f41203..fdca56bfc 100644 --- a/internal/davsync/davsync.go +++ b/internal/davsync/davsync.go @@ -173,6 +173,25 @@ type state struct { // File is what the server held where the backup should be, when the // last sync couldn't read it as one File *ServerFile `json:"file,omitempty"` + // Blind is the last write, when the server didn't check the version + // it went over (version.Blind), until a sync reads it back + Blind *blind `json:"blind,omitempty"` +} + +// blind is a write the server didn't check: the file it wrote (Sum), and +// the parts on the server and here as they were before it, by hash. When +// the next sync finds another file, another computer's write may have +// gone over this one in the moment between the HEAD and the PUT, both +// told they wrote (#1776); that sync merges from before this write, so +// what this computer changed counts as changed here again and goes back +// up beside the other's, where taking the other's file as having seen +// this one's would bring the old parts in over this computer's changes. +// Remote and Local are nil for a write to an empty server: the next sync +// is then a first one, as it would have been. +type blind struct { + Sum string `json:"sum"` + Remote map[string]string `json:"remote,omitempty"` + Local map[string]string `json:"local,omitempty"` } func path(name string) string { return filepath.Join(settings.Dir(), name) } @@ -900,6 +919,14 @@ func syncOnce(ctx context.Context, c Config, st *state) error { if err != nil && !unchanged { return err } + // a blind write read back: the server still has it, or another + // computer's write went over it (blind) + if b := st.Blind; b != nil { + if !unchanged && sum(data) != b.Sum { + st.Remote, st.Local = b.Remote, b.Local + } + st.Blind = nil + } local, err := collect(c) if err != nil { return err @@ -919,7 +946,9 @@ func syncOnce(ctx context.Context, c Config, st *state) error { } etag := ver.ETag rebuilt := false // a damaged server file replaced by this computer's merge - push := func(b backup.Bundle, etag string) error { + // push writes b over etag; were, as the parts on the server and here + // were before it, is kept when the write is blind + push := func(b backup.Bundle, etag string, were blind) error { b.Created, b.App = time.Now().UTC(), "magpie" sealed, err := backup.Seal(b, c.Passphrase) if err != nil { @@ -930,11 +959,15 @@ func syncOnce(ctx context.Context, c Config, st *state) error { return err } st.Sum, st.Server, st.Remote = sum(sealed), v, hashes(b) + if v.Blind { + were.Sum = st.Sum + st.Blind = &were + } remember(sealed) return nil } if data == nil { // nothing there yet: this computer's setup is the first - if err := push(local, ""); err != nil { + if err := push(local, "", blind{}); err != nil { return err } st.Local = L @@ -1079,7 +1112,7 @@ func syncOnce(ctx context.Context, c Config, st *state) error { // A rebuild always writes back: the server's file is the damaged one, // so even a merge that matches the cached copy must replace it. if rebuilt || !maps.Equal(M, R) { - if err := push(merged, etag); err != nil { + if err := push(merged, etag, blind{Remote: R, Local: pending}); err != nil { return err } } diff --git a/internal/davsync/restore.go b/internal/davsync/restore.go index d9f01dc08..6264e15a9 100644 --- a/internal/davsync/restore.go +++ b/internal/davsync/restore.go @@ -106,6 +106,7 @@ func Restore(ctx context.Context) ([]string, error) { // as a sync that brought them in leaves it: the next one has // nothing to push back st.Local, st.Remote, st.Sum, st.Server = hashes(local), R, sum(data), ver + st.Blind = nil // what is here now is what the server has remember(data) st.Last, st.Error = time.Now(), "" saveState(st) @@ -178,6 +179,7 @@ func Upload(ctx context.Context) error { } // as the first sync to an empty server leaves it st.Local, st.Remote, st.Sum, st.Server = hashes(local), hashes(b), sum(sealed), v + st.Blind = nil // it replaced what wasn't a backup: nothing to merge again remember(sealed) st.Last, st.Error, st.File = time.Now(), "", nil saveState(st) diff --git a/internal/davsync/s3.go b/internal/davsync/s3.go index 56c1ebbc0..27bd95247 100644 --- a/internal/davsync/s3.go +++ b/internal/davsync/s3.go @@ -263,7 +263,10 @@ func (s *s3) put(ctx context.Context, data []byte, etag string) (version, error) // header. There the ETag is looked at with a HEAD just before an // unconditional PUT: another computer's write in the moment between // the two would be lost, where a conditional PUT would refuse it, but - // one at any other time since this computer read is still caught. + // one at any other time since this computer read is still caught. The + // write is told as blind, so the next sync can tell it was gone over + // and merges this computer's changes again (syncOnce, #1776: Aliyun + // OSS answers If-Match 400 NotImplemented). now, there, err := s.head(ctx) if err != nil { return version{}, err @@ -277,7 +280,7 @@ func (s *s3) put(ctx context.Context, data []byte, etag string) (version, error) } defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { - return version{ETag: res.Header.Get("ETag")}, nil + return version{ETag: res.Header.Get("ETag"), Blind: true}, nil } return version{}, s.explain("write", readError(res)) } diff --git a/internal/davsync/s3_blind_test.go b/internal/davsync/s3_blind_test.go new file mode 100644 index 000000000..3e12eb45d --- /dev/null +++ b/internal/davsync/s3_blind_test.go @@ -0,0 +1,141 @@ +package davsync + +import ( + "context" + "testing" + "time" + + "github.com/yetone/magpie/internal/backup" + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" +) + +// #1776 (tsuixl, Aliyun OSS, which answers a conditional PUT 400 +// NotImplemented): two computers read the same file, A renames a provider +// and B sets the theme, and both write by HEAD then PUT; B's HEAD comes +// before A's PUT lands, so B's PUT goes over A's and both are told they +// wrote. Before, A's next sync took B's file as having seen A's and +// brought the old name back in over A's: the rename was lost on the +// server and here. Now A's write is kept as blind, and finding another +// file there, A merges from before it: the rename goes back up beside B's +// theme, and B brings it in. +func TestS3BlindWriteGoneOverIsMergedAgain(t *testing.T) { + f, srv := newFakeS3(t) + f.noCond = true + cfg := f.config(srv) + ctx := context.Background() + const key = "team x+y/magpie/magpie.magpie-backup" + sync := func(t *testing.T) { + t.Helper() + if err := Now(ctx); err != nil { + t.Fatal(err) + } + } + onServer := func(t *testing.T) backup.Bundle { + t.Helper() + f.mu.Lock() + data := f.objects[key] + f.mu.Unlock() + b, err := backup.Open(data, cfg.Passphrase) + if err != nil { + t.Fatal(err) + } + return b + } + name := func(ps []provider.Provider) string { + for _, p := range ps { + if p.ID == "deepseek" { + return p.Name + } + } + return "" + } + + a, b := newComputer(t), newComputer(t) + a.use(t) + provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k1"}) + settings.Save(settings.Settings{Theme: "light"}) + if err := Configure(cfg); err != nil { + t.Fatal(err) + } + sync(t) + b.use(t) + if err := Configure(cfg); err != nil { + t.Fatal(err) + } + sync(t) + if got := name(must(provider.Stored())); got != "DeepSeek" { + t.Fatalf("B's first sync: %q", got) + } + + // the file both read + f.mu.Lock() + was, wasTag := f.objects[key], f.etags[key] + f.mu.Unlock() + + a.use(t) + provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek A", Chat: "https://api.deepseek.com/v1", Key: "k1"}) + sync(t) + if got := name(onServer(t).Providers); got != "DeepSeek A" { + t.Fatalf("A's write: %q", got) + } + // B's HEAD saw the file both read, before A's PUT landed: B's write + // goes over A's + f.mu.Lock() + f.objects[key], f.etags[key] = was, wasTag + f.mu.Unlock() + b.use(t) + s := settings.Load() + s.Theme = "dark" + settings.Save(s) + sync(t) + if got := onServer(t); name(got.Providers) != "DeepSeek" || got.Settings.Theme != "dark" { + t.Fatalf("B's write over A's: %q %q", name(got.Providers), got.Settings.Theme) + } + + // A's next sync finds B's file: A's rename stays and goes back up + a.use(t) + sync(t) + if got := name(must(provider.Stored())); got != "DeepSeek A" { + t.Fatalf("A's rename here after its next sync: %q", got) + } + if got := settings.Load().Theme; got != "dark" { + t.Fatalf("B's theme on A: %q", got) + } + if got := onServer(t); name(got.Providers) != "DeepSeek A" || got.Settings.Theme != "dark" { + t.Fatalf("on the server after A's next sync: %q %q", name(got.Providers), got.Settings.Theme) + } + if n := Status().Notice; n != nil { + t.Fatalf("no conflict to tell: %+v", n) + } + + // B brings the rename in, keeping its theme (its notice is still the + // one from its first sync, which brought everything in) + b.use(t) + before := time.Now() + sync(t) + if got := name(must(provider.Stored())); got != "DeepSeek A" { + t.Fatalf("A's rename on B: %q", got) + } + if got := settings.Load().Theme; got != "dark" { + t.Fatalf("B's theme on B: %q", got) + } + if n := Status().Notice; n != nil && !n.At.Before(before) { + t.Fatalf("no conflict to tell on B: %+v", n) + } + // and nothing more is written: each sync after reads, no PUT + f.mu.Lock() + puts := len(f.log) + f.mu.Unlock() + a.use(t) + sync(t) + b.use(t) + sync(t) + f.mu.Lock() + defer f.mu.Unlock() + for _, l := range f.log[puts:] { + if l == "PUT" { + t.Fatalf("written again once both have it: %q", f.log[puts:]) + } + } +}