carlos / platform Public

ha: refuse promptly while lost lease cleanup is pending

Fix F-A R1 recovery findings: bound shutdown discovery joins, require recent demand, park stale routes and foreign owners, and wake queued stop waiters when loss latches. Stop recoveries that fail their post-check.
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
claude agent, operated by paul@keymail.dev 6da6548a05901e156d4c4f5295c111fdb1dd73d3 parent abd4002
3 files changed, +371 −38
  • internal/activator/activator.go +21 −8
  • internal/activator/haleaseloss.go +93 −25
  • internal/activator/haleaseloss_test.go +257 −5
diff --git a/internal/activator/activator.go b/internal/activator/activator.go
index dd42def7f..b9af7ea1c 100644
--- a/internal/activator/activator.go
+++ b/internal/activator/activator.go
@@ -489,6 +489,7 @@ const (
type actEntry struct {
haRecovery *haLeaseLoss
+ haRefused chan struct{}
haReleasing bool
haAcquireUntil time.Time
duty bool
@@ -1905,8 +1906,14 @@ func (a *Service) begin(ctx context.Context, rt registry.Route, stamp bool, req
case actStopping:
stopped := e.stopped
+ if e.haRefused == nil {
+ e.haRefused = make(chan struct{})
+ }
+ refused := e.haRefused
a.mu.Unlock()
select {
+ case <-refused:
+ continue
case <-stopped:
continue // entry is gone; loop wakes it fresh
case <-ctx.Done():
@@ -5194,13 +5201,6 @@ func (a *Service) retryPendingStop(e *actEntry) {
// ErrStillRunning and returned, plus whatever finishPendingStop's own
// retried proc.Stop just added.
func (a *Service) finishStop(e *actEntry, outcome *stopObservation) {
- a.mu.Lock()
- current := a.entries[e.host] == e
- a.mu.Unlock()
- if !current {
- return
- }
-
// A run attached to this instance is stopped and settled before
// replication closes (run spec §5.3, codex 1): everything it committed
// is in the final checkpoint, and nothing writes the database after.
@@ -6140,6 +6140,12 @@ func (a *Service) Shutdown(ctx context.Context) {
// (legacyRunGone, #292): none may outlive Shutdown.
a.shutdownCtx = ctx
sweepStop, sweepDone := a.sweepStop, a.sweepDone
+ var discoveries []chan struct{}
+ for _, l := range a.haLoss.hosts {
+ if l.running {
+ discoveries = append(discoveries, l.discovery)
+ }
+ }
a.mu.Unlock()
// …and those already running end with it too (Astra, round 3).
context.AfterFunc(ctx, a.cancelProofs)
@@ -6156,7 +6162,14 @@ func (a *Service) Shutdown(ctx context.Context) {
if a.lifeStop != nil {
a.lifeStop()
}
- a.haLoss.joined.Wait()
+ // Recovery entries are stopped by the normal pass below. Only discovery
+ // is joined here, within the shutdown budget.
+ for _, done := range discoveries {
+ select {
+ case <-done:
+ case <-ctx.Done():
+ }
+ }
if sweepStop != nil {
close(sweepStop)
<-sweepDone
diff --git a/internal/activator/haleaseloss.go b/internal/activator/haleaseloss.go
index 91bb1544c..835386872 100644
--- a/internal/activator/haleaseloss.go
+++ b/internal/activator/haleaseloss.go
@@ -7,6 +7,7 @@ import (
"sync"
"time"
+ "github.com/carlosframework/platform/internal/hafresh"
"github.com/carlosframework/platform/internal/lease"
"github.com/carlosframework/platform/internal/registry"
)
@@ -14,6 +15,7 @@ import (
var ErrHALeaseUnavailable = errors.New("ha_lease_unavailable")
const haLossRetryInterval = 10 * time.Second
+const haLossDemandWindow = 60 * time.Second
const haLossAttemptBudget = 2 * time.Second
const haLossWorkers = 4
@@ -21,12 +23,17 @@ const haLossWorkers = 4
// lifetime and generation. Entry removal cannot discharge it. Only the
// recovery entry's healthy, newly acquired replica can do that.
type haLeaseLoss struct {
- old *actEntry
- route registry.Route
- running bool
- next time.Time
- recovery *actEntry
- foreign bool
+ old *actEntry
+ route registry.Route
+ running bool
+ next time.Time
+ recovery *actEntry
+ foreign bool
+ wanted time.Time // last refused request
+ request uint64
+ foreignRequest uint64
+ discovery chan struct{} // closed before the normal wake/stop lifecycle begins
+ refused chan struct{}
}
type haLossState struct {
@@ -68,7 +75,11 @@ func (a *Service) latchHALossLocked(e *actEntry) {
}
return
}
- a.haLoss.hosts[e.host] = &haLeaseLoss{old: e, route: e.route}
+ if e.haRefused == nil {
+ e.haRefused = make(chan struct{})
+ }
+ close(e.haRefused)
+ a.haLoss.hosts[e.host] = &haLeaseLoss{old: e, route: e.route, refused: e.haRefused}
a.holders.drop(e.host)
}
@@ -98,7 +109,9 @@ func (a *Service) haLeaseRefusalLocked(rt registry.Route) error {
if a.haLoss.hosts == nil {
a.haLoss.hosts = make(map[string]*haLeaseLoss)
}
- a.haLoss.hosts[rt.Host] = &haLeaseLoss{route: rt}
+ refused := make(chan struct{})
+ close(refused)
+ a.haLoss.hosts[rt.Host] = &haLeaseLoss{route: rt, refused: refused}
return ErrHALeaseUnavailable
}
}
@@ -109,6 +122,24 @@ func (a *Service) haLeaseRefusalLocked(rt registry.Route) error {
// The ordinary acquire settles owed markers and applies pool, lifetime,
// same-box-empty and foreign-termination checks. It never reuses a Held.
func (a *Service) retryHALostHost(rt registry.Route) {
+ a.mu.Lock()
+ if l := a.haLoss.hosts[rt.Host]; l != nil {
+ l.wanted = a.now()
+ l.request++
+ l.route = rt
+ }
+ a.mu.Unlock()
+ a.scheduleHALostHost(rt.Host)
+}
+
+func (a *Service) scheduleHALostHost(host string) {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+ l := a.haLoss.hosts[host]
+ if l == nil || l.wanted.IsZero() || !a.now().Before(l.wanted.Add(haLossDemandWindow)) || (l.foreignRequest != 0 && l.foreignRequest == l.request) {
+ return
+ }
+ rt := l.route
if !a.LocalEligible(rt) {
return
}
@@ -119,32 +150,36 @@ func (a *Service) retryHALostHost(rt registry.Route) {
return
}
}
- a.mu.Lock()
- l := a.haLoss.hosts[rt.Host]
- if l == nil || l.running || a.haLoss.workers >= haLossWorkers || a.shuttingDown || a.life().Err() != nil || a.now().Before(l.next) || a.entries[rt.Host] != nil || a.fences[rt.Host] != 0 || a.unprovenWakes[rt.Host] != nil || a.releaseOps[rt.Host] != nil || len(a.straySidecars[rt.Host]) != 0 {
- a.mu.Unlock()
+ if l.running || a.haLoss.workers >= haLossWorkers || a.shuttingDown || a.life().Err() != nil || a.now().Before(l.next) || a.entries[rt.Host] != nil || a.fences[rt.Host] != 0 || a.unprovenWakes[rt.Host] != nil || a.releaseOps[rt.Host] != nil || len(a.straySidecars[rt.Host]) != 0 {
return
}
if l.old != nil {
select {
case <-l.old.stopped:
default:
- a.mu.Unlock()
return
}
}
l.running = true
- l.route = rt
+ l.discovery = make(chan struct{})
l.next = a.now().Add(haLossRetryInterval)
a.haLoss.workers++
a.haLoss.joined.Add(1)
- a.mu.Unlock()
- go a.recoverHALostHost(rt, l)
+ go a.recoverHALostHost(rt, l, l.request)
}
-func (a *Service) recoverHALostHost(rt registry.Route, l *haLeaseLoss) {
+func (a *Service) recoverHALostHost(rt registry.Route, l *haLeaseLoss, request uint64) {
+ discovery := l.discovery
+ foreign := false
+ var foreignRequest uint64
defer func() {
a.mu.Lock()
+ if discovery != nil {
+ close(discovery)
+ }
+ if foreign && a.haLoss.hosts[rt.Host] == l {
+ l.foreignRequest = foreignRequest
+ }
l.running = false
a.haLoss.workers--
a.mu.Unlock()
@@ -152,6 +187,26 @@ func (a *Service) recoverHALostHost(rt registry.Route, l *haLeaseLoss) {
}()
attempt, cancel := context.WithTimeout(a.life(), haLossAttemptBudget)
defer cancel()
+ lookup := a.Routes
+ if lookup == nil {
+ lookup, _ = a.due.(RouteLookup)
+ }
+ if lookup == nil {
+ return
+ }
+ fresh, err := lookup.Get(rt.Host)
+ if err != nil {
+ return
+ }
+ if fresh == nil || leaseRouteChanged(rt, fresh, rt.IsSidecar()) != nil || fresh.Pool != rt.Pool || fresh.Incarnation != rt.Incarnation || fresh.ServingSeq != rt.ServingSeq || fresh.CreatedAt != rt.CreatedAt || fresh.HA != rt.HA || fresh.Moved || fresh.HoldOp != "" || a.disabled(*fresh) || !a.LocalEligible(*fresh) {
+ a.mu.Lock()
+ if l.request == request {
+ l.wanted = time.Time{}
+ }
+ a.mu.Unlock()
+ return
+ }
+ rt = *fresh
// Discovery and acquisition share one deadline. No helper goroutine is
// abandoned when it expires: the worker owns and joins all its work.
if r := holderReaderFor(a.backend, rt); r != nil {
@@ -159,11 +214,20 @@ func (a *Service) recoverHALostHost(rt registry.Route, l *haLeaseLoss) {
if err != nil {
return
}
- if ref := otherHolder(v, r.self(), a.holders.clock()); ref != "" {
+ owner, parsed := hafresh.ParseOwner(v.Lock.Owner)
+ self := r.self()
+ foreign = !v.Absent && v.Lock.Release == nil && parsed && owner.HA && owner.Ref != self.Ref && (self.IID == "" || owner.IID != self.IID)
+ if foreign {
+ a.mu.Lock()
+ foreignRequest = l.request
+ a.mu.Unlock()
+ }
+ if ref := otherHolder(v, self, a.holders.clock()); ref != "" {
a.mu.Lock()
if a.haLoss.hosts[rt.Host] == l {
a.holders.set(rt.Host, ref)
l.foreign = true
+ l.foreignRequest = l.request
}
a.mu.Unlock()
return
@@ -183,14 +247,18 @@ func (a *Service) recoverHALostHost(rt registry.Route, l *haLeaseLoss) {
l.recovery = e
l.foreign = false
a.putEntryLocked(rt.Host, e)
+ close(discovery)
+ discovery = nil
a.mu.Unlock()
a.wake(e)
a.mu.Lock()
defer a.mu.Unlock()
- if a.haLoss.hosts[rt.Host] != l || a.entries[rt.Host] != e || e.state != actAwake || e.err != nil || e.proc == nil || e.proc.Exited() || servingError(e) != nil || !a.LocalEligible(e.route) {
+ if a.haLoss.hosts[rt.Host] != l || a.entries[rt.Host] != e || e.state != actAwake {
return
}
- if _, ha := haEntryRep(e); !ha {
+ _, ha := haEntryRep(e)
+ if e.err != nil || e.proc == nil || e.proc.Exited() || servingError(e) != nil || !a.LocalEligible(e.route) || !ha {
+ a.beginStopLocked(e, StopLeaseLost)
return
}
delete(a.haLoss.hosts, rt.Host)
@@ -207,12 +275,12 @@ func (a *Service) recoverHALostHost(rt registry.Route, l *haLeaseLoss) {
func (a *Service) retryHALostHosts() {
a.mu.Lock()
- routes := make([]registry.Route, 0, len(a.haLoss.hosts))
- for _, l := range a.haLoss.hosts {
- routes = append(routes, l.route)
+ hosts := make([]string, 0, len(a.haLoss.hosts))
+ for host := range a.haLoss.hosts {
+ hosts = append(hosts, host)
}
a.mu.Unlock()
- for _, rt := range routes {
- a.retryHALostHost(rt)
+ for _, host := range hosts {
+ a.scheduleHALostHost(host)
}
}
diff --git a/internal/activator/haleaseloss_test.go b/internal/activator/haleaseloss_test.go
index b2858574d..c707d755d 100644
--- a/internal/activator/haleaseloss_test.go
+++ b/internal/activator/haleaseloss_test.go
@@ -45,9 +45,17 @@ func lostTestEntry(t *testing.T) (*haSvc, *actEntry, *lossTestRep) {
t.Helper()
s, e := haServiceEntry(t, "shop.example.com")
s.rt.HA = true
+ if err := s.b.Registry.Upsert(s.rt); err != nil {
+ t.Fatal(err)
+ }
+ fresh, err := s.b.Registry.Get(s.rt.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ s.rt = *fresh
r := &lossTestRep{Rep: e.rep, haRep: e.rep.(haRep), entered: make(chan struct{}), unblock: make(chan struct{})}
s.a.mu.Lock()
- e.route.HA = true
+ e.route = s.rt
e.rep = r
s.a.mu.Unlock()
r.lost.Store(true)
@@ -249,7 +257,6 @@ func TestHALostLeaseRecoversOnlyAfterFreshAcquireTemporaryS3Failure(t *testing.T
s.a.mu.Lock()
s.a.latchHALossLocked(e)
s.a.mu.Unlock()
- s.a.finishStop(e, &stopObservation{})
s.a.mu.Lock()
current := s.a.entries[s.rt.Host]
refusal := s.a.haLeaseRefusalLocked(s.rt)
@@ -280,6 +287,8 @@ func TestHALostLeaseRecoversOnlyAfterFreshAcquireForeignLease(t *testing.T) {
s, e, r := lostTestEntry(t)
finishLostTestEntry(t, s, e, r)
putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(-time.Minute)))
+ clk := &fakeClock{t: time.Now()}
+ s.a.now = clk.now
before := len(lockWriteLogOf(s))
s.a.retryHALostHost(s.rt)
s.a.haLoss.joined.Wait()
@@ -287,9 +296,15 @@ func TestHALostLeaseRecoversOnlyAfterFreshAcquireForeignLease(t *testing.T) {
if len(lockWriteLogOf(s)) != before {
t.Fatal("foreign expiry authorized an acquire")
}
+ reads := len(s.fx.Requests())
+ clk.advance(11 * time.Second)
+ s.a.retryHALostHosts()
+ s.a.haLoss.joined.Wait()
+ if len(s.fx.Requests()) != reads {
+ t.Error("expired foreign holder polled without a new request")
+ }
// A live foreign holder permits forwarding, without opening local service.
- clk := &fakeClock{t: time.Now().Add(11 * time.Second)}
- s.a.now = clk.now
+ clk.advance(11 * time.Second)
putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(time.Minute)))
s.a.retryHALostHost(s.rt)
s.a.haLoss.joined.Wait()
@@ -298,6 +313,13 @@ func TestHALostLeaseRecoversOnlyAfterFreshAcquireForeignLease(t *testing.T) {
t.Fatalf("foreign forward=%+v, %v", d, ok)
}
assertLostResponse(t, s)
+ clk.advance(11 * time.Second)
+ putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(-time.Minute)))
+ s.a.retryHALostHost(s.rt)
+ s.a.haLoss.joined.Wait()
+ if d, ok := s.a.ForwardTargetFromMemory(s.rt, nil); !ok || !d.Unavailable {
+ t.Fatalf("expired foreign holder reused a stale live hint: %+v", d)
+ }
}
func TestHALostLeaseRecoversOnlyAfterFreshAcquirePoolWithdrawal(t *testing.T) {
@@ -441,8 +463,11 @@ func (lostDiscoveryBackend) readHolderView(ctx context.Context, _ registry.Route
func TestHALostLeaseRecoveryWorkersBounded(t *testing.T) {
a := New(Config{}, lostDiscoveryBackend{}, nil)
a.haLoss.hosts = make(map[string]*haLeaseLoss)
+ a.Routes = withdrawalLookup(func(host string) (*registry.Route, error) {
+ return &registry.Route{Host: host, HA: true, Kind: "instance"}, nil
+ })
for i := 0; i < haLossWorkers+1; i++ {
- rt := registry.Route{Host: fmt.Sprintf("host%d.example.com", i), HA: true}
+ rt := registry.Route{Host: fmt.Sprintf("host%d.example.com", i), HA: true, Kind: "instance"}
a.mu.Lock()
a.haLoss.hosts[rt.Host] = &haLeaseLoss{route: rt}
a.mu.Unlock()
@@ -469,3 +494,230 @@ func TestHALostLeaseDuringOrdinaryStop(t *testing.T) {
r.lost.Store(true)
assertLostResponse(t, s)
}
+
+func TestHALostRecoveryShutdownDoesNotWaitForWake(t *testing.T) {
+ s, e, r := lostTestEntry(t)
+ finishLostTestEntry(t, s, e, r)
+ entered, unblock := make(chan struct{}), make(chan struct{})
+ old := acquireHALease
+ acquireHALease = func(ctx context.Context, bucket, prefix, region string, base hafresh.Marker, p hafresh.Proofs) (*lease.Held, error) {
+ close(entered)
+ <-unblock
+ return nil, context.Canceled
+ }
+ defer func() { close(unblock); s.a.haLoss.joined.Wait(); acquireHALease = old }()
+ other := &actEntry{host: "other.example.com", route: registry.Route{Host: "other.example.com", Kind: "instance"}, state: actAwake, proc: &fakeProc{}, rep: &fakeRep{}}
+ s.a.mu.Lock()
+ s.a.putEntryLocked(other.host, other)
+ s.a.mu.Unlock()
+ s.a.retryHALostHost(s.rt)
+ <-entered
+ ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond)
+ defer cancel()
+ done := make(chan struct{})
+ go func() { s.a.Shutdown(ctx); close(done) }()
+ select {
+ case <-done:
+ case <-time.After(time.Second):
+ t.Error("Shutdown waited for recovery wake beyond its context")
+ }
+ s.a.mu.Lock()
+ stopped := other.stopped
+ s.a.mu.Unlock()
+ select {
+ case <-stopped:
+ case <-time.After(time.Second):
+ t.Error("blocked recovery delayed the other host's stop")
+ }
+}
+
+func TestHALostRecoveryNoDemandNoS3(t *testing.T) {
+ s, e, r := lostTestEntry(t)
+ finishLostTestEntry(t, s, e, r)
+ before := len(s.fx.Requests())
+ s.a.retryHALostHosts()
+ s.a.haLoss.joined.Wait()
+ if got := len(s.fx.Requests()); got != before {
+ t.Fatalf("no requests: S3 calls=%d", got-before)
+ }
+ putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(time.Hour)))
+ clk := &fakeClock{t: time.Now()}
+ s.a.now = clk.now
+ s.a.retryHALostHost(s.rt)
+ s.a.haLoss.joined.Wait()
+ before = len(s.fx.Requests())
+ for range 12 {
+ clk.advance(10 * time.Second)
+ s.a.retryHALostHosts()
+ s.a.haLoss.joined.Wait()
+ }
+ if got := len(s.fx.Requests()); got != before {
+ t.Fatalf("foreign holder without new demand: S3 calls=%d", got-before)
+ }
+ s.a.retryHALostHost(s.rt)
+ s.a.haLoss.joined.Wait()
+ if len(s.fx.Requests()) <= before {
+ t.Fatal("new request did not retry discovery")
+ }
+}
+
+func TestHALostRecoveryParksChangedRoutes(t *testing.T) {
+ for _, change := range []string{"deleted", "disabled", "moved", "lifetime", "pool", "lookup error"} {
+ t.Run(change, func(t *testing.T) {
+ s, e, r := lostTestEntry(t)
+ finishLostTestEntry(t, s, e, r)
+ current := s.rt
+ s.a.Routes = withdrawalLookup(func(string) (*registry.Route, error) {
+ switch change {
+ case "deleted":
+ return nil, nil
+ case "lookup error":
+ return nil, errors.New("registry unavailable")
+ }
+ return &current, nil
+ })
+ switch change {
+ case "disabled":
+ s.a.SetDisabledFor(func(registry.Route) bool { return true })
+ case "moved":
+ current.Moved = true
+ case "lifetime":
+ current.Incarnation++
+ case "pool":
+ current.Pool = "elsewhere"
+ }
+ before := len(s.fx.Requests())
+ s.a.retryHALostHost(s.rt)
+ s.a.haLoss.joined.Wait()
+ if got := len(s.fx.Requests()); got != before {
+ t.Errorf("changed route performed %d S3 calls", got-before)
+ }
+ assertLossLatched(t, s)
+ })
+ }
+}
+
+func TestHALostQueuedStopWaiterRefusesOnLoss(t *testing.T) {
+ s, e, r := lostTestEntry(t)
+ r.lost.Store(false)
+ s.a.mu.Lock()
+ s.a.beginStopLocked(e, StopIdle)
+ s.a.mu.Unlock()
+ <-r.entered
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ w := httptest.NewRecorder()
+ done := make(chan struct{})
+ go func() {
+ s.a.ServeHTTP(w, httptest.NewRequest("GET", "http://shop.example.com/", nil).WithContext(ctx), s.rt, http.HandlerFunc(func(http.ResponseWriter, *http.Request) { t.Error("proxied") }))
+ close(done)
+ }()
+ select {
+ case <-done:
+ t.Fatal("ordinary stop did not wait")
+ case <-time.After(30 * time.Millisecond):
+ }
+ r.lost.Store(true)
+ assertLostResponse(t, s)
+ select {
+ case <-done:
+ if w.Code != 503 || w.Header().Get("Retry-After") != "10" || w.Header().Get("X-Carlos-Holder") != "none" || w.Body.String() != edge.HAUnavailableMessage+"\n" {
+ t.Errorf("queued loss response: %d %v %q", w.Code, w.Header(), w.Body.String())
+ }
+ case <-time.After(time.Second):
+ cancel()
+ <-done
+ t.Fatal("queued waiter did not wake at loss latch")
+ }
+}
+
+type lostCountingBackend struct {
+ *fakeBackend
+ reads atomic.Int32
+}
+
+func (b *lostCountingBackend) self() hafresh.Self { return hafresh.Self{Ref: "a", IID: "i-a"} }
+func (b *lostCountingBackend) readHolderView(context.Context, registry.Route) (hafresh.View, error) {
+ b.reads.Add(1)
+ return hafresh.View{}, errors.New("S3 unavailable")
+}
+
+func TestHALostRecoveryMassLossAndDemandExpiry(t *testing.T) {
+ b := &lostCountingBackend{fakeBackend: &fakeBackend{}}
+ a := New(Config{}, b, nil)
+ defer a.lifeStop()
+ clk := &fakeClock{t: time.Now()}
+ a.now = clk.now
+ a.Routes = withdrawalLookup(func(host string) (*registry.Route, error) {
+ return &registry.Route{Host: host, Kind: "instance", HA: true}, nil
+ })
+ a.haLoss.hosts = make(map[string]*haLeaseLoss)
+ for i := range 100 {
+ rt := registry.Route{Host: fmt.Sprintf("mass%d.example.com", i), Kind: "instance", HA: true}
+ a.haLoss.hosts[rt.Host] = &haLeaseLoss{route: rt}
+ }
+ for range 3 {
+ a.retryHALostHosts()
+ a.haLoss.joined.Wait()
+ clk.advance(time.Minute)
+ }
+ if b.reads.Load() != 0 {
+ t.Fatalf("mass loss without traffic performed %d reads", b.reads.Load())
+ }
+ rt := a.haLoss.hosts["mass0.example.com"].route
+ a.retryHALostHost(rt)
+ a.haLoss.joined.Wait()
+ if b.reads.Load() != 1 {
+ t.Fatal("request did not start discovery")
+ }
+ clk.advance(61 * time.Second)
+ for range 3 {
+ a.retryHALostHosts()
+ a.haLoss.joined.Wait()
+ clk.advance(time.Minute)
+ }
+ if b.reads.Load() != 1 {
+ t.Fatalf("expired demand kept polling: %d reads", b.reads.Load())
+ }
+ if b.acquires != 0 || b.starts != 0 {
+ t.Fatal("mass loss woke an app")
+ }
+}
+
+func TestHALostRecoveryFailedPostCheckStopsEntry(t *testing.T) {
+ s, e, r := lostTestEntry(t)
+ finishLostTestEntry(t, s, e, r)
+ var withdrawn atomic.Bool
+ original := s.b.ServedPools
+ s.b.ServedPools = func() edgesfile.PoolEvidence {
+ if withdrawn.Load() {
+ return edgesfile.PoolEvidence(`["another-pool"]`)
+ }
+ return original()
+ }
+ var recovered *actEntry
+ s.a.wakeEnded = func(host string) {
+ s.a.mu.Lock()
+ recovered = s.a.entries[host]
+ s.a.mu.Unlock()
+ withdrawn.Store(true)
+ }
+ s.a.retryHALostHost(s.rt)
+ s.a.haLoss.joined.Wait()
+ if recovered == nil {
+ t.Fatal("recovery did not wake")
+ }
+ s.a.mu.Lock()
+ stopped := recovered.stopped
+ why := recovered.stopWhy
+ s.a.mu.Unlock()
+ if stopped == nil || why != StopLeaseLost {
+ t.Fatal("failed post-check left a refused entry awake")
+ }
+ select {
+ case <-stopped:
+ case <-time.After(5 * time.Second):
+ t.Fatal("failed recovery did not stop")
+ }
+ assertLossLatched(t, s)
+}