region HA: a trashed app's host never holds a departure (#1192)
On dev IE (2026-11-17) app bab/g6x had been trashed, but its instance record and a released lock naming member g-03152630 stayed. The departure matched the host and adoption deferred it on every pass (a trashed app has no route), so the member's edges record never retired and every later drain in the group timed out waiting on it. The departure scan now discharges a matched host whose app is TrashRetired (the shared trash rule), once its lock is released or expired, without importing it. It asks only when this box has no route for the host, once per app per pass. The finalization gate rereads the marker, so a restore before the delete sends the departure back to a walk. Witnesses: the 11-17 world finishes within the DR-5 bound; the same world without the rule stalls; a trashed production app and a restore both keep the departure.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+227
−7
internal/host/hadepartures.go+3 −0internal/host/hadepartures_scan.go+59 −7internal/host/hadepartures_trash_test.go+157 −0internal/host/hadepartures_wiring.go+8 −0
CI cancelled — superseded by a newer push — run details
diff --git a/internal/host/hadepartures.go b/internal/host/hadepartures.go| index 9bcce556f..4168c85a4 100644 |
| --- a/internal/host/hadepartures.go |
| +++ b/internal/host/hadepartures.go |
| @@ -88,6 +88,9 @@ type DepartureConfig struct { |
| Pending func(context.Context, registry.HADeparture, provision.InventorySnapshot) error |
| Page func(context.Context, registry.HADeparture, []provision.DepartureHost) error |
| PageIdentity func(context.Context, registry.HAPendingTermination, string) error |
| + // TrashRetired is release.TrashRetired over the deployment bucket with |
| + // this box's release key (#1192). Nil never discharges a host. |
| + TrashRetired func(ctx context.Context, account, app string) (bool, error) |
| } |
| type Controller struct { |
| telemetry departureTelemetry |
diff --git a/internal/host/hadepartures_scan.go b/internal/host/hadepartures_scan.go| index 47b3141a2..ff2ca817d 100644 |
| --- a/internal/host/hadepartures_scan.go |
| +++ b/internal/host/hadepartures_scan.go |
| @@ -169,6 +169,7 @@ func (c *Controller) ScanPass(ctx context.Context) (bool, error) { |
| } |
| blockers := []provision.DepartureHost{} |
| departed := map[string]bool{} |
| + trash := map[string]bool{} |
| count := 0 |
| for _, h := range inv.Records { |
| digest := scanDigest(h) |
| @@ -292,8 +293,12 @@ func (c *Controller) ScanPass(ctx context.Context) (bool, error) { |
| old.Result = "pending" |
| old.NotBefore = time.Time{} |
| s.Hosts[h.Host] = old |
| + // #1192: a host of an app the boxes retired to the Trash is never |
| + // adopted (no box mints its route), so it does not hold the |
| + // departure either. Like unserved, it waits for rule 1 below. |
| + trashed := c.trashed(ctx, h, trash) |
| unserved := false |
| - if !CanAdopt(h, c.cfg.Policy()) || !h.HA { |
| + if !trashed && (!CanAdopt(h, c.cfg.Policy()) || !h.HA) { |
| // DR-5 (spec §7.5): a host no surviving box could adopt does not |
| // hold the departure. It is neither imported nor touched. |
| if unserved = c.unserved(ctx, d, h, departed); !unserved { |
| @@ -308,6 +313,12 @@ func (c *Controller) ScanPass(ctx context.Context) (bool, error) { |
| wait(old.NotBefore) |
| continue |
| } |
| + if trashed { |
| + old.Result = departureTrashed |
| + s.Hosts[h.Host] = old |
| + c.logf("ha_departure_trashed departure=%s box=%s host=%s app=%s/%s generation=%d", d.ID, d.Box.Ref, h.Host, h.Account, h.App, v.Lock.Generation) |
| + continue |
| + } |
| if unserved { |
| old.Result = departureUnserved |
| s.Hosts[h.Host] = old |
| @@ -389,6 +400,35 @@ func (c *Controller) ScanPass(ctx context.Context) (bool, error) { |
| // could adopt it remains, so the departure finishes without an import. |
| const departureUnserved = "unserved" |
| +// departureTrashed marks a host discharged because its app is in the Trash |
| +// and the boxes have retired its routes (release.TrashRetired, #1192): no |
| +// box will adopt it until a restore, so the departure finishes without an |
| +// import. Rule 1 (a release marker or a passed expiry) still applies. |
| +const departureTrashed = "trashed" |
| + |
| +// trashed reports whether h's app is TrashRetired. It asks only when this |
| +// box holds no route for the host (a live app's route is always minted |
| +// here, so normal departures read nothing), and memoizes the answer per |
| +// app for one pass in memo. Any read failure answers false: the host keeps |
| +// the departure as before. |
| +func (c *Controller) trashed(ctx context.Context, h provision.DepartureHost, memo map[string]bool) bool { |
| + if c.cfg.TrashRetired == nil || h.Account == "" || h.App == "" { |
| + return false |
| + } |
| + if rt, err := c.cfg.Registry.Get(h.Host); err != nil || (rt != nil && rt.Account == h.Account && rt.App == h.App) { |
| + return false |
| + } |
| + key := h.Account + "/" + h.App |
| + if known, ok := memo[key]; ok { |
| + return known |
| + } |
| + call, cancel := context.WithTimeout(ctx, 5*time.Second) |
| + retired, err := c.cfg.TrashRetired(call, h.Account, h.App) |
| + cancel() |
| + memo[key] = err == nil && retired |
| + return memo[key] |
| +} |
| + |
| // unserved applies spec §7.5 rules 2 and 3 (rule 1, the release marker or |
| // the passed expiry, is the scan's own wait). It reads only this box's |
| // departure view and local journal: no S3 request. departed memoizes the |
| @@ -431,16 +471,28 @@ func (c *Controller) unserved(ctx context.Context, d registry.HADeparture, h pro |
| } |
| // unservedRevoked reports the first current host recorded unserved that no |
| -// longer meets spec §7.5: this box could now adopt it, or a possible server |
| -// is in view. It reads only the view, the policy and the local journal. |
| +// longer meets spec §7.5 (this box could now adopt it, or a possible server |
| +// is in view), or recorded trashed whose app is no longer TrashRetired (a |
| +// restore, #1192). Unserved reads only the view, the policy and the local |
| +// journal; trashed reads the marker and flag again, once per app. |
| func (c *Controller) unservedRevoked(ctx context.Context, d registry.HADeparture, s registry.HADepartureScan, inv provision.InventorySnapshot) (string, bool) { |
| departed := map[string]bool{} |
| + trash := map[string]bool{} |
| for _, h := range inv.Records { |
| - if hs, ok := s.Hosts[h.Host]; !ok || hs.Result != departureUnserved || hs.Lifetime != h.Lifetime { |
| + hs, ok := s.Hosts[h.Host] |
| + if !ok || hs.Lifetime != h.Lifetime { |
| continue |
| } |
| - if (CanAdopt(h, c.cfg.Policy()) && h.HA) || !c.unserved(ctx, d, h, departed) { |
| - return h.Host, true |
| + switch hs.Result { |
| + case departureTrashed: |
| + // #1192: a restore since the scan gives the host back to a box. |
| + if !c.trashed(ctx, h, trash) { |
| + return h.Host, true |
| + } |
| + case departureUnserved: |
| + if (CanAdopt(h, c.cfg.Policy()) && h.HA) || !c.unserved(ctx, d, h, departed) { |
| + return h.Host, true |
| + } |
| } |
| } |
| return "", false |
| @@ -797,7 +849,7 @@ func (c *Controller) complete(ctx context.Context, id string) (result error) { |
| c.telemetry.mu.Unlock() |
| recovered := 0 |
| for _, h := range s.Hosts { |
| - if h.Result != departureUnserved { |
| + if h.Result != departureUnserved && h.Result != departureTrashed { |
| recovered++ |
| } |
| } |
diff --git a/internal/host/hadepartures_trash_test.go b/internal/host/hadepartures_trash_test.go| new file mode 100644 |
| index 000000000..278b1d655 |
| --- /dev/null |
| +++ b/internal/host/hadepartures_trash_test.go |
| @@ -0,0 +1,157 @@ |
| +package host |
| + |
| +// #1192 (dev IE, 2026-11-17): app bab/g6x was trashed on 11-14, but its |
| +// instance record and its released lock, naming group member A, stayed. |
| +// When A was terminated its departure matched the host, and adoption |
| +// deferred it on every pass (a trashed app's route is never minted), so |
| +// A's edges record was never retired and every later drain waited on it. |
| +// |
| +// The witness is the DR-5 rig (production wiring of armHADepartures) with |
| +// one more host: an app in the Trash, in a pool this box serves, whose |
| +// clean release marker names A. |
| + |
| +import ( |
| + "context" |
| + "crypto/ed25519" |
| + "slices" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +const trashedHost = "g6x.bab.example.com" |
| + |
| +// withTrashedHost adds the 11-17 host to the rig: record, release marker by |
| +// A, then the app trashed, then an inventory round (which still lists it). |
| +func withTrashedHost(t *testing.T, x *dr5Rig) { |
| + t.Helper() |
| + ctx := t.Context() |
| + rec := provision.Record{Schema: 1, Account: "bab", App: "g6x", Host: trashedHost, Channel: "edge", HA: true, CreatedAt: x.start.Add(-time.Hour)} |
| + if err := provision.CreateRecord(ctx, x.store, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + x.records = append(x.records, rec) |
| + x.putMarker(t, trashedHost, x.dead, 9) |
| + if err := release.TrashApp(ctx, x.store, "bab", "g6x", "paul@keymail.dev", x.start.Add(-time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + x.reconcile(t) |
| + inv := x.a.Inventory.Snapshot() |
| + if !slices.ContainsFunc(inv.Records, func(h provision.DepartureHost) bool { return h.Host == trashedHost }) { |
| + t.Fatal("the trashed app's record is not in the departure inventory: the rig does not reproduce 11-17") |
| + } |
| + if rt, err := x.a.Registry.Get(trashedHost); err != nil || rt != nil { |
| + t.Fatalf("the trashed app has a route here (%+v, %v): the rig does not reproduce 11-17", rt, err) |
| + } |
| + x.locks[trashedHost] = x.lockBody(t, trashedHost) |
| +} |
| + |
| +func (x *dr5Rig) trashedUntouched(t *testing.T) { |
| + t.Helper() |
| + if got := x.lockBody(t, trashedHost); string(got) != string(x.locks[trashedHost]) { |
| + t.Fatalf("the trashed host's lock changed:\n%s\n%s", x.locks[trashedHost], got) |
| + } |
| + entry, _ := provision.LifetimeEntryOf(x.record(t, trashedHost)) |
| + if _, adopted, err := x.a.Registry.HAAdoption(t.Context(), trashedHost, entry.ID, x.id); err != nil || adopted { |
| + t.Fatalf("the trashed host was imported: %v", err) |
| + } |
| +} |
| + |
| +func TestDepartureTrashedAppDoesNotHoldIt(t *testing.T) { |
| + x := newDR5Rig(t, dr5Options{}) |
| + withTrashedHost(t, x) |
| + doneA, doneB := x.run(t, 5*time.Minute, nil) |
| + if doneA == 0 || doneA > dr5Bound || doneB == 0 { |
| + t.Fatalf("departures not finished within %s: A=%s B=%s logs=%q", dr5Bound, doneA, doneB, x.logs) |
| + } |
| + for _, key := range []string{x.edgesKey, "status/boxes/" + x.dead.Ref + ".json"} { |
| + if !x.gone(t, key) { |
| + t.Fatalf("%s left behind", key) |
| + } |
| + } |
| + if !slices.ContainsFunc(x.logs, func(l string) bool { |
| + return strings.HasPrefix(l, "ha_departure_trashed ") && strings.Contains(l, "host="+trashedHost) |
| + }) { |
| + t.Fatalf("no ha_departure_trashed line: %q", x.logs) |
| + } |
| + if x.pages != 0 { |
| + t.Fatalf("paged %d times", x.pages) |
| + } |
| + x.trashedUntouched(t) |
| + x.assertUntouched(t) |
| +} |
| + |
| +// Without the rule (the pre-#1192 scan), the same world never finishes: |
| +// this is the 11-17 stall, kept so the witness above proves the fix. |
| +func TestDepartureTrashedAppHeldItBefore1192(t *testing.T) { |
| + x := newDR5Rig(t, dr5Options{}) |
| + withTrashedHost(t, x) |
| + x.c.cfg.TrashRetired = nil |
| + doneA, doneB := x.run(t, 5*time.Minute, nil) |
| + if doneA != 0 { |
| + t.Fatalf("A finished at T+%s with no trash rule", doneA) |
| + } |
| + if doneB == 0 { |
| + t.Fatal("B did not finish") |
| + } |
| + if x.gone(t, x.edgesKey) { |
| + t.Fatal("A's edges file retired") |
| + } |
| +} |
| + |
| +// A trashed production app keeps its routes on every box, so it keeps the |
| +// departure too: the shared rule (release.TrashRetired) says not retired. |
| +func TestDepartureTrashedProductionAppStillHoldsIt(t *testing.T) { |
| + x := newDR5Rig(t, dr5Options{}) |
| + pub, priv, err := ed25519.GenerateKey(nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + ctx := t.Context() |
| + if err = release.SetProduction(ctx, x.store, priv, "bab", "g6x", true, "paul@keymail.dev"); err != nil { |
| + t.Fatal(err) |
| + } |
| + withTrashedHost(t, x) |
| + x.c.cfg.TrashRetired = func(ctx context.Context, account, app string) (bool, error) { |
| + return release.TrashRetired(ctx, x.store, pub, account, app) |
| + } |
| + if doneA, _ := x.run(t, 5*time.Minute, nil); doneA != 0 { |
| + t.Fatalf("A finished at T+%s on a production app's host", doneA) |
| + } |
| + if x.gone(t, x.edgesKey) { |
| + t.Fatal("A's edges file retired") |
| + } |
| +} |
| + |
| +// A restore after the host was discharged, before the delete, keeps the |
| +// departure: the rescan and the finalization gate read the marker again, |
| +// and the host then waits like any other. The restore lands inside the |
| +// discharge's own log call, so nothing of the scan runs before it. |
| +func TestDepartureTrashedAppRestoredBeforeDelete(t *testing.T) { |
| + x := newDR5Rig(t, dr5Options{}) |
| + withTrashedHost(t, x) |
| + restored := false |
| + logf := x.c.cfg.Logf |
| + x.c.cfg.Logf = func(f string, args ...any) { |
| + logf(f, args...) |
| + if !restored && strings.HasPrefix(f, "ha_departure_trashed ") { |
| + restored = true |
| + if err := release.RestoreApp(t.Context(), x.store, "bab", "g6x"); err != nil { |
| + t.Error(err) |
| + } |
| + } |
| + } |
| + doneA, _ := x.run(t, 5*time.Minute, nil) |
| + if !restored { |
| + t.Fatal("the host was never discharged, so the restore proves nothing") |
| + } |
| + if doneA != 0 { |
| + t.Fatalf("A finished at T+%s after its trashed host was restored", doneA) |
| + } |
| + if x.gone(t, x.edgesKey) { |
| + t.Fatal("A's edges file retired after the restore") |
| + } |
| +} |
diff --git a/internal/host/hadepartures_wiring.go b/internal/host/hadepartures_wiring.go| index dbac38bb9..41587730e 100644 |
| --- a/internal/host/hadepartures_wiring.go |
| +++ b/internal/host/hadepartures_wiring.go |
| @@ -112,6 +112,14 @@ func armHADepartures(ctx context.Context, a departureArm) (*Controller, hadepart |
| } |
| c, err := NewController(DepartureConfig{Admitted: a.Admitted, BeginWork: a.BeginWork, Registry: a.Registry, OpenStore: a.OpenStore, NewReader: defaultDepartureReader, Prefix: func(host string) string { return activator.HibernatePrefix(a.Backend.Cfg.Prefix, host) }, Identities: repo, Confirmer: a.Confirmer, Cache: cache, ProofStore: a.ProofStore, Self: a.Self, DeploymentBucket: a.Getenv("CARLOS_DEPLOYMENT_BUCKET"), Inventory: func() provision.InventorySnapshot { return departureInventorySnapshot(a.Inventory) }, RequestInventory: requestDepartureInventory, Policy: func() LocalPolicy { |
| return LocalPolicy{Scope: a.Self.Scope, Pools: provisionTickConfig(a.Getenv).Pools, Inbound: provisionTickConfig(a.Getenv).Inbound} |
| + }, TrashRetired: func(ctx context.Context, account, app string) (bool, error) { |
| + // The box's own retirement decision: the same bucket and release |
| + // key its reconcile reads (provisionTickConfig, productionClear). |
| + store, err := a.OpenStore(ctx) |
| + if err != nil { |
| + return false, err |
| + } |
| + return release.TrashRetired(ctx, store, provisionTickConfig(a.Getenv).ReleasePub, account, app) |
| }, Invalidate: a.Service.InvalidateDutyHolder, PageIdentity: func(ctx context.Context, h registry.HAPendingTermination, code string) error { |
| return PageOperator(ctx, alerting.LevelWarning, "ha_departure_pending", fmt.Sprintf("code=%s account=%s region=%s iid=%s", code, h.Account, h.Region, h.IID)) |
| }, Page: func(ctx context.Context, d registry.HADeparture, hosts []provision.DepartureHost) error { |