From 90ff867e7ac9b7a35ef411dee00d3d70621d2495 Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 18:34:48 +0100 Subject: [PATCH] lock the test fakes' maps Two tests failed under -race on main too: the re-ask state fake's maps were written by a try on the watch's goroutine while the test read and deleted them, and the operator-ask test read the build store unlocked and waited on the builder while the build its answer starts could still be counting itself in. The fake's maps are locked and read through it, the store is read under its lock, and the builder is waited on only once that build has claimed its key. Builds: design 47 H H-A-290 (the module's tests pass with -race, as its checks ask) --- .../mesh-delivery-builder/publishitem_test.go | 32 +++++++++++++++---- .../internal/build/claim_test.go | 5 ++- .../internal/build/retakeask_test.go | 10 ++++-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem_test.go index 8a70df53..9777b3b4 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem_test.go @@ -9,6 +9,7 @@ import ( "path/filepath" "slices" "strings" + "sync" "testing" "time" @@ -21,9 +22,10 @@ import ( "mesh-delivery-builder/internal/versions" ) -// itemKV is the re-ask state in memory: one revision per key. +// itemKV is the re-ask state in memory: one revision per key, its maps locked (a try writes from the re-ask watch's goroutine). type itemKV struct { jetstream.KeyValue + mu sync.Mutex vals map[string][]byte revs map[string]uint64 rev uint64 @@ -44,13 +46,25 @@ func (e itemEntry) Operation() jetstream.KeyValueOp { return jetstream.KeyValueP func (k *itemKV) entry(key string) jetstream.KeyValueEntry { return itemEntry{key: key, value: k.vals[key], rev: k.revs[key]} } + +// has says whether key holds a value. +func (k *itemKV) has(key string) bool { + k.mu.Lock() + defer k.mu.Unlock() + _, ok := k.vals[key] + return ok +} func (k *itemKV) Get(_ context.Context, key string) (jetstream.KeyValueEntry, error) { + k.mu.Lock() + defer k.mu.Unlock() if _, ok := k.vals[key]; !ok { return nil, jetstream.ErrKeyNotFound } return k.entry(key), nil } func (k *itemKV) Create(_ context.Context, key string, v []byte, _ ...jetstream.KVCreateOpt) (uint64, error) { + k.mu.Lock() + defer k.mu.Unlock() if _, ok := k.vals[key]; ok { return 0, jetstream.ErrKeyExists } @@ -59,6 +73,8 @@ func (k *itemKV) Create(_ context.Context, key string, v []byte, _ ...jetstream. return k.rev, nil } func (k *itemKV) Update(_ context.Context, key string, v []byte, rev uint64) (uint64, error) { + k.mu.Lock() + defer k.mu.Unlock() if k.revs[key] != rev { return 0, jetstream.ErrKeyExists } @@ -67,6 +83,8 @@ func (k *itemKV) Update(_ context.Context, key string, v []byte, rev uint64) (ui return k.rev, nil } func (k *itemKV) Delete(_ context.Context, key string, _ ...jetstream.KVDeleteOpt) error { + k.mu.Lock() + defer k.mu.Unlock() delete(k.vals, key) return nil } @@ -250,7 +268,7 @@ func TestThePublishItemsAreWiredToTheReservationsAndTheReAskWatch(t *testing.T) } first := done("src2:one") key := versions.PublishItemKey("m", first.Version) - if _, ok := kv.vals[key]; !ok { + if !kv.has(key) { t.Fatalf("a done build has no publish item: %v", kv.vals) } // A build whose artifacts are all published still gets its item, until its release is written (A-456b). @@ -261,7 +279,7 @@ func TestThePublishItemsAreWiredToTheReservationsAndTheReAskWatch(t *testing.T) if err := rv.Built(ctx, "m", first.Version, map[string]string{"m:a": "sha256:a"}, nil); err != nil { t.Fatal(err) } - if _, ok := kv.vals[key]; !ok { + if !kv.has(key) { t.Fatalf("a build with every artifact published has no item: %v", kv.vals) } // The watch hands the item to its try, which queues the publish. @@ -277,11 +295,11 @@ func TestThePublishItemsAreWiredToTheReservationsAndTheReAskWatch(t *testing.T) } // A takeover by another build deletes the older's item and asks the newer's. second := done("src2:two") - if _, ok := kv.vals[key]; ok { + if kv.has(key) { t.Errorf("the taken-over reservation kept its item") } secondKey := versions.PublishItemKey("m", second.Version) - if _, ok := kv.vals[secondKey]; !ok { + if !kv.has(secondKey) { t.Fatalf("the newer build has no item: %v", kv.vals) } // version-released deletes the item and keeps the release. @@ -289,7 +307,7 @@ func TestThePublishItemsAreWiredToTheReservationsAndTheReAskWatch(t *testing.T) if !rv.heard(ctx, msg) || !msg.acked { t.Fatal("version-released was not taken in") } - if _, ok := kv.vals[secondKey]; ok { + if kv.has(secondKey) { t.Errorf("a released version kept its publish item") } if ok, _ := rv.ReleasedFor(ctx, "m", "src2:two"); !ok { @@ -300,7 +318,7 @@ func TestThePublishItemsAreWiredToTheReservationsAndTheReAskWatch(t *testing.T) if _, err := rv.dropped(ctx, "m", "0.1", "op", "never", time.Unix(0, 0)); err != nil { t.Fatal(err) } - if _, ok := kv.vals[versions.PublishItemKey("m", third.Version)]; ok { + if kv.has(versions.PublishItemKey("m", third.Version)) { t.Errorf("a dropped reservation kept its item") } } diff --git a/modules/mesh-delivery-builder/internal/build/claim_test.go b/modules/mesh-delivery-builder/internal/build/claim_test.go index ebbe4ec6..320c4ffd 100644 --- a/modules/mesh-delivery-builder/internal/build/claim_test.go +++ b/modules/mesh-delivery-builder/internal/build/claim_test.go @@ -102,7 +102,10 @@ func seed(t *testing.T, st *memStore, b builds.BuilderBuild) string { func valueOf(t *testing.T, st *memStore, key string) builds.BuilderBuild { t.Helper() - b, err := builds.DecodeBuilderBuild(st.vals[key]) + st.mu.Lock() + raw := st.vals[key] + st.mu.Unlock() + b, err := builds.DecodeBuilderBuild(raw) if err != nil { t.Fatal(err) } diff --git a/modules/mesh-delivery-builder/internal/build/retakeask_test.go b/modules/mesh-delivery-builder/internal/build/retakeask_test.go index 35c461d3..b30b3c0f 100644 --- a/modules/mesh-delivery-builder/internal/build/retakeask_test.go +++ b/modules/mesh-delivery-builder/internal/build/retakeask_test.go @@ -95,10 +95,14 @@ func theOperatorAnswers(t *testing.T, option string) { if err != nil || h != occ.Acted { t.Fatalf("%s: %v, %v", option, h, err) } - for i := 0; i < 50 && option == OptionBuildOnceMore; i++ { // the build the answer starts runs on its own goroutine - r.b.Wait() - if valueOf(t, r.st, r.key).State == builds.BuildDone { + // The build the answer starts runs on its own goroutine: the builder is waited on only once that build has claimed the key + // (its start counted before its claim, read through the store's lock), never while its start may still be counted. + for i := 0; i < 50 && option == OptionBuildOnceMore; i++ { + if st := valueOf(t, r.st, r.key).State; st == builds.BuildDone { break + } else if st != builds.BuildFailed { + r.b.Wait() + continue } time.Sleep(time.Millisecond) }