console: app-gone cleanup survives a retry after the app returns
A captured purchase that finds its app gone now records Unpointing in its details, by CAS, before it releases anything. Every later pass with the flag set finishes the cleanup (release the original app's hosts, unpoint the zone, done with no app) even if the app has been restored, so a lost DNS answer or a crash can no longer leave a purchase marked done with its claims attached and no managed addresses.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+92
−1
internal/console/domainbuy.go+11 −1internal/console/domainbuy_account_test.go+76 −0internal/console/domainstore.go+5 −0
CI failed — run details
diff --git a/internal/console/domainbuy.go b/internal/console/domainbuy.go| index af561cf8a..b0f517c8f 100644 |
| --- a/internal/console/domainbuy.go |
| +++ b/internal/console/domainbuy.go |
| @@ -736,7 +736,8 @@ func (s *Server) purchaseManagedSet(ctx context.Context, p *DomainPurchase) (zon |
| // capturedPurchase finishes a paid purchase. With no app it is done. With |
| // an app it attaches the apex and www to it; when the app has been purged |
| -// or put in Trash since, it finishes unpointed instead. |
| +// or put in Trash since, it marks the purchase Unpointing and finishes |
| +// unpointed instead, on this pass or a later one. |
| func (s *Server) capturedPurchase(ctx context.Context, acct *Account, p *DomainPurchase) error { |
| if p.App == "" { |
| return s.movePurchase(ctx, p, PurchaseDone, func(p *DomainPurchase) { p.Reason = "" }) |
| @@ -745,11 +746,20 @@ func (s *Server) capturedPurchase(ctx context.Context, acct *Account, p *DomainP |
| if err := s.movePurchase(ctx, p, p.State, nil); err != nil { |
| return err |
| } |
| + if p.Unpointing { |
| + return s.finishPurchaseUnpointed(ctx, acct, p) |
| + } |
| present, err := s.store.HasAppInContext(ctx, acct.ID, p.App) |
| if err != nil { |
| return err |
| } |
| if !present { |
| + // Durable before the first release: a pass that stops midway (a |
| + // lost DNS answer, a crash) is finished by the next, whatever the |
| + // app has done meanwhile. |
| + if err := s.movePurchase(ctx, p, p.State, func(p *DomainPurchase) { p.Unpointing = true }); err != nil { |
| + return err |
| + } |
| return s.finishPurchaseUnpointed(ctx, acct, p) |
| } |
| for _, host := range []string{p.Domain, "www." + p.Domain} { |
diff --git a/internal/console/domainbuy_account_test.go b/internal/console/domainbuy_account_test.go| index 9542bc7f5..1d9a84ed1 100644 |
| --- a/internal/console/domainbuy_account_test.go |
| +++ b/internal/console/domainbuy_account_test.go |
| @@ -20,6 +20,8 @@ import ( |
| "github.com/carlosframework/platform/internal/registrar" |
| "github.com/carlosframework/platform/internal/registrar/registrartest" |
| "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/zonehost" |
| + "github.com/carlosframework/platform/internal/zonehost/zonehosttest" |
| ) |
| // pausingRegistrar records the owner handle every Register names and, when |
| @@ -689,3 +691,77 @@ func TestNewContactEqualToSavedReusesIt(t *testing.T) { |
| t.Fatalf("saved contacts after a different contact: %+v %v", contacts, err) |
| } |
| } |
| + |
| +// appliedThenLostZone applies a SetManaged and then reports an error, once, |
| +// the answer of a DNS write whose response was lost. |
| +type appliedThenLostZone struct { |
| + *zonehosttest.Fake |
| + lose bool |
| +} |
| + |
| +func (z *appliedThenLostZone) SetManaged(ctx context.Context, zone zonehost.Zone, m zonehost.ManagedSet) error { |
| + if err := z.Fake.SetManaged(ctx, zone, m); err != nil { |
| + return err |
| + } |
| + if z.lose { |
| + z.lose = false |
| + return errors.New("zone host: response lost") |
| + } |
| + return nil |
| +} |
| + |
| +func TestAppGoneCleanupFinishesAfterAppRestored(t *testing.T) { |
| + for _, partial := range []bool{false, true} { |
| + t.Run(map[bool]string{false: "nothing attached", true: "apex attached"}[partial], func(t *testing.T) { |
| + f := newPurchaseFixture(t) |
| + ctx := context.Background() |
| + var p *DomainPurchase |
| + if partial { |
| + p = partialAttachTrashed(t, f) |
| + } else { |
| + p = f.start(t) |
| + f.authorise(t, p) |
| + f.until(t, p.ID, PurchaseRegistered) |
| + if err := f.st.TrashAppRow(f.acct.ID, "kass", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + p = f.advance(t, p.ID) |
| + if p.State != PurchaseCaptured { |
| + t.Fatalf("capture: %+v", p) |
| + } |
| + } |
| + f.srv.cfg.ZoneHost = &appliedThenLostZone{Fake: f.zone, lose: true} |
| + if err := f.srv.advancePurchase(ctx, p.ID); err == nil { |
| + t.Fatal("the lost DNS answer must be an error for the pass") |
| + } |
| + if p = f.read(t, p.ID); p.State != PurchaseCaptured || p.App != "kass" { |
| + t.Fatalf("after the lost answer: %+v", p) |
| + } |
| + f.assertUnpointed(t, p.Domain) |
| + if err := f.st.RestoreAppRow(f.acct.ID, "kass"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if ok, err := f.st.HasAppInContext(ctx, f.acct.ID, "kass"); err != nil || !ok { |
| + t.Fatalf("app not restored: %v %v", ok, err) |
| + } |
| + p = f.advance(t, p.ID) |
| + if p.State != PurchaseDone || p.App != "" || p.Reason != "app_gone" { |
| + t.Fatalf("retry after the app came back: %+v", p) |
| + } |
| + if c, err := domains.GetClaim(ctx, f.rel, p.Domain); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("apex claim: %+v %v", c, err) |
| + } |
| + if c, err := domains.GetClaim(ctx, f.rel, "www."+p.Domain); partial { |
| + if err != nil || c.Account != "foreign" { |
| + t.Fatalf("foreign www claim touched: %+v %v", c, err) |
| + } |
| + } else if !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("www claim: %+v %v", c, err) |
| + } |
| + f.assertUnpointed(t, p.Domain) |
| + if f.sent(billing.PreparedCapture) != 1 { |
| + t.Fatalf("captures: %v", f.fake.Sent) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/console/domainstore.go b/internal/console/domainstore.go| index 17fd43cae..7711cd25a 100644 |
| --- a/internal/console/domainstore.go |
| +++ b/internal/console/domainstore.go |
| @@ -52,6 +52,11 @@ type purchaseDetails struct { |
| // a legacy purchase uses its own Contact and OwnerHandle. |
| ContactID string |
| ContactVersion int64 |
| + // Unpointing is set, before anything is released, once a captured |
| + // purchase finds its app gone. From then on every pass finishes the |
| + // cleanup (release the original App's hosts, unpoint the zone, done |
| + // with no app), even if the app comes back. |
| + Unpointing bool `json:",omitempty"` |
| } |
| type DomainPurchase struct { |