console: unread domain claims never block a purge's claim release
An unread claim on a custom-domain host is skipped with an hourly log line naming its key, whatever account the listing last knew: the claim cache cannot reliably remember an unreadable claim's owner, and a stale claim left behind is today's behaviour. The test covers a never-read claim and a cache-primed claim of the app that then turned malformed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+103
−51
internal/console/lifetimes_purge.go+9 −26internal/console/lifetimes_purge_test.go+94 −25
CI failed — run details
diff --git a/internal/console/lifetimes_purge.go b/internal/console/lifetimes_purge.go| index 5ce04d2b2..b5abc82ba 100644 |
| --- a/internal/console/lifetimes_purge.go |
| +++ b/internal/console/lifetimes_purge.go |
| @@ -6,7 +6,6 @@ import ( |
| "fmt" |
| "log" |
| "net/http" |
| - "os" |
| "sort" |
| "strings" |
| "sync" |
| @@ -322,15 +321,12 @@ func (s *Server) releaseAppDomainClaimsGuarded(ctx context.Context, sqid, app st |
| } |
| // appDomainClaims is sqid/app's custom-domain claims, sorted by host. A |
| -// claim the listing could not read on a custom-domain host is judged by the |
| -// account the listing last knew for it. This account's is read again on its |
| -// own: gone is skipped, still unreadable fails this purge (it fails toward |
| -// keeping), and a readable one is judged like the rest. An unknown account |
| -// (a body never read, such as an unparseable one) does not stop the purge: |
| -// it is skipped with one log line an hour naming its key, so one bad object |
| -// cannot stall every purge on the deployment; a claim left behind that way |
| -// is what every purge left before. Another account's is never this app's. |
| -// Nothing under the host-claim gate (releaseAppDomainClaims). |
| +// claim the listing could not read never stops a purge, whatever account |
| +// the listing last knew for it (the claim cache cannot reliably remember an |
| +// unreadable claim's owner): one on a custom-domain host is skipped with one |
| +// log line an hour naming its key, and the purge proceeds. A claim left |
| +// behind that way is what every purge left before. Nothing under the |
| +// host-claim gate (releaseAppDomainClaims). |
| func (s *Server) appDomainClaims(ctx context.Context, sqid, app string) ([]domains.Claim, error) { |
| if s.cfg.HostClaim || s.cfg.Releases == nil { |
| return nil, nil |
| @@ -350,22 +346,9 @@ func (s *Server) appDomainClaims(ctx context.Context, sqid, app string) ([]domai |
| } |
| } |
| for _, u := range unread { |
| - if u.Account != sqid || !custom(u.Host) { |
| - if u.Account == "" && custom(u.Host) { |
| - key := domains.ClaimKey(u.Host) |
| - s.lifetimeLogHourly("purge-unread-claim "+key, "console: purge %s/%s: skipping the unread claim %s (its account is unknown)", sqid, app, key) |
| - } |
| - continue |
| - } |
| - c, err := domains.GetClaim(ctx, s.cfg.Releases, u.Host) |
| - switch { |
| - case errors.Is(err, os.ErrNotExist): |
| - continue |
| - case err != nil: |
| - return nil, fmt.Errorf("console: purge %s/%s: read the domain claim %s: %w", sqid, app, u.Host, err) |
| - } |
| - if mine(*c) { |
| - out = append(out, *c) |
| + if custom(u.Host) { |
| + key := domains.ClaimKey(u.Host) |
| + s.lifetimeLogHourly("purge-unread-claim "+key, "console: purge %s/%s: skipping the unread claim %s (last known account %q)", sqid, app, key, u.Account) |
| } |
| } |
| sort.Slice(out, func(i, j int) bool { return out[i].Host < out[j].Host }) |
diff --git a/internal/console/lifetimes_purge_test.go b/internal/console/lifetimes_purge_test.go| index 14b53649e..0ccfd4199 100644 |
| --- a/internal/console/lifetimes_purge_test.go |
| +++ b/internal/console/lifetimes_purge_test.go |
| @@ -681,30 +681,99 @@ func TestLegacyPurgeBranchesReleaseClaims(t *testing.T) { |
| }) |
| } |
| -// An unread claim whose account nobody knows (an unparseable body, never read |
| -// before) does not stop the purge: it is skipped with one log line naming its |
| -// key, and the app's readable claims are released. |
| -func TestPurgeSkipsUnreadClaimOfUnknownAccount(t *testing.T) { |
| - l := purgeClaimsWorld(t) |
| - odd := domains.ClaimKey("odd.example") |
| - if _, err := l.fx.rel.Put(context.Background(), odd, []byte("{not json"), ""); err != nil { |
| - t.Fatal(err) |
| - } |
| - var logged bytes.Buffer |
| - log.SetOutput(&logged) |
| - t.Cleanup(func() { log.SetOutput(os.Stderr) }) |
| - if err := purgeCore(t, l.fx.srv, onCtx(t, l.fx.srv), time.Time{}); err != nil { |
| - t.Fatalf("purge = %v, want it to finish past the unread claim", err) |
| - } |
| - log.SetOutput(os.Stderr) |
| - wantShopClaimsReleased(t, l.fx.rel) |
| - if tomb, err := release.ReadAppTombstone(context.Background(), l.fx.rel, "bab", ltApp); err != nil || tomb != nil { |
| - t.Fatalf("marker = %+v %v, want gone", tomb, err) |
| - } |
| - if !strings.Contains(logged.String(), "skipping the unread claim "+odd) { |
| - t.Fatalf("the log does not name the unread key %s:\n%s", odd, logged.String()) |
| - } |
| - if raw, _, err := l.fx.rel.Get(context.Background(), odd); err != nil || string(raw) != "{not json" { |
| - t.Fatalf("the unread claim = %q %v, want it left as it was", raw, err) |
| +// agedMods reports every listed object an hour older than it is, so the |
| +// claim cache keeps what it reads (it refuses stamps inside modSettle). |
| +type agedMods struct{ release.Store } |
| + |
| +func (a agedMods) ListMod(ctx context.Context, prefix string) ([]release.KeyMod, error) { |
| + km, err := a.Store.(release.ModLister).ListMod(ctx, prefix) |
| + for i := range km { |
| + km[i].Mod = km[i].Mod.Add(-time.Hour) |
| } |
| + return km, err |
| +} |
| + |
| +// An unread claim never stops a purge, whatever account the listing last |
| +// knew for it: it is skipped with one log line naming its key, and the app's |
| +// readable claims are released. Both shapes: a body never read (no account |
| +// known), and a claim of this app the cache read before it turned malformed |
| +// (the first listing after still knows its account). |
| +func TestPurgeSkipsUnreadClaimOfUnknownAccount(t *testing.T) { |
| + ctx := context.Background() |
| + captureLog := func(t *testing.T) *bytes.Buffer { |
| + t.Helper() |
| + var logged bytes.Buffer |
| + log.SetOutput(&logged) |
| + t.Cleanup(func() { log.SetOutput(os.Stderr) }) |
| + return &logged |
| + } |
| + t.Run("never read", func(t *testing.T) { |
| + l := purgeClaimsWorld(t) |
| + odd := domains.ClaimKey("odd.example") |
| + if _, err := l.fx.rel.Put(ctx, odd, []byte("{not json"), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + logged := captureLog(t) |
| + if err := purgeCore(t, l.fx.srv, onCtx(t, l.fx.srv), time.Time{}); err != nil { |
| + t.Fatalf("purge = %v, want it to finish past the unread claim", err) |
| + } |
| + log.SetOutput(os.Stderr) |
| + wantShopClaimsReleased(t, l.fx.rel) |
| + if tomb, err := release.ReadAppTombstone(ctx, l.fx.rel, "bab", ltApp); err != nil || tomb != nil { |
| + t.Fatalf("marker = %+v %v, want gone", tomb, err) |
| + } |
| + if !strings.Contains(logged.String(), "skipping the unread claim "+odd) { |
| + t.Fatalf("the log does not name the unread key %s:\n%s", odd, logged.String()) |
| + } |
| + if raw, _, err := l.fx.rel.Get(ctx, odd); err != nil || string(raw) != "{not json" { |
| + t.Fatalf("the unread claim = %q %v, want it left as it was", raw, err) |
| + } |
| + }) |
| + t.Run("read before, then malformed", func(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.AppsDomain = "oncarlos.test" |
| + now := time.Now().Truncate(time.Second) |
| + trashedTemp(t, fx, "tmp-a", now.Add(-72*time.Hour)) |
| + putPurgeClaim(t, fx.rel, fx.sqid, "tmp-a", "shop.example", false) |
| + putPurgeClaim(t, fx.rel, fx.sqid, "tmp-a", "cached.example", false) |
| + fx.srv.cfg.Releases = agedMods{Store: fx.rel} |
| + cached := domains.ClaimKey("cached.example") |
| + // prime caches cached.example's good body, then breaks it. |
| + prime := func() { |
| + t.Helper() |
| + if err := fx.rel.Delete(ctx, cached); err != nil { |
| + t.Fatal(err) |
| + } |
| + putPurgeClaim(t, fx.rel, fx.sqid, "tmp-a", "cached.example", false) |
| + if claims, _, err := fx.srv.claims.ListWithUnread(ctx, fx.srv.cfg.Releases); err != nil || !slices.ContainsFunc(claims, func(c domains.Claim) bool { return c.Host == "cached.example" }) { |
| + t.Fatalf("priming listing = %v %v, want cached.example read", claims, err) |
| + } |
| + if err := fx.rel.Delete(ctx, cached); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := fx.rel.Put(ctx, cached, []byte("{not json"), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + // The shape under test: the first listing after the break still |
| + // names the account it last read. |
| + prime() |
| + _, unread, err := fx.srv.claims.ListWithUnread(ctx, fx.srv.cfg.Releases) |
| + if err != nil || !slices.Contains(unread, domains.UnreadClaim{Host: "cached.example", Account: fx.sqid}) { |
| + t.Fatalf("unread = %+v %v, want cached.example known as %s's", unread, err, fx.sqid) |
| + } |
| + prime() |
| + logged := captureLog(t) |
| + fx.srv.expiryPass(ctx, now) |
| + log.SetOutput(os.Stderr) |
| + if claimHeld(t, fx.rel, "shop.example") { |
| + t.Error("shop.example is still claimed by the purged app") |
| + } |
| + if markerOf(t, fx, "tmp-a") != nil || lifeOf(t, fx, "tmp-a") != nil { |
| + t.Fatal("the purge stopped at the unread claim: marker or row left") |
| + } |
| + if !strings.Contains(logged.String(), "skipping the unread claim "+cached) { |
| + t.Fatalf("the log does not name the unread key %s:\n%s", cached, logged.String()) |
| + } |
| + }) |
| } |