subdomain set: judge the claim that won a lost create race (#1141)
In subdomainWrite's not-found branch, newETag, created, err := shadowed err. After a lost create, the re-read assigned the inner err, so the outer one was still os.ErrNotExist and the set was refused as unreadable (502) instead of being judged on the claim that won. The two results are now declared with var and assigned with =. The new test plants another account's claim just before the door's create-only write and checks the answer is occupied, naming it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+48
−1
internal/console/apisubdomain.go+5 −1internal/console/apisubdomain_test.go+43 −0
CI cancelled — superseded by a newer push — run details
Intent
diff --git a/internal/console/apisubdomain.go b/internal/console/apisubdomain.go| index 6c1484c31..341d905da 100644 |
| --- a/internal/console/apisubdomain.go |
| +++ b/internal/console/apisubdomain.go |
| @@ -467,7 +467,11 @@ func (s *Server) subdomainWrite(w http.ResponseWriter, ctx context.Context, acct |
| } |
| cur, etag, err := domains.GetClaimETag(ctx, s.cfg.Releases, host) |
| if errors.Is(err, os.ErrNotExist) { |
| - newETag, created, err := domains.AttachAccountNamespace(ctx, s.cfg.Releases, name, domain, acct.Sqid, |
| + // Plain assignment, not :=, so the re-read below a lost race sets |
| + // the err the check after this block reads (#1141). |
| + var newETag string |
| + var created bool |
| + newETag, created, err = domains.AttachAccountNamespace(ctx, s.cfg.Releases, name, domain, acct.Sqid, |
| now.UTC().Format(time.RFC3339), addr) |
| if err != nil { |
| unreadable("write", err) |
diff --git a/internal/console/apisubdomain_test.go b/internal/console/apisubdomain_test.go| index ebe947589..a910c6fb7 100644 |
| --- a/internal/console/apisubdomain_test.go |
| +++ b/internal/console/apisubdomain_test.go |
| @@ -240,6 +240,49 @@ func TestSubdomainDoorRollsBackOnRace(t *testing.T) { |
| } |
| } |
| +// nsLoseCreateStore lands before the door's create-only write of key, once: |
| +// another writer's claim reaches the key first, so the create loses with a |
| +// CAS conflict, as it would to a concurrent set from another console. |
| +type nsLoseCreateStore struct { |
| + release.Store |
| + key string |
| + before func() |
| + once sync.Once |
| +} |
| + |
| +func (s *nsLoseCreateStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| + if key == s.key && ifMatch == "" && s.before != nil { |
| + s.once.Do(s.before) |
| + } |
| + return s.Store.Put(ctx, key, body, ifMatch) |
| +} |
| + |
| +// TestSubdomainSetJudgesTheClaimThatWonTheCreateRace (#1141): the key was |
| +// empty when the door read it, but bcd's namespace claim landed before the |
| +// door's create-only write, so the create lost. The door re-reads and judges |
| +// the claim that won: harbour is bcd's, so the answer is occupied, naming |
| +// it. The lost-race branch used to shadow err with :=, so the re-read's |
| +// result never reached the check after it and the set was refused as |
| +// unreadable (a 502) instead. |
| +func TestSubdomainSetJudgesTheClaimThatWonTheCreateRace(t *testing.T) { |
| + fx, operator, _ := sdFixture(t, "code-sd-lost-create") |
| + nsAccount(t, fx, 49, "bcd") |
| + fx.srv.cfg.Releases = &nsLoseCreateStore{Store: fx.rel, key: domains.ClaimKey(nsHarbourHost), |
| + before: func() { nsHold(t, fx, "harbour", "bcd") }} |
| + fx.srv.claims.Reset() |
| + code, a, out := sdReq(t, fx, http.MethodPost, "bdq", operator, `{"name":"harbour"}`) |
| + want := fmt.Sprintf(subdomainRefuseOccupiedCopy, "harbour."+nsApps) |
| + if code != http.StatusConflict || a.Error != want || a.Code != string(domains.SubdomainOccupied) { |
| + t.Fatalf("set after losing the create race = %d %s, want 409 %q", code, out, want) |
| + } |
| + if wantHosts := []sdHost{{Host: nsHarbourHost, Account: "bcd", Source: occupantClaim}}; !reflect.DeepEqual(a.Hosts, wantHosts) { |
| + t.Errorf("occupants = %+v, want %+v", a.Hosts, wantHosts) |
| + } |
| + if c := nsClaimAt(t, fx, nsHarbourHost); c == nil || c.Account != "bcd" { |
| + t.Errorf("claim after the lost race = %+v, want bcd's untouched", c) |
| + } |
| +} |
| + |
| // nsCachingRaceStore is nsRaceStore over a store that answers ListMod (the |
| // fixture's DirStore under its deletableStore), so the claim cache caches as |
| // it does in production, and it counts the reads of each key. |